Cow Acrobats POJ - 3045

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.

1.详解 http://www.cnblogs.com/flipped/p/6372321.html

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<cstring>
 6 using namespace std;
 7 typedef long long ll;
 8
 9 struct node{
10     int we,st;
11     bool operator<(const node& i)const{
12         return (we+st)<(i.we+i.st);
13     }
14 }a[50005];
15
16 int n;
17
18 int main()
19 {   while(~scanf("%d",&n)){
20        for(int i=0;i<n;i++) scanf("%d%d",&a[i].we,&a[i].st);
21        sort(a,a+n);
22        ll pre=0,ma=-1000000000;
23        for(int i=0;i<n;i++){
24              ma=max(pre-a[i].st,ma);
25              pre+=a[i].we;
26        }
27        printf("%lld\n",ma);
28     }
29     return 0;
30 }
时间: 2024-10-15 21:55:45

Cow Acrobats POJ - 3045的相关文章

Greedy:Cow Acrobats(POJ 3045)

牛杂技团 题目大意:一群牛想逃跑,他们想通过搭牛梯来通过,现在定义risk(注意可是负的)为当前牛上面的牛的总重量-当前牛的strength,问应该怎么排列才能使risk最小? 说实话这道题我一开始给书上的二分法给弄懵了,后来看了一下题解发现根本不是这么一回事,其实就是个简单的贪心法而已. 这题怎么贪心呢?就是按w+s从小到大排序就好了,证明一下: 1.先证明如果不满足w+s的序列性,无论谁在谁的上面,都会违反题设:(设A在B的上面) 如果 A.s+A.w<B.s+B.w 则如果B.s<m+A

POJ 3045 Cow Acrobats (想法题)

题目链接:POJ 3045 Cow Acrobats 题意:有n只牛叠罗汉,危险指数的计算是 该层牛以上的牛重量总和减去这层牛的强度,求使最大的危险指数中的最小值. 思路:根据w+s排序,最大的在最下面,道理很简单,危险指数: sum-(w+s),(sum该层牛以上的牛重量总和). AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #define ll __int64 using namespa

POJ3045 Cow Acrobats —— 思维证明

题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5713   Accepted: 2151 Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. The

[USACO2005][POJ3045]Cow Acrobats(贪心)

题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风险值 分析:这就是noip2012 T2的来源= =只不过这里是加,noip里是乘 不妨设所有牛都按最优顺序排好了,考虑相邻的两头牛i和i+1,如果交换他们的位置,那么对前面和后面的结果都无影响,只是他们两个的风险值变化了(变大了),于是我们可以得到这个时候i和i+1的关系 设w1+w2+...+wi-1

bzoj1629[Usaco2007 Demo]Cow Acrobats*

bzoj1629[Usaco2007 Demo]Cow Acrobats 题意: n头牛,每天牛都有体重与力量值.它们玩叠罗汉的游戏,每个牛的危险值等于它上面的牛的体重总和减去它的力量值,求所有方案中危险值最大的最小. 题解: 贪心.第i头牛比第j头牛高当且仅当i的重量-j的力量<j的重量-i的力量. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn

【BZOJ 1629】 [Usaco2007 Demo]Cow Acrobats

1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 657  Solved: 331 [Submit][Status] 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 p

Cow Acrobats(贪心)

Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3686   Accepted: 1428 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 tig

1629: [Usaco2007 Demo]Cow Acrobats

1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1023  Solved: 531[Submit][Status][Discuss] Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofe

poj 3045 Cow Acrobats(数学题)

题目链接:http://poj.org/problem?id=3045 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