您的位置:首页 > 其它

C. Ilya and Sticks(Codeforces Round 297(div2))

2015-03-27 16:38 411 查看
C. Ilya and Sticks

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
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, sticks5 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

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.

Sample test(s)

input
4
2 4 4 2


output
8


input
4
2 2 3 5


output
0


input
4
100003 100004 100005 100006


output
10000800015


题意:

给出n条线段构造矩形面积最大。每条线段可以减掉1个单位长度

题解:

先排序,从大到小处理来保证面积最大,将所有可以做长和宽的两条线段找出来,要使矩形面积最大,则长和宽尽量差别小,于是就可以用相邻的满足可以做长和宽的线段组成矩形。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
long long a[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
}
sort(a,a+n);
long long l=0,ans=0;
for(int i=n-1;i>=0;)
{
// cout<<a[i]<<endl;
if((a[i]-a[i-1]==1)||(a[i]==a[i-1]))
{
if(l==0)
{
l=a[i-1];
//cout<<l<<endl;
}
else
{
ans=ans+l*a[i-1];
l=0;
}
i=i-2;
}
else
i--;
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: