CodeForces 589B -- B. Layer Cake(多重集合+技巧枚举)

49 篇文章 0 订阅
17 篇文章 0 订阅

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.
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

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 个矩形,知道矩形的长度和宽度,每个矩形的长度可以剪掉一部分,宽度也同样,求出可以得到多少个一样的矩形,使得面积之和最大(大体上是这样,在仔细读读吧= =!)


思路:
比赛时没有时间了,自己瞎暴的,果断TLE !
借鉴了学长的代码
因为最终的矩形的长一定是输入的某个矩形的长,宽也一定是某个矩形的宽。
所以在输入时令长为max(r,c),宽为min(r,c)
先枚举长R,那么宽一定是从其他矩形的长大于等于R的矩形里选择。
所以就可以倒着枚举长,从长的开始,把此时宽压入multiset里,此时set里的的宽肯定都是长大于等于A的矩形里的宽,枚举计数即可!


详细见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node{
    int r,c;
    bool operator < (const Node& rhs) const {
        return r > rhs.r || (r == rhs.r && c < rhs.c);
    }
}p[4007];
multiset<int>s;
multiset<int>::iterator it;
int main(){
    int n;
    scanf("%d",&n);
    for (int i = 0; i < n; ++i){
        int r,c;
        scanf("%d %d",&r,&c);
        if (r < c)swap(r,c);
        p[i].r = r;
        p[i].c = c;
    }
    sort(p,p+n);
    int cur = 0;
    ll ans = 0ll;
    int posi,posj;
    for (int i = 0; i < n; ++i){
        s.insert(p[i].c);
        ++cur;
        int cnt = 0;
        for (it = s.begin(); it != s.end(); ++it){
            ll sum = (ll)p[i].r * (ll)(*it) * ll(cur-cnt);
//            printf("%I64d\n",sum);
            if (sum > ans){
                ans = sum;
                posi = p[i].r;
                posj = (*it);
            }
            ++cnt;
        }

    }
    printf("%I64d\n",ans);
    printf("%d %d\n",posi,posj);
    return 0;
}

吐槽:
头一次用Markdown编辑器,哎哟,不错哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值