6424 Russian Dolls--贪心

来源:互联网 发布:买家怎么加入淘宝联盟 编辑:程序博客网 时间:2024/06/05 13:31
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 — costiis 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-thof 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

这是一道贪心的题:


题意:给你n个箱子,包含他们的外体积outi,内体积ini,还有单位空余内体积的花费costi,当箱子a的内体积大于另一个b箱子的外体积的时候,箱子b可以放在箱子a里面,并且箱子只能嵌套放置,不能平行放置;问求出放置完成后空余内体积的最小总花费;


思路:因为每次放置时公式为 cost[i]*(in[i]-out[x])         将公式展开就为

cost[i]*in[i]-cost[i]*out[x];

由此可见前面的式子是一定的,我们只需要求出后面式子的最小值就可以了

这里我们用到了贪心的方法

首先我们先给式子按 花费为第一条件  内径为第二条件排序,由于n<1000所以可以直接暴力搜索找出答案;

注意这里一定要标记cost 和out  !!!!!!!!!!!我就是死在out上

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;#define inff -0x3fffffff#define mx 1009struct node{    int in,ou,cost;}a[mx];int cmp(node x,node y){   if(x.cost==y.cost)    return x.in>y.in;   return x.cost>y.cost;}int main(){    int i,j;    int n;    int sum;    int flag1[mx],flag2[mx];    while(~scanf("%d",&n))    {        sum=0;        memset(flag1,0,sizeof(flag1));//标记cost        memset(flag2,0,sizeof(flag2));//标记out        for(i=0;i<n;i++)        {           scanf("%d %d %d",&a[i].ou,&a[i].in,&a[i].cost);           sum+=a[i].in*a[i].cost;        }        sort(a,a+n,cmp);        int ff=inff;        int dian;        for(i=0;i<n;i++)        {            ff=inff;            dian=inff;            for(j=0;j<n;j++)            {               if(i!=j)               {                   if(a[i].in>a[j].ou&&flag1[i]==0&&flag2[j]==0)                   {                      if(ff<a[i].cost*a[j].ou)                      {                           ff=a[i].cost*a[j].ou;                           dian=j;                      }                   }               }            }            if(ff!=inff)            {                flag2[dian]=1;                flag1[i]=1;                sum-=ff;            }            ff=inff;            dian=inff;        }       printf("%d\n",sum);    }}






0 0