文章标题UVALive 6424:Russian Dolls(贪心)

本文介绍了一种基于俄罗斯套娃的算法问题,该问题旨在通过合理安排不同大小的套娃来最小化总体成本。每只套娃都有特定的外部体积、内部体积及单位体积的成本,目标是最小化所有未被占据的内部空间的总成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值