HDOJ 4296 Buildings

这是2012亚洲区域网络预选赛成都赛区的一道贪心题。

Buildings

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

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

 这是成都网络预选赛的一道题,很多人都是试着过的,没有给出证明。当时我们都不敢试,囧啊!
事实证明:撑死胆大的,饿死胆小的。贪心的题目有时候就靠直觉加运气,大胆的去试吧!
按wi+si排序即可,最下面的木板w+s最大,它的PDV就是所有的木板的w相加之和减去最下面木板的(w+s)。也是所求值。
#include<stdio.h>
typedef long long LL;
int main()
{
    int n;
    LL s,w,sum,max;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        max=0;
        while(n--)
        {
            scanf("%I64d%I64d",&w,&s);
            if(w+s>max)max=s+w;
            sum+=w;
        }
        sum-=max;
        printf("%I64d\n",sum);
    }
    return 0;
}

本题虽然都AC了,但很多人都是蒙出来的,不是做出来的。我想了很长时间,才想到证明:

对于相邻放置的两块板,设两块板为i,j他们上面的重量为sum

           1) PDV_a=sum-si;PDV_b=sum+wi-sj;

           交换两个板的位置

          2)PDV_a'=sum+wj-si;PDV_b'=sum-sj;

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

          所以按si+wi的和排序贪心即可,这样最下面的木板就一定是PDV最大的木板。

那他是不是所求值呢?证法:

任意木板放在最下面的PDV一定是SUM(w1+w2+.....wn)-(si+wi),这个要最小,只能是si+wi最大。

综合以上两方面,得证。

以后一定要锻炼自己的思维能力,大牛们应该是当场就想出证明了才会一A。真牛啊!

原文地址:https://www.cnblogs.com/liuyalunuli/p/2702074.html