Codeforces Round #297 (Div. 2) C. Ilya and Sticks
来源:互联网 发布:三哥平面户型优化 编辑:程序博客网 时间:2024/06/06 20:31
原题链接:
http://codeforces.com/contest/525/problem/C
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?
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.
42 4 4 2
8
42 2 3 5
0
4100003 100004 100005 100006
10000800015
思路:就是贪心。不知道比赛的时候为什么会错。。。
代码:
#include "stdio.h"#include "iostream"#include "string.h"#include "stdlib.h"#include "algorithm"#include "math.h"#include "map"#include "queue"#include "stack"using namespace std;const int INF=0x3f3f3f3f;const int MAXN=100005;typedef long long LL;int d[100010],n,num,flag=0,pre;LL ans=0,nans=0,np=0;int cmp(int a1,int a2){return (a1>a2);}void getans(int a){if(nans%2==0){np=a;}elseans+=(LL)np*(LL)a;nans++;}int main(){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&d[i]);}sort(d,d+n,cmp);for(int i=0;i<n;i++){if(flag==0){flag=1;pre=d[i];}else{if(d[i]==pre||d[i]==pre-1){getans(d[i]);flag=0;}else{pre=d[i];}}}printf("%I64d\n",ans);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
- Codeforces Round #297 (Div. 2) C. Ilya and Sticks
- C. Ilya and Sticks(Codeforces Round #297 (Div. 2) 水题)
- Codeforces Round #297 (Div. 2) C. Ilya and Sticks 解题报告
- Codeforces Round #297 (Div. 2) 525C Ilya and Sticks(脑洞)
- Codeforces #297 (Div. 2) C. Ilya and Sticks(贪心
- #297 (div.2) C. Ilya and Sticks
- C. Ilya and Sticks(Codeforces Round 297(div2))
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- codeforces 525C Ilya and Sticks
- CodeForces-525C Ilya and Sticks
- Codeforces 525C:Ilya and Sticks
- CodeForces 525C Ilya and Sticks
- Codeforces 525C. Ilya and Sticks
- 促销#卡券系统基本设计
- 公司前端页面制作小心得
- 三羊献瑞
- Java线程进程相关知识点
- Tolua++技术文档
- Codeforces Round #297 (Div. 2) C. Ilya and Sticks
- 介绍手机软件运行环境
- hdu1078 (记忆化搜索)
- 第六届蓝桥杯java试题-循环节长度
- 小谈IE6下处理PNG图片的兼容问题
- 一切从零开始(系统)
- 响应式网页设计教程:展示响应式设计的基本原理
- 新人浅谈IE6兼容
- 海量处理题目