Cow Acrobats - POJ 3045 排序
来源:互联网 发布:淘宝日版手办 编辑:程序博客网 时间:2024/05/29 21:32
Cow Acrobats
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2508 Accepted: 985
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 (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.
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.
Input
* Line 1: A single line with the integer N.
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
Output
* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
Sample Input
310 32 53 3
Sample Output
2
题意:让你给n个牛排序,使这些牛最大的危险度最小。
思路:让体重大的,承受大的往下排,你要是问为什么这样,看看我排序的那句大概就明白了。
AC代码如下:
#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct node{ int w; int s;}cow[50010];bool cmp(node a,node b){ if(a.w-b.s<b.w-a.s) return true; else if(a.w-b.s==b.w-a.s) return a.w<b.w; else return false;}int main(){ int n,i,j,k,ans=0,sum=0,ret; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d%d",&cow[i].w,&cow[i].s); sort(cow+1,cow+1+n,cmp); // for(i=1;i<=n;i++) // printf("%d %d\n",cow[i].w,cow[i].s); ans=0-cow[1].s; sum+=cow[1].w; for(i=2;i<=n;i++) { ret=sum-cow[i].s; if(ret>ans) ans=ret; //printf("%d %d\n",sum,ret); sum+=cow[i].w; } //if(ans<0) // ans=0; printf("%d\n",ans);}
0 0
- Cow Acrobats - POJ 3045 排序
- POJ 3045 Cow Acrobats(排序题)
- poj-3045 Cow Acrobats
- POJ 3045 Cow Acrobats
- POJ 3045-Cow Acrobats
- POJ-3045-Cow Acrobats
- POJ 3045 Cow Acrobats
- poj 3045 Cow Acrobats
- POJ 3045 Cow Acrobats
- poj 3045 Cow Acrobats
- POJ-3045Cow Acrobats
- poj 3045 Cow Acrobats
- POJ 3045 Cow Acrobats 贪心
- poj 3045 Cow Acrobats(数学题)
- poj 3045 Cow Acrobats 贪心
- POJ 3045 - Cow Acrobats(贪心)
- POJ - 3045 Cow Acrobats 贪心
- poj 3045 Cow Acrobats 贪心
- 一篇优秀的MVC教程
- 80端口被占用了!修改Apache端口号
- Hql 一种错误写法的分析
- Optional优雅的使用null
- 定时且周期性的任务研究I--Timer
- Cow Acrobats - POJ 3045 排序
- Radar Installation
- 黑马程序员——Enum的原理及用法总结
- win下用cmd编译cpp文件
- 最容易植入恶意代码具有极大的欺骗性
- 火星上发现外星人留下的金字塔和城市
- TexturePacker非常棒的图像处理工具
- 几篇android物理按键的文章
- POJ 2677(双调旅行商问题<bictonicTSP>