UVALive 6424 Russian Dolls

来源:互联网 发布:淘宝上传宝贝图片大小 编辑:程序博客网 时间:2024/06/08 22:11
6424 Russian Dolls
Maybe you know the famous russian souvenir Russian Dolls. It looks like a set of nested wooden dolls. A doll with a smaller size is placed inside a bigger one. Let’s consider all dolls taken apart. Each doll has an outer volume outi which is the volume it occupies in space and an inner volume ini — the volume of the empty space inside the doll. You may assume that you can put one doll inside another if the outer volume of the first doll is strictly less than the inner volume of the second one. If two or more dolls are inside another one they can’t lie one near the other, they must be nested. For each doll the cost of unit of empty space — costi is known. You must pay exactly costi for each unit of empty space which directly belongs to the i-th doll (but not to ones inside it). You may arrange the dolls the way you want as long as you are not contradicting the rules. The objective is to find an arrangement of nesting the dolls (not necessarily all of them) such that the overall cost you have to pay is minimized.
Input
The input file contains several test cases, each of them as described below. First line contains an integer N (1 ≤ N ≤ 1000) which is the number of dolls you have. The i-th of the next N lines contains three integers outi, ini, costi (1 ≤ ini < outi ≤ 1000, 1 ≤ costi ≤ 1000), which are the outer volume, inner volume and the empty space cost of the i-th doll.
Sample Output
For each test case, write to the output a single integer P which is the minimum possible cost you should pay on a line by itself.
Sample Input

5 4 1 

4 2 2 

3 2 1

Sample Output

7


一开始没有想到是贪心,还以为是动规,没仔细想清楚Orz

每个套娃有一个外体积和一个内体积还有一个单位空闲空间的花费

嵌套可以减小花费,故按花费降序排列,内含可容下的最大体积的套娃

每个内含的套娃标记为用过


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct Doll{int out, in, cost;bool operator < (const Doll &A)const{return cost > A.cost;}//降序}d[1005];int n,sum;bool used[1010];int main(){while(cin>>n)    {sum = 0;        memset(used,0,sizeof(used));for(int i=0; i<n; i++){cin>>d[i].out>>d[i].in>>d[i].cost;sum += d[i].cost * d[i].in;used[i] = 0;}sort(d, d+n);int x;for(int i=0; i<n; i++){x = -1;for(int j=0; j<n; j++){if(!used[j] && d[j].out<d[i].in){if(x==-1 || d[j].out>d[x].out)    x=j;}}if(x!=-1){sum -= d[x].out * d[i].cost;used[x] = 1;}}cout<<sum<<endl;}return 0;}


0 0
原创粉丝点击