CodeForces 589B Layer Cake(枚举)
来源:互联网 发布:轩辕剑神器进阶数据 编辑:程序博客网 时间:2024/05/18 01:05
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.
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.
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.
55 121 14 66 44 6
966 4
2100001 900000900001 100000
180000000000900000 100000
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个矩形长中的一个,宽也是n个矩形宽中的某一个。先对n个矩形按照长从大到小,宽从小到大排序。对于每个可能的长和宽,枚举满足长和宽都大于当前长和宽的矩形的个数,乘以当前的面积取最大值即可。在下面的代码中,集合中插入的是每个矩形的宽,每次选择一个长L,然后扫描当前集合中所有的宽W,对每一条宽W,算出满足宽大于等于it的矩形的个数NUM(因为集合中的元素是有序排列的,这个数目可以直接算出。因为L是逆序排序的,所以集合中的W所对应的L肯定大于当前选择的L)。答案就是当前L*W*NUM的最大值。
#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<set>#include<map>#include<cctype>using namespace std;typedef long long ll;const int maxn = 4000 + 10;const int INF = 1e9+10;struct node{ ll a,b; bool operator < (const node& rhs) const { return a > rhs.a || a == rhs.a && b < rhs.b; }}rec[maxn];multiset<int> s;multiset<int>::iterator it;int main(){ int n; scanf("%d",&n); for (int i = 0; i < n; i++) { scanf("%I64d%I64d",&rec[i].a, &rec[i].b); if (rec[i].a < rec[i].b) swap(rec[i].a, rec[i].b); } sort(rec,rec+n); ll ans_a(0),ans_b(0),ans(0); for (int i = 0; i < n; i++) { s.insert(rec[i].b); int cur = 0; for (it = s.begin(); it != s.end(); ++it) { ll sum = rec[i].a * (*it) * (i+1-cur); if (sum > ans) { ans = sum; ans_a = *it; ans_b = rec[i].a; } cur++; } } printf("%I64d\n%I64d %I64d\n",ans,ans_b,ans_a);}
- CodeForces 589B Layer Cake(枚举)
- CodeForces 589B -- B. Layer Cake(多重集合+技巧枚举)
- CodeForces 589B Layer Cake
- CodeForces 589B Layer Cake
- Codeforces 589B Layer Cake(两次排序)
- CodeForces #589 B Layer Cake 类似01背包的模拟题
- codeforces 589B (枚举角度题目)
- CodeForces 707B Bakery(建图 + 枚举)
- 枚举 codeforces 452B
- 【codeforces 711B + 枚举】
- cf589B Layer Cake 排序
- Codeforces589B Layer Cake
- CodeForces 156B Suspects(枚举)
- codeforces 724B 暴力+枚举
- CodeForces 724B(暴力枚举)
- 10167 - Birthday Cake(直接枚举)
- uva 10167 Birthday Cake(暴力/枚举)
- UVA - 10167 - Birthday Cake (简单枚举)
- Android解决eclipse导入工程到androidstudio中出现错:There are unrecoverable errors which must be corrected first
- cygwin下使用apt-cyg 安装软件及遇到MD5 sum did not match, exiting的问题
- 1043: 绩点计算
- hdoj3466Proud Merchants(排序+01背包)
- 数据结构实验之二叉树四:还原二叉树
- CodeForces 589B Layer Cake(枚举)
- bootstrap分页插件
- C语言第三周,指针
- HDU 5826 physics (数学推导)(规律)
- mongo2.x的mongo.exe连接3.x的mongod的时候,show collections不出内容
- Mybatis foreach 批量操作
- ubuntu 安装man 帮助手册
- 深入浅出聊iptables
- sublime text3 前端编译神器,浏览器实时显示