Cow Acrobats
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2302   Accepted: 912

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

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.

Source

 
按照w + s从小到大排序,证明的思想假如是任意一个排列,进行类似冒泡的操作就可以不断的减少最大值
 
 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define maxn 50005 #define INF (1 << 30) struct node {
int w,s;
}; node s[maxn];
int n; bool cmp(node a,node b) {
return (a.w + a.s) < (b.w + b.s);
} int main() { scanf("%d",&n);
for(int i = ; i <= n; ++i) {
scanf("%d%d",&s[i].w,&s[i].s);
} sort(s + ,s + n + ,cmp); int now = ,ans = -INF ;
for(int i = ; i <= n; ++i) {
ans = max(ans,now - s[i].s);
now += s[i].w;
} printf("%d\n",ans); return ; }

POJ 3045的更多相关文章

  1. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

  2. Greedy:Cow Acrobats(POJ 3045)

    牛杂技团 题目大意:一群牛想逃跑,他们想通过搭牛梯来通过,现在定义risk(注意可是负的)为当前牛上面的牛的总重量-当前牛的strength,问应该怎么排列才能使risk最小? 说实话这道题我一开始给 ...

  3. poj 3045 Cow Acrobats(二分搜索?)

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  4. POJ 3045 Cow Acrobats

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  5. POJ - 3045 Cow Acrobats (二分,或者贪心)

    一开始是往二分上去想的,如果risk是x,题目要求则可以转化为一个不等式,Si + x >= sigma Wj ,j表示安排在i号牛上面的牛的编号. 如果考虑最下面的牛那么就可以写成 Si + ...

  6. poj 3045 叠罗汉问题 贪心算法

    题意:将n头牛叠起来,每头牛的力气 s体重 w  倒下的风险是身上的牛的体重的和减去s 求最稳的罗汉倒下去风险的最大值 思路: 将s+w最大的放在下面,从上往下看 解决问题的代码: #include& ...

  7. POJ 3045 Cow Acrobats (最大化最小值)

    题目链接:click here~~ [题目大意] 给你n头牛叠罗汉.每头都有自己的重量w和力量s,承受的风险数rank就是该牛上面全部牛的总重量减去该牛自身的力量,题目要求设计一个方案使得全部牛里面风 ...

  8. 【POJ - 3045】Cow Acrobats (贪心)

    Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...

  9. POJ 2456 3258 3273 3104 3045(二分搜索-最大化最小值)

    POJ 2456 题意 农夫约翰有N间牛舍排在一条直线上,第i号牛舍在xi的位置,其中有C头牛对牛舍不满意,因此经常相互攻击.需要将这C头牛放在离其他牛尽可能远的牛舍,也就是求最大化最近两头牛之间的距 ...

随机推荐

  1. 济南学习 Day2 T1 am

    T1 题意:从1− n中找一些数乘起来使得答案是一个完全平方数,求这个完全平方数 最大可能是多少. 解析: 1.  质因数分解 2.  1->n用质因数指数的相加的形式将1*n累乘起来 3.   ...

  2. UIView背景渐变三种方法

    //此作品非原创 #import "ACViewController.h" @interface ACViewController () @end @implementation  ...

  3. Linux常用命令操作说明(链接)

    1. Busybox下tftp命令使用详解 2. Linux中rc的含义 3. <Unix文件系统结构标准>(Filesystem Hierarchy Standard) 4. 用size ...

  4. javascript "\" 在字符串里的是转义的意思

    "\n" 在字符串里是换行的意思 alert("dfdsad\ndfsdf");   //有换行 alert("abc\de");   // ...

  5. Lucene Field

    org.apache.lucene.demo.IndexFiles类中,使用递归的方式去索引文件.在构造了一个IndexWriter索引器之后,就可以向索引器中添加Doucument了,执行真正地建立 ...

  6. 基础学习总结(八)--HttpClient

    在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端.它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET ...

  7. 计算 sql查询语句所花时间

    --1:下面这种是SQL Server中比较简单的查询SQL语句执行时间方法,通过查询前的时间和查询后的时间差来计算的: declare @begin_date datetimedeclare @en ...

  8. Bootstrap模态框

    backdrop选项,当设置成false的时候, 背景不会出现半透明的遮盖层,当用户点击模态框外部时不会关闭模态框: 设置成true的时候会出现遮盖层,当用户点击模态框外部时则会关闭模态框. 那如果又 ...

  9. C#inSSIDer强大的wifi无线热点信号扫描器源码

    一个完整的无线信号扫描工具源码,包含了从热点扫描到强度绘制以及信号变换曲线图.源码基于Managed Wifi实现基础功能,Managed Wifi也是开源项目,这个可以在本站搜索到. 指定网卡信号扫 ...

  10. jquery批量控制form禁用的代码

    jquery批量控制form禁用的代码. 代码: <script type="text/javascript" src="/jquery/jquery-1.8.2. ...