【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest B】【暴力双排序】Layer Cake 若干矩形 选择相同长和宽的最大体积

来源:互联网 发布:javascript面向对象 编辑:程序博客网 时间:2024/06/05 09:35

B. Layer Cake
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
55 121 14 66 44 6
output
966 4
input
2100001 900000900001 100000
output
180000000000900000 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.



#include<stdio.h>#include<algorithm>using namespace std;typedef long long LL;const int N=4000+10;int n;pair<int,int>a[N];int b[N];int main(){    while(~scanf("%d",&n))    {        LL ans=0;int ans1,ans2;        for(int i=1;i<=n;i++)        {            scanf("%d%d",&a[i].first,&a[i].second);            if(a[i].first>a[i].second)swap(a[i].first,a[i].second);        }        sort(a+1,a+n+1);        int m=0;        for(int i=n;i>=1;i--)        {            b[++m]=a[i].second;            sort(b+1,b+m+1);            for(int j=1;j<=m;j++)            {                LL tmp=(LL)a[i].first*b[j]*(m+1-j);                if(tmp>ans)                {                    ans=tmp;                    ans1=a[i].first;                    ans2=b[j];                }            }        }        printf("%lld\n",ans);        printf("%d %d\n",ans1,ans2);    }}/*【题意】有n(4000)个矩形,每个矩形都有一个长和宽,长宽都是[1e6]范围内的数我们想从中选出任意数量的矩形,可以把其长或宽切小,使得之后所有选定矩形的长和宽都相同。而且使得(个数*长*宽)这个数值尽可能大,并输出这个数值,和对应一组可行的长与宽。【类型】排序【分析】我们可以先使得我们最后取得的长>=宽。然后我们发现,最后选择的长一定是某个矩形的长,最后选择的宽也一定是某个矩形的宽。然后先枚举长,使得它作为我们选定的长,设为A。那其他可以选这个长度为长的矩形,肯定是从长>=A的矩形中选那对应的宽是什么呢?我们把这所有长度>=A的矩形的宽都拿出来,然后排个序。再依次枚举宽选定为什么。这样就可以啦。时间复杂度O(nnlogn)实际上,因为每次是在原有的排序后有序数列的基准上,又添加一个新的长度的宽。排序其实单次没有nlogn所以实际上的时间复杂度是介于[n^2,n^2logn]之间的。*/


1 0
原创粉丝点击