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 ( <= W_i <= ,) and strength ( <= S_i <= ,,,). 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 : A single line with the integer N. 

* Lines ..N+: Line i+ describes cow i with two space-separated integers, W_i and S_i. 

Output

* Line : A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
Sample Input

Sample Output


Hint

OUTPUT DETAILS: 

Put the cow with weight  on the bottom. She will carry the other two cows, so the risk of her collapsing is +-=. The other cows have lower risk of collapsing.

Source

 

题意:这道题居然和今年成都赛区的倒数第二题一模一样。。。或者说该反过来说、、给你n头牛叠罗汉,每头都有自己的重量w和力量s,承受的风险数就是该牛上面牛的总重量减去它的力量,题目要求设计一个方案使得所有牛里面风险最大的要最小。

题解:按照w+s贪心叠,越大的越在下面。如果最优放置时,相邻两头牛属性分别为w1,s1,w2,s2,第一头牛在第二头上面,sum为第一头牛上面的牛的体重之和,那么

第一头牛风险:a=sum-s1;第二头牛风险:b=sum+w1-s2;交换两头牛位置之后

a'=sum+w2-s1,b'=sum-s2,

由于是最优放置,所以w2-s1>=w1-s2,即w2+s2>=w1+s1,所以和最大的就该老实的在下面呆着= =!

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define N 50006
#define inf 1<<30
struct Node{
int w,s;
}cows[N];
int sum[N];
bool cmp(Node a,Node b){
return a.w+a.s<b.w+b.s;
}
int main()
{
int n;
while(scanf("%d",&n)==){
for(int i=;i<=n;i++){
scanf("%d%d",&cows[i].w,&cows[i].s);
}
sort(cows+,cows+n+,cmp);
int ans=-inf;
sum[]=;
for(int i=;i<=n;i++){
sum[i]=sum[i-]+cows[i].w;
ans=max(ans,sum[i-]-cows[i].s);
}
printf("%d\n",ans);
}
return ;
}

poj 3045 Cow Acrobats(二分搜索?)的更多相关文章

  1. POJ 3045 Cow Acrobats (贪心)

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

  2. POJ 3045 Cow Acrobats

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

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

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

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

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

  5. POJ3045 Cow Acrobats —— 思维证明

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

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

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

  7. 【POJ3045】Cow Acrobats(贪心)

    BUPT2017 wintertraining(16) #4 B POJ - 3045 题意 n(1 <= N <= 50,000) 个牛,重wi (1 <= W_i <= 1 ...

  8. POJ 3045

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

  9. BZOJ1629: [Usaco2007 Demo]Cow Acrobats

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

随机推荐

  1. python下载多个文件

    # -*- coding: utf-8 -*-__author__ = 'Administrator'import urllib2,urllib,os,redef Url1(url):#多个文件    ...

  2. std::remove_if

    原型: #include <algorithm>forward_iterator remove_if( forward_iterator start, forward_iterator e ...

  3. Linux目录结构及文件基础操作

    一.Linux目录结构 windows系统中主要以存储介质为主(磁盘). UNIX和Linux系统中主要是以目录为主(以树形目录结构的形式构建整个系统). 1. FHS(Filesystem Hier ...

  4. css_day6

  5. 学习iOS必须知道的[转载]

    part1 : http://www.cocoachina.com/ios/20150608/12052.html part2 : http://www.cocoachina.com/ios/2015 ...

  6. 使用RDS不得不知的注意事项

    使用RDS不得不知的注意事项 1.RDS实例升级需要注意的事项 RDS在进行实例升级的过程中会出现最长30秒左右的连接闪断,需要您提前做好准备,并设置好程序跟RDS的自动重连,避免因为升级的闪断导致您 ...

  7. 关于java中根据身份证求生日和年龄的问题

    /*这个也没什么大的功能,也没什么安全验证,只是对输入的身份证号码的长度进行了验证.其他的功能可以自己添加.*/import java.util.*; import java.util.Scanner ...

  8. oracle 数据库 分割字符串返回结果集函数

    CREATE OR REPLACE FUNCTION "UFN_SPLIT" (      p_list varchar2,      p_sep varchar2 := ',' ...

  9. Linux知识扫盲

    1.发现linux中好多软件以d结尾,d代表什么? d 代表 deamon 守护进程守护进程是运行在Linux服务器后台的一种服务程序.现在比较常用 是 service 这个词.它周期性地执行某种任务 ...

  10. HDU 4268 Alice and Bob set用法

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4268 贪心思想,用set实现平衡树,但是set有唯一性,所以要用 multiset AC代码: #i ...