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个棒子的长度, 每一个棒子能够变为自己的len - 1, 问能够组成题目中要求的矩形的面子最大是多少.

将棒子排序, 由大到小遍历棒子, 因为每一个棒子能够变成自己的len - 1, 若相邻棒子差值为1或0, 则四边构成一个矩形, 累加得到答案.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 5;
int n;
ll x, ans;
int main(int argc, char const *argv[])
{
cin >> n;
vector<int> v(n);
for(int i = 0; i < n; ++i)
cin >> v[i];
sort(v.begin(), v.end());
for(int i = n - 2; i >= 0; --i)
if(v[i + 1] - v[i] < 2) {
if(x) {
ans += x * v[i];
x = 0;
}
else x = v[i];
i--;
}
cout << ans << endl;
return 0;
}

Codeforces Round #297 (Div. 2) 525C Ilya and Sticks(脑洞)的更多相关文章

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

  2. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  3. Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索

    Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx  ...

  4. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  5. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  6. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  7. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  8. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  9. 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

    题目传送门 /* 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 题目倒是很长:) */ #include <cstdio> #include <algorithm> ...

随机推荐

  1. 树莓派编译nginx,支持rtmp直播

    树莓派3B+ 系统更新至最新 下载依赖 sudo apt-get update sudo apt-get install libxslt1-dev libgd-dev libgeoip-dev lib ...

  2. 博弈论与SG函数

    巴什博奕: 两个顶尖聪明的人在玩游戏,有n个石子,每人可以随便拿1−m个石子,不能拿的人为败者,问谁会胜利 结论: 设当前的石子数为\(n=k∗(m+1)\)即\(n%(m+1)==0\)时先手一定失 ...

  3. Miller-Rabin与Pollard-Rho备忘

    Miller-Rabin素性测试算法: 根据费马小定理当p为素数时成立,所以如果存在一个a使x不满足此定理,则x必然不为素数. 但这是充分条件而不是必要条件,所以对于每个a,可能存在满足定理的x,这时 ...

  4. POJ 3688 Cheat in the Game(博弈论)

    [题目链接] http://poj.org/problem?id=3688 [题目大意] 有俩人玩一个取石子的游戏,你是裁判. 游戏中有W块石头和N张卡片,卡片上分别写着数字Ai. 玩家随机抽走一张卡 ...

  5. ToggleButton控件,Switch控件

    (一) 1.效果图    2. activity_main.xml <?xml version="1.0" encoding="utf-8"?> & ...

  6. Saga的实现模式——进化(Saga implementation patterns – variations)

    在之前的几个博客中,我主要讲了两个saga的实现模式: 基于command的控制者模式 基于事件的观察者模式 当然,这些都不是实现saga的唯一方式.我们甚至可以将这些结合起来. 发布者——收集者 回 ...

  7. Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface解决方案

    原文:http://pimin.net/archives/432 环境:Eclipse LUNA.Spring 4.1.1.或Spring 4.3.3.Spring Data Elasticsearc ...

  8. [Android 新特性] 谷歌发布Android Studio开发工具1.0正式版(组图) 2014-12-09 09:35:40

    Android Studio是谷歌于13年I/O大会推出的Android开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的Androi ...

  9. UNDO表空间损坏导致数据库无法OPEN

    在数据库undo表空间文件损坏.或者undo表空间文件缺失的情况下.无法打开数据库. 这两种情况都能够视为一种情况处理,解决方法一样. 场景:在23:10的时候新建一个undo表空间undotbs02 ...

  10. 计算两个经纬度之间的距离(python算法)

    EARTH_REDIUS = 6378.137 def rad(d): return d * pi / 180.0 def getDistance(lat1, lng1, lat2, lng2): r ...