POJ cow acrobats
来源:互联网 发布:钣金放样软件 编辑:程序博客网 时间:2024/06/11 18:04
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.
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.
Output
* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
Sample Input
3
10 3
2 5
3 3
Sample Output
2
Hint
OUTPUT DETAILS:
Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.
贪心,两个条件对判断稳定程度同等重要。找正确的排序规则。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
long long num[50001];
struct cow
{
int h,s;
long long sum;
}c[50001];
bool cmp(cow a,cow b)
{
return a.sum<b.sum;
}
int main()
{
int i,j,Max=-999999999;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&c[i].h,&c[i].s);
c[i].sum=c[i].h+c[i].s;
}
sort(c+1,c+n+1,cmp);
num[1]=0;
num[2]=c[1].h;
for(i=3;i<=n;i++)
num[i]=num[i-1]+c[i-1].h;
for(i=1;i<=n;i++)
{
if(num[i]-c[i].s>Max)
Max=num[i]-c[i].s;
}
cout<<Max<<endl;
return 0;
}
- poj-3045 Cow Acrobats
- POJ 3045 Cow Acrobats
- POJ 3045-Cow Acrobats
- POJ-3045-Cow Acrobats
- POJ 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
- Cow Acrobats POJ
- POJ cow acrobats
- Cow Acrobats - POJ 3045 排序
- POJ 3045 Cow Acrobats 贪心
- poj 3045 Cow Acrobats(数学题)
- poj 3045 Cow Acrobats 贪心
- POJ 3045 - Cow Acrobats(贪心)
- 2017多校联合第8场1008/hdu 6140Hybrid Crystals(思维)
- MyBatis之java.lang.UnsupportedOperationException异常解决方案
- Tomcat(2)生命周期管理
- 前端框架Easey UI(二)表格控件的查找与删除操作消息提示对话框
- NYOJ 79 拦截导弹
- POJ cow acrobats
- 《Java源码分析》:ConcurrentHashMap JDK1.8
- iOS MermaidSwimming(美人鱼游泳)~demo
- 链表的基本操作
- 2017暑假训练第十七天
- MySQL怎么使用
- 深入解析Java并发程序中线程的同步与线程锁的使用
- 车辆姿态角(Euler角)Pitch、Yaw、Roll 的设定
- jdk环境变量配置