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. js 去除左右空格

    /*****************************************************Method1*************************************** ...

  2. navicat for mysql 注册码,简简单单,一个搞定(蔡军帅亲测有效)

    打开navicat for mysql接着打开帮助,选中注册, 把下面的复制上去就可以了 NAVH-WK6A-DMVK-DKW3 转载自:https://blog.csdn.net/qq_403845 ...

  3. 廖雪峰Java8JUnit单元测试-1JUnit简介-1JUnit测试

    测试驱动开发(Test Driver Development) 1.使用main()方法测试的缺点: 只能有1个main()方法,不能把测试代码分离 没有打印出测试结果和期望结果,例如expected ...

  4. 基于fastadmin快速搭建后台管理

    FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架:开发文档 下面对环境搭建简要概述,希望后来者能少走弯路: 1. 百度搜索最新版wampserver, 安装并启动 ...

  5. [UE4]AttachToComponent的AttachmentRule

    官方文档 KeepRelative 将当前相对转换保持为新父级的相对转换 KeepWorld 自动计算相对变换,使附着的组件保持相同的世界变换 SnapToTarget 捕捉转换到附着点

  6. Python绘图的颜色设置

    (转载):Python中的matplotlib函数中的颜色选取 转载链接:https://www.cnblogs.com/darkknightzh/p/6117528.html 原文内容:完全出自于自 ...

  7. msf help.

    root@Debian:~# msfconsole [-] ***rtiNg the Metasploit Framework console...- [-] * WARNING: No databa ...

  8. java利用反射交换两个对象中的字段相同的字段值

    有时候我们的两个对象字段都是一样的,只有极少的区别,想要把一个对象字段的值,赋值给另外一个对象值 然后传给另外一个方法使用,但是这个字段太多,一个一个的复制太过繁琐. 这时候利用反射解决这个问题. c ...

  9. 使用Git Bash上传代码到新的分支

    1.进入想要提交的项目,点击鼠标右键,选择"Git Bash Here" 2.输入命令,查看当前所有分支 git branch -a 3.输入命令,新建分支 git checkou ...

  10. Linux常用命令 笔记

     Linux常用命令  笔记 一.文件处理命令 1. ls命令:显示目录文件                          -a 显示所有文件,包括隐藏文件.(all)               ...