hdu4296 贪心

Buildings

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

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
3
10 6
2 3
5 4
2
2 2
2 2
3
10 3
2 5
3 3
 
Sample Output
1
0
2
 
Source
题意:有N层地板, 每层都有一个自己的重量  wi 和强度 si, 每层都有一个潜在的伤害值, 第i层的伤害值等于(Σwj )-si,(0<j<i)。问:使的每层的伤害值中的最大值最小。
思路:
看到这个最大值最小,就想到二分,不过这道题不是二分~~感觉贪心贪的挺好玩的~~
证明过程是(参照东哥的证明):
对于相邻放置的两块板,设两块板为i,j他们上面的重量为sum

1) a=sum-si;b=sum+wi-sj;

 交换两个板的位置

2)a'=sum+wj-si;b'=sum-sj;

如果1优于2,求解得有效的条件为wj-si>wi-sj

即wj+sj>wi+si

所以按si+wi的和排序贪心即可。

View Code
 1 #include <math.h>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 #define LL long long
 6 using namespace std;
 7 struct node{
 8     LL a,b;
 9 }p[100010];
10 int cmp(node u,node v){
11     return u.a+u.b<v.a+v.b;
12 }
13 int main(){
14     int n;
15     while(scanf("%d",&n)!=EOF){
16         for(int i=0;i<n;i++)
17           scanf("%I64d%I64d",&p[i].a,&p[i].b);
18         sort(p,p+n,cmp);
19         LL res=0,_max=0;
20         for(int i=0;i<n;i++){
21             _max=max(_max,res-p[i].b);
22             res+=p[i].a;
23         }
24         printf("%I64d\n",_max);
25     }
26 }

 也可以不排序,思路是一样的,代码是别人的:

View Code
 1 #include<iostream>
 2 using namespace std;
 3 long long sum,lmax,swmax;   
 4 int main()
 5 {
 6     long long N,s,w;
 7     int i;
 8     while(cin>>N)
 9     {
10         sum=0;
11         lmax=0;
12         swmax=0;
13         for(i=0;i<N;i++)
14         {
15             cin>>w>>s;
16             if(w+s>swmax)swmax=s+w;
17             sum+=w;
18         } 
19         sum-=swmax;
20         if(sum>lmax)lmax=sum;
21         cout<<lmax<<endl;                               
22     }
23     return 0;
24 }        

 

posted @ 2012-09-17 16:38  _sunshine  阅读(1251)  评论(0编辑  收藏  举报