B. Layer Cake
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard 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.
Sample test(s)
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
Note
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个长宽是x,y,高为1的长方体。问用这些小的长方体能组成的最大的长方体的体积是多少,(一个长方体你可以切出一个你需要的小长方体,但是剩下的部分是不能再用的)
思路:按照X从大到小。Y从小到大的顺序排序。然后枚举x的时候,把之前的Y的排序。
#include<bits/stdc++.h>
using namespace std;
const int maxn=4011;
const int inf=1<<27;
#define LL long long
#define P pair<int,int>
#define pb push_back
#define cl(a,b) memset(a,b,sizeof(a));
struct node{
LL x,y;
bool operator<(const node&t) const {
return x>t.x||(t.x==x&&y<t.y);
}
}p[maxn];
LL X[maxn],Y[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
LL mi1=inf,mi2=inf;
for(int i=0;i<n;i++){
scanf("%lld%lld",&p[i].x,&p[i].y);
if(p[i].x>p[i].y)swap(p[i].x,p[i].y);
}
sort(p,p+n);
for(int i=0;i<n;i++){
X[i]=p[i].x;
Y[i]=p[i].y;
}
LL x,y,z=0;
for(int i=0;i<n;i++){
sort(Y,Y+i+1);
for(int j=0;j<=i;j++){
if((i-j+1)*X[i]*Y[j]>z){
z=(i-j+1)*X[i]*Y[j];
x=X[i];
y=Y[j];
}
}
}
printf("%lld\n%lld %lld\n",z,x,y);
}
return 0;
}