poj 3045 Cow Acrobats(二分搜索?)
Description
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 ( <= W_i <= ,) and strength ( <= S_i <= ,,,). 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.
Input
* Line : A single line with the integer N. * Lines ..N+: Line i+ describes cow i with two space-separated integers, W_i and S_i.
Output
* Line : A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
Sample Output
Hint
OUTPUT DETAILS: Put the cow with weight on the bottom. She will carry the other two cows, so the risk of her collapsing is +-=. The other cows have lower risk of collapsing.
Source
题意:这道题居然和今年成都赛区的倒数第二题一模一样。。。或者说该反过来说、、给你n头牛叠罗汉,每头都有自己的重量w和力量s,承受的风险数就是该牛上面牛的总重量减去它的力量,题目要求设计一个方案使得所有牛里面风险最大的要最小。
题解:按照w+s贪心叠,越大的越在下面。如果最优放置时,相邻两头牛属性分别为w1,s1,w2,s2,第一头牛在第二头上面,sum为第一头牛上面的牛的体重之和,那么
第一头牛风险:a=sum-s1;第二头牛风险:b=sum+w1-s2;交换两头牛位置之后
a'=sum+w2-s1,b'=sum-s2,
由于是最优放置,所以w2-s1>=w1-s2,即w2+s2>=w1+s1,所以和最大的就该老实的在下面呆着= =!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define N 50006
#define inf 1<<30
struct Node{
int w,s;
}cows[N];
int sum[N];
bool cmp(Node a,Node b){
return a.w+a.s<b.w+b.s;
}
int main()
{
int n;
while(scanf("%d",&n)==){
for(int i=;i<=n;i++){
scanf("%d%d",&cows[i].w,&cows[i].s);
}
sort(cows+,cows+n+,cmp);
int ans=-inf;
sum[]=;
for(int i=;i<=n;i++){
sum[i]=sum[i-]+cows[i].w;
ans=max(ans,sum[i-]-cows[i].s);
}
printf("%d\n",ans);
}
return ;
}
poj 3045 Cow Acrobats(二分搜索?)的更多相关文章
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- POJ 3045 Cow Acrobats
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- POJ - 3045 Cow Acrobats (二分,或者贪心)
一开始是往二分上去想的,如果risk是x,题目要求则可以转化为一个不等式,Si + x >= sigma Wj ,j表示安排在i号牛上面的牛的编号. 如果考虑最下面的牛那么就可以写成 Si + ...
- POJ 3045 Cow Acrobats (最大化最小值)
题目链接:click here~~ [题目大意] 给你n头牛叠罗汉.每头都有自己的重量w和力量s,承受的风险数rank就是该牛上面全部牛的总重量减去该牛自身的力量,题目要求设计一个方案使得全部牛里面风 ...
- POJ3045 Cow Acrobats —— 思维证明
题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- 【POJ - 3045】Cow Acrobats (贪心)
Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...
- 【POJ3045】Cow Acrobats(贪心)
BUPT2017 wintertraining(16) #4 B POJ - 3045 题意 n(1 <= N <= 50,000) 个牛,重wi (1 <= W_i <= 1 ...
- POJ 3045
Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 Accepted: 912 Descr ...
- BZOJ1629: [Usaco2007 Demo]Cow Acrobats
1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 601 Solved: 305[Su ...
随机推荐
- node js npm 和 cnpm的使用
安装nodejs后会有npm命令 npm 可以安装node插件 cnpm使用的是淘宝网的镜像http://npm.taobao.org 安装命令提示符执行:npm install cnpm -g -- ...
- WebService-调用第三方提供的webService服务
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...
- PHP设计模式笔记三:三种基本设计模式(工厂模式、单例模式、注册树模式) -- Rango韩老师 http://www.imooc.com/learn/236
一.工厂设计模式 index.php $db = IMooc\Factory::createDatabase(); 使用工厂类的静态方法直接创建一个dababase对象,当类名发生修改时,在工厂里修改 ...
- Android开发之DatePickerDialog与TimePickerDialog的功能和使用方法具体解释
DatePickerDialog与TimePickerDialog的功能比較简单,使用方法也非常easy.仅仅要以下两步就可以. Ø 通过newkeyword创建DatePickerDialog.T ...
- 最流行的android组件大全
目录 [−] 工具和教程 UI组件 类库 游戏引擎 Android HTML5应用 Android 是目前最流行的移动操作系统(还需要加之一吗?). 随着新版本的不断发布, Android的功能也日益 ...
- 如何判断Linux load的值是否过高
1.先使用top看下CPU占用高的进程,找出进程的进程ID(pid): 查看方法:top 2.根据进程ID(pid)查看是进程的那些线程占用CPU高. 查看方法:top -Hp pid 3.使用pst ...
- Entity Framework - Func引起的数据库全表查询
原文:http://www.cnblogs.com/dudu/archive/2012/04/01/enitity_framework_func.html 使用 Entity Framework 最要 ...
- Material 字体样式与排版
Material 字体样式与排版 Google Material 设计规范文档 Style– Typography 字号 再好的布局也会被过多的字号和样式会毁掉.要规定好在这个布局上合适的 字号集合 ...
- windows下配置svn的https访问
svn是一个功能强大的代码版本管理系统,可以将服务端安装在linux.unix以及windows下.svn通常采用http方式进行代码提交与下载.由于密码采用明文传输,因此存在泄密的风险.若采用htt ...
- C++中使用stringstream简化类型转换
C++标准库中的<sstream>提供了一个stringstream,以前基本没用过,突然发现很好用(^-^)V 参见 http://www.cplusplus.com/reference ...