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简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
随机推荐
- SRAM and DRAM
DRAM:(Dynamic Random Access Memory)动态随机访问存储器,只能将数据保存较短的时间.每隔一段时间需要对数据进行刷新一次,没有刷新的单元数据会丢失.常见的用途是作为内存( ...
- [Swift通天遁地]四、网络和线程-(12)使用ReachabilitySwift实现对网络状态的检测
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- ajax 以json 的形式来传递返回参数的实例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestWcf.aspx.c ...
- python自动化测试学习笔记-4常用模块
常用模块 1.os 2.sys 3.random 4.string 5.time 6.hashlib 一.os模块 os模块主要用来操作文件.目录,与操作系统无关.要使用os模块首先要导入OS模块,用 ...
- BZOJ 3625 多项式求逆+多项式开根
思路: RT //By SiriusRen #include <bits/stdc++.h> using namespace std; <<,mod=; int A[N],C[ ...
- 【洛谷2926/BZOJ1607】[USACO08DEC]Patting Heads拍头(筛法)
题目: 洛谷2926 (截止至本博客发表时,BZOJ1607题面有误,正确题面请到洛谷2926查看) 分析: = 一句话题意:给定\(n\)个数\(\{a_i\}\),求对于每个\(a_i\)有多少个 ...
- ACM_变形课(并查集)
变形课 Time Limit: 2000/1000ms (Java/Others) Problem Description: 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermio ...
- 使用yum命令更新时锁住了怎么办?
出现的状况如下: [root@iZwz951sp834mvbed8gdzzZ ~]# yum update kernelLoaded plugins: fastestmirrorExisting lo ...
- html5——多媒体(三)
自定义进度条 1.video标签是内联块,可以设置宽高,但是需要用大盒子将其包裹起来进行定位 2.小盒子设计成进度条样式,并进行定位 3.进度条样式中的特殊按钮可以用web字体 4.通过点击实现视频的 ...
- windows phone 8 使用页面传对象的方式 实现页面间的多值传递
在做windows phone 开发的时候,会经常碰到页面间之间的跳转和传递数据,如果传递的值不多,只有两三个,我们通常使用NavigationService.Navigate(new Uri(&qu ...