zoukankan      html  css  js  c++  java
  • hdu 4296 贪心

    Buildings

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


    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
     
     
    题目大意是给出具有两个属性的物体,计算出如何堆放使得最大的PDV值越小,,,这个贪心不看题解,,真的想不到啊。。。
    感觉贪心有点数学证明的意思,,蛤
    我们不妨设有两个相邻的物体 a(wi,si),b(wj,sj),假设a在方案一中这个位置上方总w为SUM.显然只有两种情况 a上b下或者a下b上,我们就这个方案讨论最优解,
    对于方案1,PDV(a)=SUM-si,PDV(b)=SUM+wi-sj;
    对于方案二,PDV(b)=SUM-sj,PDV(a)=SUM+wj-si;
    如果想要方案一更优的话,则要按照方案一的方式排序,则有PDV(b)<PDV(a)-->wi-sj<wj-si-->wi+si<wj+sj,即总和小的在上面。
    选方案二同理。
    #include<bits/stdc++.h>
    using namespace std;
    #define LL long long
    struct node
    {
        LL w,s;
        bool operator<(const node &chs)const{
        return w+s<chs.w+chs.s;
        }
    }P[100005];
    int main()
    {
        int n,m,i,j,k;
        while(cin>>n){
            for(i=0;i<n;++i) scanf("%lld%lld",&P[i].w,&P[i].s);
            sort(P,P+n);
            LL sum=P[0].w,ans=0;
            for(i=1;i<n;sum+=P[i++].w){
               ans=max(ans,sum-P[i].s);
            }
            if(ans<0) ans=0;
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Java第9次作业--接口及接口回调
    Java第8次作业--继承
    软件工程第三次作业——关于软件质量保障初探
    Java第7次作业--访问权限、对象使用
    Java第6次作业--static关键字、对象
    Java第5次作业--对象的创建与使用
    20194629 自动生成四则运算题第一版报告
    软件工程第一次作业
    今天开通博客啦!
    1170. Compare Strings by Frequency of the Smallest Character
  • 原文地址:https://www.cnblogs.com/zzqc/p/6930680.html
Copyright © 2011-2022 走看看