Codeforces 525C Ilya and Sticks【贪心】

来源:互联网 发布:html打赏视频源码 编辑:程序博客网 时间:2024/05/24 07:13

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 a1a2a3 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.

Examples
input
42 4 4 2
output
8
input
42 2 3 5
output
0
input
4100003 100004 100005 100006
output
10000800015

题目大意:

一共有N根棒子,每根棒子的长度已知,而且我们可以将任意棒子的长度减1,问所有棒子能够组成的举行的最大的面积和。


思路:


1、首先将棒子的长度按照从小到大排序,那么我们每次贪心出来两个矩形的长,然后再贪心出来两个矩阵的宽,每当能够使得其组成一个矩形的时候,累加其和即可。


2、贪心找长和宽的时候,只需要每次枚举临近的两根棒子即可,如果其长度差==0或者是==1,那么其两根棒子的最小值就可以作为两个对着的边的长度(可能是作为长,也可能作为是宽)。


Ac代码:


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll __int64ll a[1000006];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)        {            scanf("%I64d",&a[i]);        }        sort(a,a+n);        ll chang=0;        ll kuan=0;        ll output=0;        for(int i=n-1;i>=1;i--)        {            if(a[i]==a[i-1]||a[i]==a[i-1]+1)            {                if(chang==0)                chang=a[i-1],i--;                else if(kuan==0)                kuan=a[i-1],i--;            }            if(chang!=0&&kuan!=0)            {                output+=chang*kuan;                chang=0;                kuan=0;            }        }        printf("%I64d\n",output);    }}





0 0
原创粉丝点击