1629: [Usaco2007 Demo]cow acrobats Time limit: 5 Sec Memory Limit: + MB
Submit: 657 Solved: 331
[Submit] [Status] Descriptionfarmer John ' s n (1 <= n <= 50,000) cows (numbered 1..N) is 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 co W out of a cannon met with a dismal failure). Thus, they has decided to practice performing acrobatic stunts. The cows aren ' t terribly creative and has only come up with one acrobatic stunt:standing on top of each of these to form a Vertical stack of some height. The cows is trying to figure out the order in which they should arrange themselves within this stack. Each of the N cows have an associated weight (1 <= w_i <=) 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 hers (not including her own weight, of C Ourse) minus her strength (so, a stronger cow has a lower risk). Your task is to determine a ordering of the cows that minimizes the greatest risk of collapse for any of the cows. There are three cows, the following three rows of two numbers represent their weight and strength//They play dogpile games, each cow's dangerous value equals the total weight of the cow above it minus its strength value, because it has to carry up all the cattle above. Find the smallest input* in all scenarios with the highest risk value Line 1: A, A, and the integer N. * Lines 2..n+1:line i+1 describes cow I with II space-s eparated integers, w_i and s_i.output* Line 1: A single integer, giving the largest risk of all the cows in any optimal or Dering that minimizes the risk. Sample Input3
10 3
2 5
7 ·
Sample Output2
OUTPUT DETAILS:
Put the cow with weight in the bottom. She'll carry the other
The cows, so the risk of hers collapsing is 2+3-3=2. The other cows
has lower risk of collapsing.
HINT
Source
Silver
Greedy.
This problem is almost the same as the "noip2012" King game.
The first thing to understand is that for the first I, the J,K Exchange order placed above him has no effect on him.
It is now possible to consider only the first j,k:
J on top of K, when and only if WJ-SK<WK-SJ is Wj+sj<wk+sk
So all we have to do is follow the Wi+si ascending order and place them in turn.
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include < Cmath>using namespace Std;int n;struct data{int x,y,s;} A[50005];bool CMP (data a,data b) {return A.S<B.S;} int main () { scanf ("%d", &n), for (int i=1;i<=n;i++) scanf ("%d%d", &a[i].x,&a[i].y), A[i].s=a[i].x+a [I].y;sort (a+1,a+1+n,cmp); int sum=a[1].x,ans=-a[1].y;for (int i=2;i<=n;i++) Ans=max (ANS,SUM-A[I].Y), sum+=a[i].x ; Cout<<ans<<endl;return 0;}
"Bzoj 1629" [Usaco2007 Demo]cow Acrobats