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 (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>力气s会有风险,求最小风险。
一开始想的是体重轻力气小的在上,交完发现WA了,最后猜测w和s相加排序,过了。
引用某大神的推导,证明:
设Di表示第i头奶牛的难受值,Wi表示第i头奶牛的体重,Si表示第i头奶牛的力量,令i,j相邻,且Wi+Si>Wj+Sj,设∑表示i和j上面的奶牛的重量之和

当i在j的上方时有

- Di=∑−Si


- Dj=∑+Wi−Sj


当j在i的上方时有

- Di=∑+Wj−Si


- Dj=∑−Sj


显然我们可以得到
③>①,②>④,②>③

这里面②最大,所以如果我们让i在j的上方最终答案一定不会更优,即证得此贪心策略的正确性。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct cow{
int w,p,sum;
};
int cmp(cow c1,cow c2)
{
return c1.sum<c2.sum;
}
cow c[50100];
int main()
{
int n;
//int w[10100],p[10100];
//memset(w,0,sizeof(w));
//memset(p,0,sizeof(p));
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&c[i].w,&c[i].p);
c[i].sum=c[i].p+c[i].w;
}
//int sum[10100];
sort(c,c+n,cmp);
int ans=-0x3f3f3f3f;
int sum=0;
for(int i=0;i<n;i++)
{
//ans+=c[i].p-c[i-1].w;
ans=max(ans,sum-c[i].p);
sum+=c[i].w;
}
printf("%d\n",ans);
}
return 0;
}

  

POJ-3045 Cow Acrobats (C++ 贪心)的更多相关文章

  1. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

  2. POJ - 3045 Cow Acrobats (二分,或者贪心)

    一开始是往二分上去想的,如果risk是x,题目要求则可以转化为一个不等式,Si + x >= sigma Wj ,j表示安排在i号牛上面的牛的编号. 如果考虑最下面的牛那么就可以写成 Si + ...

  3. poj 3045 Cow Acrobats(二分搜索?)

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  4. POJ 3045 Cow Acrobats

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  5. POJ 3045 Cow Acrobats (最大化最小值)

    题目链接:click here~~ [题目大意] 给你n头牛叠罗汉.每头都有自己的重量w和力量s,承受的风险数rank就是该牛上面全部牛的总重量减去该牛自身的力量,题目要求设计一个方案使得全部牛里面风 ...

  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. 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...

  9. BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...

随机推荐

  1. 分页(将数据库中的多条数据一页一页的显示在jsp页面中)

    一.显示数据库中的多条数据为什么要用分页 在真正的开发中,数据库中所存储的数据绝对不像我们平时所写的那样,仅仅有几条数据,而是有几十条甚至上百条,像淘宝京东的用户把都是上几十万甚至百万的.如果这时候在 ...

  2. Ubuntu 简单安装 Docker

    服务器版本 Ubuntu 16.04 LTS. 1. 普通安装 安装命令: 更新程序包索引,以及添加使用 HTTPS 传输的软件包以及 CA 证书. $ sudo apt-get update $ s ...

  3. ueditor单独调用上传附件和图片的功能

    javascript富文本编辑器使我们添加.编辑网站中的文章更加方便和容易.这些富文本编辑器提供了所见即所得(What You See Is What You Get - WYSIWYG)的功能,可以 ...

  4. 浏览器支持播放的视频播放格式要求(H5的video标签)

    今天给一个客户上传视频后发现,即使是MP4格式的视频浏览器也打不开,找了好久的问题,最红发现客户视频的编码方式不是h5支持的,折腾了好久,最终确认了浏览器对于MP4编码方式的如下: 浏览器对mp4的编 ...

  5. 【Ubuntu16】apt-get安装MariaDB

    一.Mysql背景信息 Mysql在互联网早期就流行了,追求速度.简单.坚持开源.几乎支持所有操作系统.完全支持多用户.多线程,支持海量数据存储,采用MyISAM.InnoDB两大存储引擎优势互补.但 ...

  6. 80C51学习 流水灯

    /* c语言常用预处理命令 1.#define使用 #define A PO 后面不用加分号. #define PI 3.14 2.循环左移右移函数 _crol_(a,b)循环左移函数,a是左移的值, ...

  7. Win10家庭版重命名Administrator用户文件夹

    需要将Windows系统默认的Administrator帐号文件夹改名为我们自定义的名称.. 但是Win10家庭版找不到组策略gpedit.msc 根据微软官方有关Win10各版本操作系统中对于组策略 ...

  8. 解说asp.net core MVC 过滤器的执行顺序

    asp.net core MVC 过滤器会在请求管道的各个阶段触发.同一阶段又可以注册多个范围的过滤器,例如Global范围,controller范围等.以ActionFilter为例,我们来看看过滤 ...

  9. 九天学会Java,第二天,算术运算

    算术运算 先回顾上次我们提到的编程特性 变量和数据类型,赋值和输出 算术运算 选择结构 循环结构 函数定义,函数调用 变量作用域 栈,程序运行的基石 面向对象 异常处理 语言提供的公用包 第一天我们讲 ...

  10. SoapUI中XML解析

    From http://www.robert-nemet.com/2011/11/groovy-xml-parsing-in-soapui.html Introduction Since soapUI ...