CodeForces #589 B Layer Cake 类似01背包的模拟题

来源:互联网 发布:ebay销售数据采集 编辑:程序博客网 时间:2024/06/13 10:37

题目描述:

Description
Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n rectangular cake layers. The length and the width of the i-th cake layer were ai and bi respectively, while the height of each cake layer was equal to one.

From a cooking book Dasha learned that a cake must have a form of a rectangular parallelepiped constructed from cake layers of the same sizes.

Dasha decided to bake the biggest possible cake from the bought cake layers (possibly, using only some of them). It means that she wants the volume of the cake to be as big as possible. To reach this goal, Dasha can cut rectangular pieces out of the bought cake layers. She always cuts cake layers in such a way that cutting lines are parallel to the edges of that cake layer. Dasha isn’t very good at geometry, so after cutting out a piece from the original cake layer, she throws away the remaining part of it. Also she can rotate a cake layer in the horizontal plane (swap its width and length).

Dasha wants her cake to be constructed as a stack of cake layers of the same sizes. Each layer of the resulting cake should be made out of only one cake layer (the original one or cut out from the original cake layer).

Help Dasha to calculate the maximum possible volume of the cake she can bake using given cake layers.

Input
The first line contains an integer n(1 ≤ n ≤ 4000) — the number of cake layers that Dasha can use.

Each of the following n lines contains two integer numbers ai and bi(1 ≤ ai, bi ≤ 106) — the length and the width of i-th cake layer respectively.

Output
The first line of the output should contain the maximum volume of cake that can be baked using given layers.

The second line of the output should contain the length and the width of the resulting cake. If there are many solutions with maximum possible volume, print any of them.

Sample Input
Input

55 121 14 66 44 6

Output

966 4

Input

2100001 900000900001 100000

Output

180000000000900000 100000

Hint
In the first example Dasha doesn’t use the second cake layer. She cuts 4 × 6 rectangle from the first cake layer and she uses other cake layers as is.

In the second example Dasha cuts off slightly from the both cake layers.

题目分析:

有n个长宽为ai和bi的矩形蛋糕,高被视为1,将这些蛋糕堆成一个大蛋糕,使得其长宽一致,(将大蛋糕裁成小蛋糕,不能将小蛋糕拼成大蛋糕),高为这些蛋糕数量。求这个大蛋糕的体积,长和宽最大是多少。
首先,将这些蛋糕降序排序,并且在每个蛋糕中,满足长>=宽。用dp数组dp[i][j]表示在以i为开始,堆了j个蛋糕时,最大的长度和宽度。类似01背包的思想。

代码如下:

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;const int MAXN=4040;typedef long long ll;struct Cake{    ll a,b;}cake[MAXN];bool cmp(Cake x, Cake y){    return x.a>y.a;}ll dp[MAXN][MAXN];//记录叠第i层最大的宽度ll tmp[MAXN];int n;int main(){    while(scanf("%d",&n)!=-1)    {        for(int i=1; i<=n; i++)        {            scanf("%I64d%I64d",&cake[i].a,&cake[i].b);            if (cake[i].a>cake[i].b) swap(cake[i].a,cake[i].b);//长大于等于宽        }        sort(cake+1,cake+n+1,cmp);        ll ans=0,length=0,width=0;        memset(dp,0,sizeof(dp));        memset(tmp,0,sizeof(tmp));        for(int i=1; i<=n; i++)//从小到大遍历每一个蛋糕        {            for(int j=i; j>0; j--)//多少层            {                if (tmp[j-1]) dp[i][j]=min(tmp[j-1],cake[i].b);//是否将宽度更新                else dp[i][j]=cake[i].b;//i为第一层情况                tmp[j]=max(tmp[j],dp[i][j]);                if (dp[i][j]*cake[i].a*j>=ans)//更新答案                {                    ans=dp[i][j]*cake[i].a*j;                    length=cake[i].a;                    width=dp[i][j];                }            }        }        printf("%I64d\n%I64d %I64d\n",ans,length,width);    }    return 0;}
0 0
原创粉丝点击