hdu4296 贪心
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
The public opinion is that Guanghua Building is nothing more than one of hundreds of modern skyscrapers recently built in Shanghai, and sadly, they are all wrong. Blue.Mary the great civil engineer had try a completely new evolutionary building method in project of Guanghua Building. That is, to build all the floors at first, then stack them up forming a complete building.
Believe it or not, he did it (in secret manner). Now you are face the same problem Blue.Mary once stuck in: Place floors in a good way.
Each floor has its own weight w i and strength s i. When floors are stacked up, each floor has PDV(Potential Damage Value) equal to (Σw j)-s i, where (Σw j) stands for sum of weight of all floors above.
Blue.Mary, the great civil engineer, would like to minimize PDV of the whole building, denoted as the largest PDV of all floors.
Now, it’s up to you to calculate this value.
Input
In each test case, in the first line is a single integer N (1 <= N <= 10 5) denoting the number of building’s floors. The following N lines specify the floors. Each of them contains two integers w i and s i (0 <= w i, s i <= 100000) separated by single spaces.
Please process until EOF (End Of File).
Output
If no floor would be damaged in a optimal configuration (that is, minimal PDV is non-positive) you should output 0.
Sample Input
10 6
2 3
5 4
2
2 2
2 2
3
10 3
2 5
3 3
Sample Output
交换两个板的位置
2)a'=sum+wj-si;b'=sum-sj;
如果1优于2,求解得有效的条件为wj-si>wi-sj
即wj+sj>wi+si
所以按si+wi的和排序贪心即可。
贪心好奇妙,感觉贪心虽然是人的天性,但是有的人会贪,有的人不会贪,
很多时候并没有什么道理可言,全靠个人直觉,但是得考虑得全面,我感觉
贪心策略的确定基本可以有两个想法,一个是全自己想,然后自己出数据,
不断的使自己的策略接近正确答案。二是可以先搞少的数据,两个或三个,
列公式确定贪心策略。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
struct nod
{
int w;
int s;
};
nod f[maxn];
bool cmp(nod a,nod b)
{
return a.s+a.w<b.s+b.w;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
__int64 ma=0,sum,t;
for(int i=0;i<n;i++)
scanf("%d%d",&f[i].w,&f[i].s);
sort(f,f+n,cmp);
sum=f[0].w;
for(int i=1;i<n;i++)
{
t=sum-f[i].s;
if(t>ma) ma=t;
sum+=f[i].w;
}
printf("%I64d\n",ma);
}
return 0;
}
hdu4296 贪心的更多相关文章
- HDU-4296 Buildings 贪心 从相邻元素的相对位置开始考虑
题目链接:https://cn.vjudge.net/problem/HDU-4296 题意 有很多板子,每一个板子有重量(w)和承重(s)能力 现规定一块板子的PDV值为其上所有板子的重量和减去这个 ...
- 【Python3】【贪心】hdu4296 Buildings
题意: n个板,每个板有重量和强度w和s,还有PDV值(上面的总重量-该板的强度) 对于某种叠放方式,PDV的最大值为其代表值 求该值的最小值 考虑只有两个板的情况:a和b,很显然下面的比上面的容 ...
- 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 堆纸牌 ...
随机推荐
- OpenCV——识别手写体数字
这个是树莓派上运行的, opencv3 opencv提供了一张手写数字图片给我们,如下图所示,可以作为识别手写数字的样本库. 0到9共十个数字,每个数字有五行,一行100个数字.首先要把这5000个数 ...
- Html5 学习之 Html5功能判断插件 Modernizr
---恢复内容开始--- Modernizr 浏览器对HTML5和CSS3开发的功能检测类库 由于当前用户使用的浏览器版本较多,对H5和CSS3的支持也各不相同.前端的开发者,在使用一些新的特性的时候 ...
- JavaScript进阶学习的一些建议
blankyao最近问我如何学习JavaScript,他觉着在理解了JavaScript的语法之后,不知如何去学习JavaScript了. 其实我也是个JavaScript小菜,最近在开发中遇到不少关 ...
- bug经验
1.异常信息丢失导致定位问题困难. 2.findbugs工具 3.在某些if语句判断中return,可能会导致文件句柄无法关闭. 4.lastmodified()在win下和linux下的处理是不同的 ...
- Ubuntu12.04下载Android4.0.1源码全过程,附若干问题解决[转]
学校里一直在做应用层开发,考虑到日后就业问题,这次决定研究源码和驱动,并进行编译.没想到就下载源码这一步折腾了我整整两天,期间遇到很多问题,哎,记录于此,希望日后再下源码的人不要再走无谓的弯路了.事实 ...
- 这样就算会了PHP么?-3
关于循环,IF,WHILE.... <?php $month = date("n"); $today = date("j"); if ($today &g ...
- Delphi 函数指针(函数可以当参数)
首先学习: 指向非对象(一般的)函数/过程的函数指针 Pascal 中的过程类型与C语言中的函数指针相似,为了统一说法,以下称函数指针.函数指针的声明只需要参数列表:如果是函数,再加个返回值.例如声明 ...
- Qt中文乱码问题(比较清楚,同一个二进制串被解释成不同的语言)
文章来源:http://blog.csdn.net/brave_heart_lxl/article/details/7186631 以下是dbzhang关于qt中文乱码问题原因的阐述,觉得不错: 首先 ...
- 关于老驱动不能在windows 8下正常安装的问题
问题: 老驱动(WDF),能在windows 7下安装并工作,但是不能在windows 8下安装. 先了解下windows 8驱动的新东西吧: New for windows 8 http://msd ...
- RedisTemplate
Spring Boot中Jedis几个api返回值的确认 @RequestMapping("/del/{key}") public String del(@PathVariable ...