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. poj 3744 Scout YYF I (矩阵)

    Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...

  2. python lcd 时间显示

    #!/usr/bin/python # QStopWatch -- a very simple stop watch # Copyright (C) 2006 Dominic Battre <d ...

  3. hdu1015(Safecracker )

    Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...

  4. read(),write() 读/写文件

    read read()是一个系统调用函数.用来从一个文件中,读取指定长度的数据到 buf 中. 使用read()时需要包含的头文件: <unistd.h> 函数原型: ssize_t re ...

  5. 使用Express创建一个简单的示例

    1.安装Express 使用npm包安装工具来安装Express安装包,打开npm命令行,输入: npm install -g express 2.创建一个工程 本示例是在windows下创建的,项目 ...

  6. C# XML,XmlDocument简单操作实例

    private static string _Store = LocalPathHelper.CurrentSolutionPath + "/data/bookstore.xml" ...

  7. 提高jQuery执行效率

    1. 使用最新版本的jQuery jQuery的版本更新很快,你应该总是使用最新的版本.因为新版本会改进性能,还有很多新功能. 下面就来看看,不同版本的jQuery性能差异有多大.这里是三条最常见的j ...

  8. CODEVS 1062 路由选择

    1062 路由选择  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 在网络通信中,经常需要求最短路径.但完全用最短路径传 ...

  9. 遍历String字符串,得到出现次数最多的字母

    //There is no need to explain the code right? package com.hp.test; import java.util.HashMap; import ...

  10. 网页title标题的闪动效果

    通过网页title来提示用户有新消息这个功能很常见,比如现在的微博,还有一些邮箱,这个功能都很常见. 显示信息数: <input type="text" id="t ...