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. EF Core 通过延迟加载获取导航属性数据

    EF 6及以前的版本是默认支持延迟加载(Lazy Loading)的,早期的EF Core中并不支持,必须使用Include方法来支持导航属性的数据加载. 当然在EF Core 2.1及之后版本中已经 ...

  2. 【Offer】[33] 【二叉搜索树的后序遍历序列】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果.如果是则返回true, 否则返回false. 假设输入的数组的任意两个数字 ...

  3. centos 6.5 搭建DHCP实验

    搭建DHCP服务 安装DHCP服务 挂载光盘:mount /dev/cdrom /qswz 从光盘的安装包中安装DHCP rpm -ivh dhcp-4.1.1-38.P1.el6.centos.i6 ...

  4. 云开发数据库VS传统数据库丨云开发101

    云开发数据库与传统数据库的不同 在小程序·云开发中,最核心的便是三大组件:数据库.云存储和云函数,从今天开始,我们将开始隔日更的专栏文章,云开发101,在第一周,我们将从最最核心的数据库开始说起. 云 ...

  5. PHP 异或 算法

    /** * PHP字符串“异或”算法 * param array key * @param Request $request * @return mixed|string|void */ public ...

  6. 正确应用Java数组

    一.数组的特点 数组与其他容器的区别有三方面:效率.类型和保存基本类型的能力. 1.效率.数组是一种效率最高的存储和随机访问对象引用序列的方式.数组是一段连续地址空间内的线性序列,所以访问非常快.但也 ...

  7. apache ignite系列(二):配置

    ignite有两种配置方式,一种是基于XML文件的配置,一种是基于JAVA代码的配置: 这里将ignite常用的配置集中罗列出来了,一般建议使用xml配置. 1,基于XML的配置 <beans ...

  8. JDBC连接时出现的问题总结

    java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more tha ...

  9. CDH5.16.1离线集成Phoenix

    1.安装环境 Centos 7.6 CDH 5.16.1 2.下载Phoenix所需的parcel包 3.上传parcel包到ClouderaManager server所在的节点上 /opt/clo ...

  10. Intellij IDEA 2019 + Java Spring MVC + Hibernate学习笔记(2)

    书接上文 首先根据各种Spring MVC教程,建立了基础的结构,是否合理不知道,姑且先这样,有问题再解决问题.学习新东西,不能怕掉坑里... 查询网上别人的经历说需要把根目录下的lib目录下的所有包 ...