N - 贪心
2024-10-21 07:55:13
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.
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);
}
}
最新文章
- poj-3264-Balanced Lineup
- STM32学习笔记——点亮LED
- c#跨线程访问控件帮助类
- Grunt 构建SeaJS
- Java添加事件的四种方式
- SQL Server 2012 读写分离设置
- (转)SVN源码管理(上&;下)
- 第四章 Linux环境
- 《Lua游戏开发实践指南》读后感
- [ACM] hdu 1671 Phone List (特里)
- 自定义view(二)
- JS弹窗带遮蔽的功能
- WPF:Webbrowser 捕获关闭事件
- JSON跨域解决方案收集
- Eigen库和STL容器冲突问题
- 如何获取Input标签自定义属性的值?
- linux 下apache 停止、重启等操作
- 【HDU4405】Aeroplane chess [期望DP]
- uiwebview 屏幕自适应 -- 根据 内容适应或者 webview适应
- 解读人:朱月琴,Hippocampal proteomic alteration in triple transgenic mouse model of Alzheimer’s disease and implication of PINK 1 regulation in donepezil treatment
热门文章
- Ansi2Utf8 小工具
- 【BZOJ1483】[HNOI2009]梦幻布丁(平衡树启发式合并+并查集)
- webHttpBinding+wsHttpBinding+basicHttpBinding的区别 (转)
- 【GAN学习笔记】对抗式生成网络入门
- 新浪云虚拟机ftp链接显示失败问题
- Java 6中类路径ClassPath对通配符的支持
- JS——鼠标跟随
- [Windows Server 2008] 安装Apache+PHP+MySQL
- [Windows Server 2012] 安装SQL Server 2012
- Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage