N - 贪心
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 - 贪心的更多相关文章
- 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 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
- hdu 2037简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
随机推荐
- PCB genesis连孔加除毛刺孔(槽孔与槽孔)实现方法(三)
一.为什么 连孔加除毛刺孔 原因是 PCB板材中含有玻璃纤维, 毛刺产生位置在于2个孔相交位置,由于此处钻刀受力不均导致纤维切削不断形成毛刺 ,为了解决这个问题:在钻完2个连孔后,在相交处再钻一个孔, ...
- node.js的模块引用
1.模块的引用示例 var math = require(‘math’): 在common.js规范中,存在require()方法,这个方法接受模块标识,此引引入一个模块的api ...
- 慕课网3-10编程练习:简单的flex布局
小伙伴们,我们学习了伸缩容器的一些属性,接下来使用我们所学的伸缩容器属性完成下面的效果图. 完成效果: 任务 1.先将容器设置为伸缩容器 2.在垂直方向上对齐,行与行之间的空白距离一样 3.在水平方向 ...
- php从数据库读取中文显示问号??的解决办法
出错原因:1.数据库编码格式不对 2.PHP编码格式不对 3.浏览器编码格式不对 上面三者编码格式不统一,就会出现问题 数据库读取的时候在mysqli_connect()之后要设置连接字符编码mysq ...
- 涨知识---V
1.内存对齐 在解释内存对齐的作用前,先来看下内存对齐的规则: 1). 对于结构的各个成员,第一个成员位于偏移为0的位置,以后每个数据成员的偏移量必须是min(#pragma pack()指定的数, ...
- Hadoop Hive概念学习系列之hive里的用户定义函数UDF(十七)
Hive可以通过实现用户定义函数(User-Defined Functions,UDF)进行扩展(事实上,大多数Hive功能都是通过扩展UDF实现的).想要开发UDF程序,需要继承org.apache ...
- System.Data.SqlClient.SqlException: 在向服务器发送请求时发生传输级错误。 (provider: TCP 提供程序, error: 0 - 远程主机强迫关闭了一个现有的连接。) .
今天使用sql server 2008 R2管理器,进行SQL查询时,频率非常高的报错: System.Data.SqlClient.SqlException: 在向服务器发送请求时发生传输级错误. ...
- Xml的读取
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebAp ...
- Eclipse中自动生成get/set时携带注释给get/set
Eclipse中自动生成get/set时携带注释给get/set 编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclips ...
- day03-执行python方式、变量及数据类型简介
目录 执行Python程序的两种方式 1. 第一种:交互式 2. 第二种:命令式 3. Python执行程序的三个阶段 变量 变量 什么是变量 Python中的变量 变量名的命名规范 内存管理 定义变 ...