首页 技术 正文
技术 2022年11月15日
0 收藏 846 点赞 2,617 浏览 1756 个字

题意

Farmer John’s N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking

and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing

acrobatic stunts.

The cows aren’t terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The

cows are trying to figure out the order in which they should arrange themselves ithin this stack.

Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the

combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to

determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.

分析

考虑相邻的两头牛i和i+1,初始时他们的难受值是

\[\sum_{j=i+1}^n W_j-S_i \quad \sum_{j=i+2}^nW_j-S_{i+1}
\]

交换后的难受值是

\[\sum_{j=i+1}^nW_j+W_i-S_{i+1} \quad \sum_{j=i+2}^n W_j -S_i
\]

观察式子,发现需要比较的是

\[W_{i+1}-S_i \quad W_i-S_{i+1}
\]

设前者小于后者,则

\[W_i+S_i>W_{i+1}+S_{i+1}
\]

所以W和S的和大的牛排在下面更优。

时间复杂度\(O(N \log N)\)

代码

#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll;co int N=5e4+1;
int w[N],s[N],id[N];
bool cmp(int x,int y){
return w[x]+s[x]<w[y]+s[y];
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int n=read<int>();
for(int i=1;i<=n;++i)
read(w[i]),read(s[i]),id[i]=i;
std::sort(id+1,id+n+1,cmp);
int ans=0,sum=0;
for(int i=1;i<=n;++i)
ans=std::max(ans,sum-s[id[i]]),sum+=w[id[i]];
printf("%d\n",ans);
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903