C. Ilya and Sticks
2 seconds
256 megabytes
standard input
standard output
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?
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.
The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.
4
2 4 4 2
8
4
2 2 3 5
0
4
100003 100004 100005 100006
10000800015 给出n条线段的长度,任意一条长度为len的线段可以当作len或len-1的线段使用,求能构成的矩形的最大的总面积。 任意一个矩形当中有两对边的长度是相等的。我们将问题转化为 对子问题。 要是总面积最大,就要贪心,使长度最大的对子和长度次最大的对子 组合,接下去同样的组合。 代码实现如下。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=;
const int MS2=; int a[MS]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
LL ans=,l=;
for(int i=n-;i>;)
{
if(a[i]-a[i-]==||a[i]==a[i-])
{
if(l==)
l=a[i-];
else
{
ans+=l*a[i-];
l=;
}
i-=;
}
else
i--;
}
printf("%lld\n",ans);
return ;
}
C. Ilya and Sticks的更多相关文章
- 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 MBSubmit: xxx ...
- 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) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- C - Ilya and Sticks(贪心)
Problem description In the evening, after the contest Ilya was bored, and he really felt like maximi ...
- CodeForces 525C Ilya and Sticks 贪心
题目:click here #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- 【Henu ACM Round#18 C】Ilya and Sticks
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用cnt[i]记录数字i出现的次数就好. 然后i从1e6逆序到1 如果cnt[i+1]和cnt[i]>0同时成立的话. 那么得 ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
随机推荐
- 有关require package的应用
http://stackoverflow.com/questions/9302284/relative-paths-with-requirejs-modules-packages http://sta ...
- 新 esb-cs-tool.jar 参数说明
旧esb-cs-tool.jar 使用说明 : invoke(RequestBusinessObject requestBo) 旧参数说明: requestBo : 封装好的请求参数大对象 Req ...
- Spring中的BeanUtils与apache commons中的BeanUtils用法[1]
1. 前言 在开发过程中,经常遇到把要给一个bean的属性赋给另外一个bean.最笨的方法是每个属性都单独写一个,聪明的方法是应用反射写一个工具方法.考虑到这个需求基本每个程序员都会遇到,那么一定已经 ...
- ajax 本地测试,使用Chrome 浏览器
出现问题: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is ...
- AFNetworking 与 NSURLSession
转载自:http://blog.sina.com.cn/s/blog_8157560c0101kt7h.html 1. 也就是说在IOS 7.1 之后你想用网络请求的话有两种途径,NSUrlSessi ...
- Linux内核完全注释之编程语言和环境(一)
as86汇编器 1.来源与对于linux的用途 as86来源minix-386开发的intel 8086.80386汇编编译程序和链接程序,他主要为linux创建16位的启动引导扇区程序boot/bo ...
- Spring REST实践之REST基本介绍
REST是什么 REST(REpresentational State Transfer)是一个设计分布式web应用的框架风格,有六个基本原则: Client-Server:应用的参独立与者可分为Cl ...
- 设置ul阴影效果和边框圆角
ul.box {position: relative;z-index: 1; /* prevent shadows falling behind containers with backgrounds ...
- dojo grid 分页
dojo很强大,也很方便,但是缺少文档,只能看源代码,也挺好的,就是费时间... 网上找了一段代码(找不到原出处了,不好意思),也看了dojo自带的demo,放一段可以执行的页面代码这里.把ip换成自 ...
- 【转】【公司调查】车来了APP
http://blog.sina.com.cn/s/blog_83b10acc0102vk7k.html [APP简介] "车来了"是武汉元光科技有限公司开发的一款查询公交车实 ...