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. 在Windows Phone中使用HTML编程

    在开发Windows Phone的项目中,需求中有几个页面是要用表格来布局的(效果图如下),由于Grid中有的边线是虚的,而且没有边线,果断放弃了,用了border将表格的线加上去了.于是在有表格布局 ...

  2. 基于BT协议的文件分发系统

    基于BT协议的文件分发系统构成:    1.一个Web服务器:保存着种子文件    2.一个种子文件:保存共享文件的一些信息(文件名,文件大小    ,Tracker服务器地址,torrent为后缀) ...

  3. 纯css3的上下左右提示框几种方法

    经常用到三角形提示框,用图片吧,大小框不定,所以,css自己写了,可设置宽高比,就可自适应了. 图形例子如下: css代码如下 <style type="text/css"& ...

  4. SequoiaDB 1.5 版本发布

    SequoiaDB 1.5 – 2013.11.13 新特性    1. 新增聚合特性,API实现 GROUPBY, MAX 等功能:    2. 全新改版的Web管理界面:    3. 提供C#语言 ...

  5. Requests库的几种请求 - 通过API操作Github

    本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...

  6. openerp经典收藏 对象定义详解(转载)

    对象定义详解 原文地址:http://shine-it.net/index.php/topic,2159.0.htmlhttp://blog.sina.com.cn/s/blog_57ded94e01 ...

  7. Python脚本控制的WebDriver 常用操作 <二十七> 文件下载

    测试用例场景 webdriver允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中. Python脚本 测试用Python代码: # coding=gbk ''' Crea ...

  8. Python编码与解码

    # -*- coding: utf-8 -*- # 直接保存为Python脚本,对照执行结果会好看点. # 实验的内容都是在Python 2.7.x下进行的. # Python3默认采用unicode ...

  9. APACHE 403 FORBIDDEN错误的解决办法之一

    打开 apache的配置文件httpd.conf,找到这段代码: Options FollowSymLinksAllowOverride NoneOrder deny,allowDeny from a ...

  10. 修改myeclipse的jsp模板

    在myeclipse的安装目录下: C:\Users\Seeker\AppData\Local\MyEclipse Professional\plugins 找到com.genuitec.eclips ...