hdu4296 Buildings

来源:互联网 发布:pmi指数知乎 编辑:程序博客网 时间:2024/06/13 07:18

Buildings

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4465    Accepted Submission(s): 1638


Problem Description
  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 wi and strength si. When floors are stacked up, each floor has PDV(Potential Damage Value) equal to (Σwj)-si, 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 <= 105) denoting the number of building’s floors. The following N lines specify the floors. Each of them contains two integers wi and si (0 <= wi, si <= 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
310 62 35 422 22 2310 32 53 3
 

Sample Output
102

题目大意:一栋楼有n层,每层有两个值,w代表它自身的重量,s代表它的强度,把这些楼层整合到一起,会产生一个PDV的值

PDV: (Σwj)-si   i层楼上面的所有重量总和减去第i层楼的强度,求得最大的PDV的值最小

假设有两层楼 j, i;   第一种情况 j在i 的上面,PDV1= wj-si, 第二种情况 i 在j 的上面 PDV2= wi - sj;

因为要使j在i的上面的PDV1的值最小,所以令 wj - si < wi - sj;  得 wj + sj < wi + si  ,按照每层自身的重量与它的强度之和从小到大排序

#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;struct node{int w, s;int sum;}a[100005];int cmp(node x, node y){return x.sum < y.sum;}int main(){int n;while(scanf("%d", &n) != EOF){for(int i = 0; i < n; i++){scanf("%d %d", &a[i].w, &a[i].s);a[i].sum = a[i].w + a[i].s;}sort(a, a+n, cmp);long long pdv = 0, ans = a[0].w, Max = -0x3f3f3f3f;for(int i = 1; i < n; i++){pdv = ans-a[i].s;ans += a[i].w;if(Max < pdv){Max = pdv;}}if(Max < 0){printf("0\n");}else{printf("%lld\n", Max);}}return 0;} 






原创粉丝点击