题意

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;
}

POJ3045 Cow Acrobats的更多相关文章

  1. poj3045 Cow Acrobats (思维,贪心)

    题目: poj3045 Cow Acrobats 解析: 贪心题,类似于国王游戏 考虑两个相邻的牛\(i\),\(j\) 设他们上面的牛的重量一共为\(sum\) 把\(i\)放在上面,危险值分别为\ ...

  2. POJ3045 Cow Acrobats 2017-05-11 18:06 31人阅读 评论(0) 收藏

    Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4998   Accepted: 1892 Desc ...

  3. POJ3045 Cow Acrobats —— 思维证明

    题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  4. POJ-3045 Cow Acrobats (C++ 贪心)

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  5. poj3045 Cow Acrobats(二分最大化最小值)

    https://vjudge.net/problem/POJ-3045 读题后提取到一点:例如对最底层的牛来说,它的崩溃风险=所有牛的重量-(底层牛的w+s),则w+s越大,越在底层. 注意范围lb= ...

  6. [USACO2005][POJ3045]Cow Acrobats(贪心)

    题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风 ...

  7. 【POJ - 3045】Cow Acrobats (贪心)

    Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...

  8. BZOJ1629: [Usaco2007 Demo]Cow Acrobats

    1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 601  Solved: 305[Su ...

  9. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

随机推荐

  1. python UI自动化测试

    为了减小维护成本: 1.UI自动化测试需要有较为稳定的环境 2.代码设计合理,那么我们就需要面向对象的设计一个框架,将重复的代码模块化 一.首先总结一下 UI自动化大概要哪些模块 1.config(配 ...

  2. Number.MIN_VALUE常量说明

    Number.MIN_VALUE常量说明 Number.MIN_VALUE表示的最小值为5e-324 MIN_VALUE代表的并不是负最小,而是最接近0的一个数 负最小值可以使用-Number.MAX ...

  3. 使用jenkins自动构建docker容器范例

    1.登录Jenkins,新建一个自由风格的软件项目. 2.源码管理选择git,并添加Repository URL.Credentials 3.构建选择 Execute Shell,命令如下: dock ...

  4. Spring框架中,在工具类或者普通Java类中调用service或dao

    spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...

  5. 小米2S手机 - Charles无法安装证书 因为无法读取证书

    1.不要使用小米原装的浏览器安装证书 2.使用第三方浏览器安装,如我使用的是UC浏览器 3.使用第三方浏览器安装的证书格式是".pem"格式问卷 4.将这个文件放入小米的downl ...

  6. 伪基站,卒于5G——本质上是基于网络和UE辅助的伪基站检测,就是将相邻基站的CI、信号强度等信息通过测量报告上报给网络,网络结合网络拓扑、配置信息等相关数据,对所有数据进行综合分析,确认在某个区域中是否存在伪基站

    伪基站,卒于5G from:https://www.huxiu.com/article/251252.html?h_s=h8 2018-07-05 21:58收藏27评论6社交通讯     本文来自微 ...

  7. linux FTP 操作

    1.登陆: ftp 172.xxx.xxx.xxx 按提示输入用户名和密码 2.上传: 单个文件:put /路径/文件名 批量: 输入 prom 此命令是关闭交互(否则总是询问你是否要上传) 输入下载 ...

  8. bzoj3623

    题解: 刚看到题目,还以为是2-sat 可是似乎不对啊... 然后就只能爆搜了 看了网上的题解,woc还真是报搜 然后就ac了 当然爆搜还要随机化 代码: #include<bits/stdc+ ...

  9. redhat9安装gcc(转)

    原文链接:http://blog.chinaunix.net/uid-20260767-id-118036.html 第一种方法: 相信现在还有不少人在用经典的RedHat9,毕竟他是完全免费的红帽L ...

  10. webpack 事件触发 按需加载

    比较易懂, 方法简单 var util_sync = require('./util-sync.js') alert(util_sync.data) document.getElementById(& ...