Cow Acrobats
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

* 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.


大体思路是: 每只牛的风险 = 他上面牛的体重和 - 他自己的力量
     也就是:   每只牛的风险 = 他的体重加上面的中体重  -  自己体重  -  他自己的力量
所以应当按照(W_i + S_i)排序,  值越大应该位于底层。
 
还要注意最上面那一只牛,也因该有一个值   就是   -S
我在这里wa了好久。

#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 贪心的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. SQL SERVER2008 数据库日志文件的收缩方法

    最近公司的数据库随着业务量的增多,日志文件巨大(超过300G),造成磁盘空间不够用,进而后来的访问数据库请求无法访问. 网上类似的方法也很多,但不可行,如下是我实践过,可行的,将日志文件收缩至任意指定 ...

  2. 酒店web认证802.11x+ROS共享NAT上网

    网络环境: 维也纳酒店-上网方式 web认证上网(wifi有线) 设备-ROS RB952Ui-5AC2ND一台笔记本电脑一台 web认证的三个特征,我们下面ROS的设置就是为了满足下面三个条件来分布 ...

  3. DataGridView导出数据到Excel

    //传入DataGridView /// <summary> /// 输出数据到Excel /// </summary> /// <param name="da ...

  4. 【原创】Open JDK更换过程及更换后的问题总结与分析

    由于2019年1月起Oracle对通用计算以外的应用场景开始收费,综合看来还是主要针对嵌入式的Java应用进行收费,毕竟嵌入式设备的数量是庞大的,可以有数亿元进账. 因Oracle JDK收费,各大公 ...

  5. python学习笔记之五

    1.Python HTTP server win环境需要加cgi参数 python -m http.server --cgi 8000 其他如下: python -m http.server 8080 ...

  6. webapi xml序列化删除<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">标签

    在WebApiConfig.Register中加入config.Formatters.Remove(config.Formatters.XmlFormatter); 移除默认的xml序列化器

  7. 最大化等比例测试演化Demo-传统方法

    demo-1: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...

  8. 走进HashMap

    1.手写实现HashMap 2.解析代码并阐述HashMap1.7到1.8的变化 3.HashMap的遍历方式 4.HashMap与HashSet,Hashtable,ConcurrentHashMa ...

  9. 四、Python-元组

    列表非常适合用于存储在程序运行期间可能变化的数据集.列表是可以修改的,这对处理网 站的用户列表或游戏中的角色列表至关重要. Python将不能修改的值称为不可变的,而不可变的列表被称为元组 一.元组( ...

  10. linux 文件描述符

    文件描述符是什么?和文件句柄有啥区别?文件描述符是linux/unix操作系统中特有的概念.相当于windows系统中的文件句柄.一个意思不同叫法.Linux系统中, 每当进程打开一个文件时,系统就为 ...