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
3
5 4 1
4 2 2
3 2 1
Sample Output
7
题意:俄罗斯套娃,每一个娃娃有外部体积,内部体积和单位体积的花费。如果一个娃娃的内部体积大于另一个的外部体积,这个娃娃就能嵌套另一个娃娃。现在要求的是每个娃娃剩余的内部体积所用的花费的总和最小。
分析:一开始,所有的娃娃没有嵌套娃娃的时候总花费为sum=(cost【i】in【i】);所以当娃娃有嵌套娃娃花费就会减小因为其内部体积减小了。每个的花费cost【i】(in【i】- out【j】)(i为第in【i】为第i个娃娃的内部体积,out【j】为嵌套在第i个娃娃的外部体积),而cost【i】*cost【j】一定,所以想花费更少,cost【i】*out【j】越大。所以先按cost第一关键排序,这样,然后再找out【j】最大的。期间,用flag数组标记那个娃娃已经被嵌套了。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int n,cnt;
struct node {
int outt,inn;
int cost;
int leave;
bool operator <(const node &t)const{
return (cost>t.cost);
}
};
node a[1005];
int flag[1005];
int main ()
{
while (scanf ("%d",&n)!=EOF){
memset (flag,0,sizeof (flag));
for (int i=0;i<n;i++){
scanf ("%d%d%d",&a[i].outt,&a[i].inn,&a[i].cost);
a[i].leave=a[i].inn;
}
sort (a,a+n);
flag[0]=0;
int ans=0;
for (int i=0;i<n;i++){
int place=-1;
int sizee=0;
for (int j=0;j<n;j++){
if (flag[j]==0&&a[j].outt>sizee&&a[j].outt<a[i].inn){
place=j;
sizee=a[j].outt;
}
}
if (place!=-1){
flag[place]=1;
a[i].leave = a[i].inn-sizee;
}
}
for (int i=0;i<n;i++){
ans+=a[i].leave * a[i].cost ;
}
printf ("%d\n",ans);
}
return 0;
}