CodeForces - 589B(暴力+排序)
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 birespectively, 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
5
5 12
1 1
4 6
6 4
4 6
96
6 4
2
100001 900000
900001 100000
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层蛋糕,每一层的蛋糕的长和宽分别为为 l,和 w,高为 1 ,要你求出把这个蛋糕的每层的长和宽都切成一样的时候可以得到的最大的体积(每一层的蛋糕切完的时候多余的部分会舍去)(可以完全把一层蛋糕舍去) (可以把某一层的蛋糕旋转)。
思路:写这道题目的时候想了想怎么用贪心来切,没想到怎么去贪,后面看了题目给的时间,就想用暴力应该可以过,但是如果不作任何优化的话肯定会超时,后面想了想可不可以通过排序来
优化一下,但是一开始傻了一下,想当然的以为排序暴力的时间是n*n* n*log2 n,比n*n*n还要大,就否认了这种方法,但实际上降为了n*(n+n*log2 n),以后做题不能这样了啊,引以为戒。
代码:
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
struct st{
ll w,l;
}s[4040];
bool cmp(st a,st b){
return a.w<b.w;
}
ll wl[4040];
int main(){
ll n;
ll a,b;
ll i,j,k;
scanf("%lld",&n);
for(i=0;i<n;i++){
scanf("%lld%lld",&a,&b);
if(a>b){
s[i].l=a;
s[i].w=b;
}
else{
s[i].l=b;
s[i].w=a;
}
}
sort(s,s+n,cmp);
ll w,l;
ll w1,l1;
ll sum;
ll ans=0;
w=s[0].w-1;
for(i=0;i<n;i++){
if(w==s[i].w)
continue;
w=s[i].w;
for(k=0,j=i;j<n;j++,k++){//拿出宽度大于等于要切宽度的蛋糕层
wl[k]=s[j].l;
}
sort(wl,wl+k);//把他们的长从小到大排序
sum=0;
l=wl[0]-1;
for(j=0;j<k;j++){
if(l==wl[j])
continue;
l=wl[j];
sum=w*l*(k-j);//因为长度是递增的,所以枚举的长度中有k-j 层蛋糕满足条件
if(ans<sum){
ans=sum;
w1=w;
l1=l;
}
}
}
printf("%lld\n",ans);
printf("%lld %lld\n",l1,w1);
return 0;
}
CodeForces - 589B(暴力+排序)的更多相关文章
- CodeForces 589B Layer Cake (暴力)
题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...
- CodeForces - 589B(暴力)
题目链接:http://codeforces.com/problemset/problem/589/B 题目大意:告诉你n 个矩形,知道矩形的长度和宽度(长和宽可以互换),每个矩形的长度可以剪掉一部分 ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...
- Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
- codeforces 691F 暴力
传送门:https://codeforces.com/contest/691/problem/F 题意:给你n个数和q次询问,每次询问问你有多少对ai,aj满足ai*aj>=q[i],注意 a* ...
- CodeForces 558E(计数排序+线段树优化)
题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串 思路: 该题数据规模 ...
随机推荐
- JavaWeb-----实现第一个Servlet程序
1.Servlet简介 Servlet是在服务器端运行的一个小程序,实际上一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问的这个驻留在服务器内 存里的servl ...
- restful规范快速记忆
restful规范: 十个规则: 用户发来请求,url必须: 1.因为是面向资源编程,所以每个URL代表一种资源,URL中尽量不要用动词,要用名词 2.尽量使用HTTPS,https代替http 3. ...
- vue 监听变量或对象
注意:监听的对象必须已经在data中声明了 data: { a: 1, b: 2, c: 3, d: 4, e: { f: { g: 5 } } }, watch: { a: function (va ...
- java面试题汇总(有的题无视即可,没什么实际用途)
相关概念 面向对象的三个特征 封装,继承,多态,这个应该是人人皆知,有时候也会加上抽象. 多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消 ...
- 2018-2019-2 20189206 Python3学习
python3简明教程学习 基本概念 脚本文件: 脚本文件英文为Script.实际上脚本就是程序,一般都是由应用程序提供的编程语言.应用程序包括浏览器(javaScript.VBScript).多媒体 ...
- for循环介绍
流程控制之for循环names=['yb','zs','yxd','lb'] i=0 while i < len(names): #4 < 4 print(names[i]) i+=1 # ...
- spark生成大宽表的parquet性能优化
1. 背景介绍 将一份数据量很大的用户属性文件解析成结构化的数据供查询框架查询剖析,其中用户属性包含用户标识,平台类型,性别,年龄,学历,兴趣爱好,购物倾向等等,大概共有七百个左右的标签属性.为了查 ...
- CSS设置DIV边框为圆角,添加背景色溢出的问题
这么几天需要做一个类似于层级展示的东西,最后一层需要做一些div框来展示数据,我用css设置了div的边框为圆角,但是添加背景色的时候颜色溢出,覆盖了四个角的圆弧,效果如图所示: css代码如下: . ...
- 想做AI测试,需要学习哪些数学知识?
摘自知乎的回答 作者:者也 以上是个人读研以来感受用得最多的数学基础课,挂一漏万,大侠请补充指正 高等数学是基础中的基础,研究生以上级别的一切理工科都需要这个打底,数据挖掘.人工智能.模式识别此类跟数 ...
- pip3 freeze导出依赖包 --Python3
1.导出依赖包到*.txt文件 pip3 freeze>blueflag.txt 2.导出后的结果