【贪心】 hdu4296 Buildings

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4296

题意:有n个板,每个板有重量和强度两个属性,把板叠在一起,对于每个板有个PDV值,计算方式为这个板上面的板的重量和减去这个板的强度,对于每种叠放方式,取这个叠放方式中所以板中PDV值最大的值为代表值,问所有叠放方式中最小的代表值为多少。

题解:对于相邻放置的两块板,设两块板为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。

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

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
struct point
{
    int w,s;
}node[100005];
int n;
bool cmp(const point &a,const point &b)
{
    return a.w+a.s<b.w+b.s;
}
int main()
{
    for(;~scanf("%d",&n);)
    {
        for(int i=0;i<n;++i)
            scanf("%d%d",&node[i].w,&node[i].s);
        sort(node,node+n,cmp);
        LL summ=0,maxx=0;
        for(int i=0;i<n;++i)
        {
            maxx=max(maxx,summ-node[i].s);
            summ+=node[i].w;
        }
        printf("%I64d\n",maxx);
    }
    return 0;
}

来源: http://blog.csdn.net/acm_ted/article/details/7984935

你可能感兴趣的:(【贪心】 hdu4296 Buildings)