Codeforces #297 (Div. 2) C. Ilya and Sticks(贪心
2017-03-21 23:39
435 查看
C. Ilya and Sticks
time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn 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
4000also 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?
InputThe 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.
OutputThe 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.
ExamplesInput
4 2 4 4 2Output
8Input
4 2 2 3 5Output
0Input
4 100003 100004 100005 100006Output
10000800015
很经典的贪心问题,英语太差,看错题系列
注意LL,没理解题意wa一次,在LL这里wa3次 还是菜啊[/u]
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 200000+100#define LL long long
bool cmp(int x,int y)
{
return x > y;
}
int arr
;
int main()
{
LL n;
scanf("%lld",&n);
for(int i = 0;i < n; i++) {
scanf("%d",&arr[i]);
}
sort(arr,arr+n,cmp);
LL sum = 0, len = 0;
for(int i = 0;i < n;) {
if(arr[i]==arr[i+1] || arr[i]==arr[i+1]+1) {
if(len == 0) {
len = arr[i+1];
}
else {
sum += arr[i+1]*len;
len = 0;
}
i += 2;
}
else {
i++;
}
}
printf("%lld",sum);
return 0;
}
相关文章推荐
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
- Codeforces Round #297 (Div. 2) C. Ilya and Sticks
- Codeforces Round #297 (Div. 2) C - Ilya and Sticks
- CF 525C(Ilya and Sticks-贪心)
- Codeforces #297 (Div. 2) B. Pasha and String (贪心
- Codeforces Round #297 (Div. 2) C. Ilya and Sticks
- Codeforces Round #297 (Div. 2) C. Ilya and Sticks 解题报告
- Codeforces Round #297 (Div. 2) C.Ilya and Sticks
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks
- C. Ilya and Sticks(Codeforces Round 297(div2))
- C. Ilya and Sticks(Codeforces Round #297 (Div. 2) 水题)
- Codeforces Round #444 (Div. 2) D. Ratings and Reality Shows 贪心
- 【Codeforces Round 269 (Div 2)B】【简单构造】MUH and Sticks 数值升序输出三种可能编号序
- Codeforces Round #394 (Div. 2)D. Dasha and Very Difficult Problem【贪心】
- 【Codeforces Round 370 (Div 2) C】【正难则反 贪心】Memory and De-Evolution
- Codeforces Round #425 (Div. 2) A. Sasha and Sticks
- Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator 概率DP