Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. 
  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 (Σwj) 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  There’re several test cases. 
  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  For each test case, your program should output a single integer in a single line - the minimal PDV of the whole building. 
  If no floor would be damaged in a optimal configuration (that is, minimal PDV is non-positive) you should output 0. 
Sample Input

3
10 6
2 3
5 4
2
2 2
2 2
3
10 3
2 5
3 3

Sample Output

1
0
2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 100008
#define INF 1000000009
struct node
{
int w, s;
}a[MAXN];
int n;
bool cmp(node a, node b)
{
return a.w - b.s < b.w - a.s;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for (int i = ; i < n; i++)
scanf("%d%d", &a[i].w, &a[i].s);
sort(a, a + n, cmp);
LL sum = a[].w,ans = ;
for (int i = ; i < n; i++)
{
LL tmp = sum-a[i].s;
ans = max(ans, tmp);
sum += a[i].w;
}
printf("%lld\n", ans);
}
}

N - 贪心的更多相关文章

  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,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

  10. hdu 2037简单贪心--活动安排问题

    活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...

随机推荐

  1. [Swift通天遁地]三、手势与图表-(10)创建包含圆点、方形、三角形图标的散点图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. jdk1.8 api 下载

    链接: https://pan.baidu.com/s/1Wmf2vzXxclVcBPUfPp_g_A 提取码: dpwu 希望那些CSDN的不要借此収积分,行行好吧你,小众程序员就是为了方便 凑字数 ...

  3. [转]我是蒟蒻,但我有我的OI信仰

    我想最大的浪漫莫过于有人陪你征战OI吧 有多少无眠的夜晚?我总是在想, 到底是为了什么? 为了自招?为了省队?为了签约? 这条路很艰难,不可谓不凶险, 当你第一次踏上复试, 你肯定有看到过那些很厉害很 ...

  4. 【转】Linux下变量内容删除与替换

    转自:http://www.linuxidc.com/Linux/2015-01/111781.htm 当一个变量被赋予值后,有时会对变量的值进行一些微小的调整,比如删除变量值中特定一部份,或替换掉一 ...

  5. webHttpBinding+wsHttpBinding+basicHttpBinding的区别 (转)

    1. webHttpBinding (web AJAX/JSON)2. wsHttpBinding (ASP.NET client) 3. basicHttpBinding (Silverlight) ...

  6. java定时器和实时查询数据库

    定时器: Timer timer = new Timer();                    timer.schedule(new TimerTask() {                  ...

  7. 集合Set、List、Map的遍历方法

    package com.shellway.javase; import java.util.ArrayList; import java.util.Collection; import java.ut ...

  8. 在Yosemite中创建个人站点

    Yosemite变动很大,随之而来的就是一堆坑,之前在旧版OS中有效的方法在新版OS上已经不起作用了,创建个人站点就是一例. Mac OS内置Apache,安装目录在/etc/apache2/,etc ...

  9. CSS3利用box-shadow实现相框效果

    CSS3利用box-shadow实现相框效果 <style> html { overflow: hidden; background-color: #653845; background- ...

  10. 常见Android安装启动失败问题

    1.INSTALL_FAILED_VERSION_DOWNGRADE版本过低2.Failed to install Funm_AND.apk on device 'QWOJLVR8KNHYA6NR': ...