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.

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

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
原创粉丝点击