poj_3045 贪心
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7408 | Accepted: 2770 |
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
* 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.
#include <iostream>
#include <stdio.h>
#include <algorithm> using namespace std; struct dat
{
int W;
int S;
} data[]; bool cmp(dat a, dat 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",&data[i].W,&data[i].S);
} sort(data, data+n, cmp); long long max=-0x3f3f3f3f, temp;
long long sc=;
for(int i=n-; i>=; i--)
{
temp = sc-data[i].S;
if(temp>max)
max=temp;
sc+=data[i].W;
} printf("%lld\n", max);
}
return ;
}
poj_3045 贪心的更多相关文章
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- Firefox控制台日志转入文件
应该说这个需求并不常见-但有时候我的确想过,要是能知道Firefox此时在干吗就好了–有那么几次,该运行的脚本没有运行,状态条显示页面的加载并未完成,但你却永远等不到它. 意外地是,谷哥和度娘似乎并不 ...
- subprocess模块 sys模块
常用模块学习—subprocess模块详解 要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就是发起一个新进程,通过python调用 ...
- android SDK 安装
Android SDK在线更新镜像服务器 参见:http://www.androiddevtools.cn/ 1. 启动 Android SDK Manager ,打开主界面,依次选择『Too ...
- struct2depth 记录
把效果图放在前面 03.28 handle_motion False architecture simple joint_encoder False depth_normalization ...
- CFile、CStdioFile、FILE和其他文件操作(转+总结)
CFile.CStdioFile.FILE和其他文件操作(转+总结) 2010-04-10 20:36:33| 分类: VC++|举报|字号 订阅 下载LOFTER我的照片书 | ...
- dialog学习
11.dialog底部弹出动画: ==== 11.dialog底部弹出动画: 点击Button调用代码 private void show() { Dialog dialog = new Dialog ...
- [UE4]Event Tick
一.每一帧都会触发Event Tick事件,“Delta Seconds”参数表示当前帧说花费的时间 二.因为各种原因(比如卡帧等),每一帧所花费的时间并不是相同的
- LeetCode 791. Custom Sort String
题目链接:https://leetcode.com/problems/custom-sort-string/description/ S and T are strings composed of l ...
- WPF常用TriggerAction用法 (一)
Microsoft.Expression.Interactivity 常用TriggerAction-> CallMethodAction ChangePropertyAction Contro ...
- vue $refs 无法动态拼接,获取不到对象(转)
原文地址: http://www.php.cn/js-tutorial-410304.html 本篇文章给大家带来的内容是关于vue $refs中不使用拼接的原因以及解决方法,有一定的参考价值,有需要 ...