ACM_codeforces_589B_Layer Cake_sorting

B. Layer Cake

time limit per test6 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
题目
   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.
Examples
input
5
5 12
1 1
4 6
6 4
4 6
output
96
6 4
input
2
100001 900000
900001 100000
output
180000000000
900000 100000
问题描述
给n个ai*bi*1的蛋糕块,选其中任意多块,每层一块,可切割,问能搭成的长方体体积最大为多少?该长方体地面长宽?
问题分析
将所有蛋糕按照短边排序,依次枚举短边为当前边时能搭成的最大体积。
一次枚举时的操作:
易知当前蛋糕块后面的所有块都满足当前短边要求,所以将后面所有块按照其长边排序,再次枚举。
二次枚举时的操作:
易知当前蛋糕块后面的所有块都能经过切割匹配当前枚举到的短边和长边,所以以当前枚举到的短边和长边为长和宽的长方体体积为当前短边*当前长边*后面的蛋糕块数(包括当前块)。
注意:
二次枚举时的短边和长边可能来自两块蛋糕。
AC代码
#include <iostream>
#include<queue>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll long long int
struct node {
	ll l, w;//l<=w
}cakes[4100],tem[4100];
bool cmp(node a, node b) {
	return a.l < b.l;
}
bool cmp2(node a, node b) {
	return a.w < b.w;
}
int main()
{
	int n;
	ll area[4100];
	ll ans = 0, mmax = 0;
	int ta, tb;
	while (cin >> n) {
		for (int i = 0; i < n; ++i) {
			cin >> ta>>tb;
			cakes[i].l = min(ta, tb);
			cakes[i].w = max(ta, tb);
		}
		sort(cakes, cakes + n, cmp);//按短边排序

		for (int i = 0; i < n; ++i) {
			memcpy(tem, cakes, sizeof(cakes));
			sort(tem + i, tem + n, cmp2);//按长边排序
			for (int j = i; j < n; ++j) {
				ll cnt = cakes[i].l*tem[j].w*(n-j);
				if (mmax < cnt) {
					ta = cakes[i].l;
					tb = tem[j].w;
					mmax = cnt;
				}
			}
			
		}
		cout << mmax << endl;
		//for (int i = 0; i < n; ++i) {
		//	if (area[i] == mmax)
				cout << ta << ' ' << tb << endl;
		//}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值