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.

 
题解:
  和国王游戏很像对不对,有是一道贪心题,顺序是按照si+wi从小到大排序!为什么呢?我们来证明一下。
  首先两个相邻的牛i,i+1,根据排序有wi+si<w(i+1)+s(i+1).我们考虑每个牛的危险度,对于不是i和i+1的其他牛,因为他们的si不会变,所需要承受的重量不会变,所以危险度就不变,i这头牛的危险度是si-sum(Wi-1),i+1的危险度为s(i+1)-wi-sum(wi-1)。换位置,i+1这牛就是s(i+1)-sum(wi-1),i为si-(wi+1)-sum(wi-1).将做差就可以发现恒大于了。
 
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 50100
#define ll long long
using namespace std;
struct node{
int w,s;
}a[MAXN*];
int n; bool cmp(node x,node y){
return x.w+x.s<y.w+y.s;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&a[i].w,&a[i].s);
sort(a+,a+n+,cmp);
int ans=-(<<),sum=;
for(int i=;i<=n;i++){
ans=max(ans,sum-a[i].s);
sum+=a[i].w;
}
printf("%d\n",ans);
return ;
}

Cow Acrobats的更多相关文章

  1. BZOJ1629: [Usaco2007 Demo]Cow Acrobats

    1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 601  Solved: 305[Su ...

  2. POJ 3045 Cow Acrobats (贪心)

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

  3. Cow Acrobats(贪心)

    Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3686   Accepted: 1428 Desc ...

  4. POJ3045 Cow Acrobats 2017-05-11 18:06 31人阅读 评论(0) 收藏

    Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4998   Accepted: 1892 Desc ...

  5. POJ3045 Cow Acrobats —— 思维证明

    题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  6. poj3045 Cow Acrobats (思维,贪心)

    题目: poj3045 Cow Acrobats 解析: 贪心题,类似于国王游戏 考虑两个相邻的牛\(i\),\(j\) 设他们上面的牛的重量一共为\(sum\) 把\(i\)放在上面,危险值分别为\ ...

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

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

  8. BZOJ 1629: [Usaco2007 Demo]Cow Acrobats

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

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

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

  10. POJ-3045 Cow Acrobats (C++ 贪心)

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

随机推荐

  1. zabbix监控nginx脚本

    ~]# cd /etc/zabbix/scripts/ scripts]# ls nginx_status.sh scripts]# cat nginx_status.sh ############# ...

  2. golang开发:类库篇(五)go测试工具goconvey的使用

    为什么要使用goconvey测试程序 goconvey 集成go test,go test 无缝接入.管理运行测试用例,而且提供了丰富的函数断言.非常友好的WEB界面,直观的查看测试结果. 如果没有g ...

  3. WPF 自定义UI控件学习

    最近项目中运用到了WPF处理三维软件,在C/S结构中WPF做UI还是有很多优越性,简单的学了一点WPF知识,成功的完成项目目标.项目过度阶段对于WPF的一些基本特点有了进一步了解 .至此花费一点时间研 ...

  4. idea控制台乱码解决方案

    第一步:修改intellij idea配置文件: 找到intellij idea安装目录,bin文件夹下面idea64.exe.vmoptions和idea.exe.vmoptions这两个文件,分别 ...

  5. VMware Workstation Fixed Unable to connect to the MKS

    场景:早上开虚拟机时突然报这个错 解决办法如下: 以管理员的身份打开CMD,然后执行如下命令: net start vmx86 net start hcmon net start vmauthdser ...

  6. 5.1、顺序队列(java实现)

    1.实现代码 public class SeqQueue { private final int MaxSize = 8; private int rear; //队尾指针 private int f ...

  7. html的表格边框为什么会这么粗?

    因为默认情况下,cellspacing = 2px. 当表格的 border 不为 0 的时候,单元格(cell)的 border 为 1. 只有当表格的 border 设置为 0 的时候,单元格的 ...

  8. Linux环境基于CentOS7 搭建部署Docker容器

    1.Docker容器概述 区分Docker容器技术和VM虚拟机技术: evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom ...

  9. jmeter运行第三方java项目

    自己写了个简化系统操作的小工具,因为不想给别人用的时候占用本地资源于是写的是纯java项目,后面放到jmeter中通过beanshell sampler调用. java源码不贴了,把写好的项目导出成可 ...

  10. VUE从入门到放弃(项目全流程)————VUE

    VUE从入门到放弃(第一天)--整体流程 先想想一个项目,vue项目是从什么到什么,然后再什么的?那是什么呢? 搭建 ( vue-cli) 代码内容 运行 封装 成品 一.搭建(脚手架vue-cli) ...