当前位置:编程学习 > C/C++ >>

HDU 4296 Buildings (YY)

题意: 给定N个物体,每个物体有两个参数w,s。   w代表它自身的重量; s代表它的强度。现在要把这些物体叠在一起,会产生一个PDV值。
PDV解释:(Σwj)-si, where (Σwj) stands for sum of weight of all floors above.即为在i物体上方所有物体的重量和 - i的强度。
现在希望最大的PDV值最小....................
 
YY: 假设两个物体i,j,把谁放上面比较好? 假设把i放上面,则pdv1 = Wi - Sj;把j放上面 则pdv2 = Wj - Si;要使得pdv尽量小,设pdv1 < pdv2 则 Wi + Si < Wj + Sj
 
所以按照w+s由小到大sort,就能满足条件了......
 
#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include<functional>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <string>  
#include <vector>  
#include <set>  
#include <queue>  
#include <stack>  
#include <climits>//形如INT_MAX一类的  
#define MAX 100005  
#define INF 0x7FFFFFFF  
using namespace std;  
  
struct node {  
    int w,s;  
}a[MAX];  
int n;  
  
bool cmp(const node &x, const node &y) {  
    return x.w + x.s < y.w + y.s;  
}  
int main() {  
    while(scanf("%d",&n) != EOF) {  
        for(int i=0; i<n; i++) {  
            scanf("%d%d",&a[i].w,&a[i].s);  
        }  
        sort(a,a+n,cmp);  
        __int64 sum = 0;  
        __int64 pdv = -INF;  
        for(int i=1; i<n; i++) {  
            sum += a[i-1].w;  
            pdv = max(pdv,sum - a[i].s);  
        }  
        if(pdv < 0) printf("0\n");  
        else printf("%I64d\n",pdv);  
    }  
    return 0;  
}  

 


补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,