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
5
5 12
1 1
4 6
6 4
4 6
96
6 4
2
100001 900000
900001 100000
180000000000
900000 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个矩形n(1 ≤ n ≤ 4000) ,矩形可以削减,从中选出k个,(可以)削减后,保证k个矩形面积一样,求k*矩形面积的最大值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=20000+10;
int n,b[maxn];
struct node{
int x,y;
bool operator<(const node &a) const
{return this->x<a.x;}
}ne[maxn];
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d %d",&ne[i].x,&ne[i].y);
if(ne[i].x>ne[i].y) swap(ne[i].x,ne[i].y);
}
sort(ne+1,ne+n+1);
int l,w,cnt=0;ll ans=0;
for(int i=n;i>=1;i--)
{
b[++cnt]=ne[i].y;
sort(b+1,b+cnt+1);
for(int j=1;j<=cnt;j++)
if(ans<((ll)ne[i].x)*b[j]*(cnt-j+1))
{
ans=((ll)ne[i].x)*b[j]*(cnt-j+1);
l=ne[i].x;
w=b[j];
}
}
if(l<w) swap(l,w);
printf("%lld\n%d %d\n",ans,l,w);
}
return 0;
}
比赛分析:
比赛时想到了需要枚举所有的边的组合(n^2),然后还想到了应该算出这种组合下的矩形的个数(O(n)),但是这样
复杂度是n^3啊,显然需要降低,然后就一直在纠结着怎么用二分求出矩形个数,,,但是二分出来时错的,因为虽然是先按x排序,再
按y排序,那么当二分出来一个点后,很可能x比该点大的点的y值不满足条件,那么这个点就不能计入.......
纠错:
我们先将x排好序后,然后从大到小枚举x(O(n)),将所有x>=当前枚举的x的点的y值从小到大排排序(nlogn),然后再
排好序的y上进行枚举,其实就是通过两次排序筛选出x'>=x,y'>=y的值得矩形个数