【贪心】 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
#include
#include
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

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

你可能感兴趣的:(ACM,贪心)