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 (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.

解题报告:

各种乱搞+贪心WA一片,最后按w[i]+s[i]从大到小排序就莫名对了,后来仔细一想还是可靠的,假设A在B上面,那么 \(rist_A=Sum-w_A-w_B-s_A\) 并且交换A,B位置之后上面位置的risk都不变,但是下面的就会变成\(Sum_B-w_A-s_A\),所以贪心策略就是把 \(w_i+s_i\)最大的放在最下面,以此类推

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=5e4+5;
int w[N],s[N],a[N];
bool comp(int i,int j){
return w[i]+s[i]>w[j]+s[j];
}
void work()
{
int n;ll tot=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&w[i],&s[i]);
a[i]=i;tot+=w[i];
}
sort(a+1,a+n+1,comp);
int ans=-2e9;
for(int i=1;i<=n;i++){
ans=Max(ans,tot-w[a[i]]-s[a[i]]);
tot-=w[a[i]];
}
printf("%d\n",ans);
} int main()
{
work();
return 0;
}
上一篇:Linux进程实践(2) --僵尸进程与文件共享


下一篇:利用CSS3实现div页面淡入动画特效