Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks
Time Limit: 2 Sec Memory Limit: 256 MB
Submit: xxx Solved: 2xx
题目连接
http://codeforces.com/contest/525/problem/C
Description
In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.
Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.
Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed:
- a1 ≤ a2 ≤ a3 ≤ a4
- a1 = a2
- a3 = a4
A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7.
Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.
You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks.
The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.
Output
Sample Input
4
2 4 4 2
4
2 2 3 5
4
100003 100004 100005 100006
Sample Output
8
0
10000800015
HINT
题意:
给你一堆棍子,然后选择一些棍子来组成多个矩形,这些棍子有一个特点,长度为l的可以当成长度为l-1的用,问你最多能够成的矩形总面积是多少!
总面积是多少!
总面积是多少!
唔,因为很重要,所以说三遍,喵~
题解:
用一个flag[l]记录长度为l的棍子有多少个,flag1[l]记录长度为l+1的棍子有多少个,然后从大往小扫,如果发现flag[l]+flag1[l]=>2的时候,那就把长度为l的当成一条边,然后再i++再继续扫一下这个长度,优先减flag1[l]的数量。
注意,当减flag[l]的时候,flag1[l-1]也会减少~
就这样边扫边维护就好啦~
唔,我感觉我说的不是很清楚……
还是看我呆呆的代码吧
~\(≧▽≦)/~啦啦啦,这道题完啦
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 4000100
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* */
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int flag[maxn];
int flag2[maxn];
map<int,int> s;
int main()
{
int n;
n=read();
int m=;
for(int i=;i<n;i++)
{
int x=read();
flag[x]++;
flag2[x-]++;
m=max(m,x);
}
ll ans2=;
ll ans1=;
ll ans=;
for(int i=m;i>=;i--)
{
if(flag[i]+flag2[i]>=)
{
if(ans1==)
{
ans1=i;
if(flag2[i]==)
{
flag[i]-=;
flag2[i-]-=;
}
else if(flag2[i]==)
{
flag2[i]-=;
flag[i]-=;
flag2[i-]-=;
}
else
flag2[i]-=;
}
else
{
if(flag2[i]==)
{
flag[i]-=;
flag2[i-]-=;
}
else if(flag2[i]==)
{
flag2[i]-=;
flag[i]-=;
flag2[i-]-=;
}
else
flag2[i]-=;
ans+=ans1*i;
ans1=;
}
i++;
}
}
cout<<ans<<endl;
return ;
}
Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心的更多相关文章
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- Codeforces Round #297 (Div. 2) 525C Ilya and Sticks(脑洞)
C. Ilya and Sticks time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
- 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie
题目传送门 /* 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 题目倒是很长:) */ #include <cstdio> #include <algorithm> ...
随机推荐
- AndroidManifest.xml权限设置
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permi ...
- jumpserver安装教程
centos7系统一步一步安装jumpserver 参照官方文档,查找了百度所有的文档,基本上都是按照官方的文档操作的 官方文档点我-> 安装jumpserver需注意: 1:网络环境要好,有的 ...
- html5拖拽初窥
说到拖动,大概有两种方式,一种是js实现,之前已经介绍过,今天来讲解另外一种方式,那就是使用html5实现拖动. css样式 .box { width: 200px; height: 200px; b ...
- java基础56 HTML5的标签知识(网页知识)
本文知识点(目录): 1.html常用标签 2.html实体标签 3.html媒体标签 4.html超链接标签 5.html图片标签 6.html标个标签 7.html框 ...
- 安装mysql-python报错解决办法
报错: 按照网上的办法,安装mysql-connector-c-6.1.10-winx64.msi和MySQL-python-1.2.3.win-amd64-py2.7 .exe都不行,又源码安装My ...
- C# TabControl 隐藏标签头(TabControl Hide Head)
TabControl控件,有时候需要动态显示一个或者多个标签页,如果只是显示一个标签页的时候不想显示标签头,所以有可能隐藏头部的需求. 如下代码可以实现 public Form1() { Initia ...
- drools7 (一、最简单的例子)
切记!!! 必须使用jdk1.8 工程目录 引入依赖包,pom.xml <?xml version="1.0" encoding="UTF-8"?> ...
- day7面向对象--反射
反射 通过字符串映射或修改程序运行时的状态.属性.方法, 有以下4个方法 1.getattr(object, name[, default]) -> value Get a named ...
- C语言:输入一个多位的数字,12345,求各位相加1+2+3+4+5=15
题目: 输入一个多位的数字,12345,求各位相加1+2+3+4+5=15(10分)题目内容: 输入一个多位的数字,1求各数位相加. 例如输入12345,则计算1+2+3+4+5=15 输入格式: 一 ...
- IAR搭建unity框架
1. 新建工程 2. 增加组 unit,并加入相应源码 3. 增加需要测试的API源文件 4. 编写相应的测试用例 编译,download and debug view->TerminalIO ...