POJ --3045--Cow Acrobats(贪心模拟)
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
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
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
Output
Sample Input
3
10 3
2 5
3 3
Sample Output
2
Hint
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
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
struct node
{
LL w,s;
}cow[100100];
int cmp(node s1,node s2)
{
return s1.s+s1.w>s2.s+s2.w;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
LL temp=0,ans=-INF;
for(int i=0;i<n;i++)
scanf("%d%d",&cow[i].w,&cow[i].s);
sort(cow,cow+n,cmp);
if(n==1) ans=-cow[0].s;
else
{
for(int i=n-1;i>=0;i--)
{
if(temp-cow[i].s>ans)
ans=temp-cow[i].s;
temp+=cow[i].w;
}
}
printf("%lld\n",ans);
}
return 0;
}
POJ --3045--Cow Acrobats(贪心模拟)的更多相关文章
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- POJ - 3045 Cow Acrobats (二分,或者贪心)
一开始是往二分上去想的,如果risk是x,题目要求则可以转化为一个不等式,Si + x >= sigma Wj ,j表示安排在i号牛上面的牛的编号. 如果考虑最下面的牛那么就可以写成 Si + ...
- poj 3045 Cow Acrobats(二分搜索?)
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- POJ 3045 Cow Acrobats
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- POJ 3045 Cow Acrobats (最大化最小值)
题目链接:click here~~ [题目大意] 给你n头牛叠罗汉.每头都有自己的重量w和力量s,承受的风险数rank就是该牛上面全部牛的总重量减去该牛自身的力量,题目要求设计一个方案使得全部牛里面风 ...
- Cow Acrobats(贪心)
Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3686 Accepted: 1428 Desc ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- [USACO2005][POJ3045]Cow Acrobats(贪心)
题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风 ...
- POJ:1017-Packets(贪心+模拟,神烦)
传送门:http://poj.org/problem?id=1017 Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- poj 3045 叠罗汉问题 贪心算法
题意:将n头牛叠起来,每头牛的力气 s体重 w 倒下的风险是身上的牛的体重的和减去s 求最稳的罗汉倒下去风险的最大值 思路: 将s+w最大的放在下面,从上往下看 解决问题的代码: #include& ...
随机推荐
- urlrewrite地址重写的使用
地址重写: 主要是为了站点的安全. 比如我们平时的地址请求 地址重写前,訪问路径是: /read.egov?action=read&bid=2 地址重写后,訪问路径是:/read-read-2 ...
- iOS 常见小问题
1. iOS 编译后上下有黑边 ? 缺少启动图片 2.Failed to instantiate the default view controller for UIMainStoryboardFil ...
- 编程与算法中的端点问题(linspace(a, b, n),endpoint)
左闭右开,[0, n) ⇒ [0, n-1],共 n 个元素: 1. 列表长与编号 列表(seq,也可以是数组等线性结构)的长度要比末尾元素的编号多 1,比如一个列表内含有 5 个元素,最后一个元素的 ...
- 15-11-24 system同步与异步
//打开关闭qq #define _CRT_SECURE_N0_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #i ...
- ServiceStack.Redis之IRedisClient<第三篇>【转】
事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...
- 亿财道APP赚钱攻略,亿财道,一个看广告年入36万的APP
亿财道(http://etway.net/),一款看广告(传单)赚钱的软件,这是一项革新的广告产品,代替了以往的纸质传单.在商家节约成本的同时,还给阅读者佣金,推广也有相应提成比例. ...
- layui(弹出层)
首先引入文件 layui.css jquery.min.js layui.js 弹出层 data-method 后面的属性控制是什么弹窗,在js中写方法 <div class="sit ...
- e.Row.Attributes.Add
其实看到属性这个单词,还有点发憷呢,C#里面有个关键词是Attributes, 搞了半天貌似没有弄清楚 e.Row.Attributes.Add()函数的介绍,包括参数,什么是Attributes 就 ...
- QT笔记 -- (2) 文件相关操作、中文路径乱码
1.显示文件对话框,选择一个目录,显示选中目录中的所有图片的代码如下 主要class: QFileDialog QStringList QFileInfoList QDir void open(){ ...
- springmvc整合mybatis实现商品列表查询
转载.https://blog.csdn.net/chizhuyuyu/article/details/82180404 https://www.jianshu.com/p/689bdd11bfcc. ...