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

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: xxx  Solved: 2xx

题目连接

http://codeforces.com/contest/525/problem/C

Description

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, sticks 5 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 Input

Input
42 4 4 2

Input
42 2 3 5

Input
4100003 100004 100005 100006

Sample Output

Output
8
Output
0
Output
10000800015

HINT

题意:

给你一堆棍子,然后选择一些棍子来组成多个矩形,这些棍子有一个特点,长度为l的可以当成长度为l-1的用,问你最多能够成的矩形总面积是多少!

总面积是多少!

总面积是多少!

唔,因为很重要,所以说三遍,喵~

题解:

用一个flag[l]记录长度为l的棍子有多少个,flag1[l]记录长度为l+1的棍子有多少个,然后从大往小扫,如果发现flag[l]+flag1[l]=>2的时候,那就把长度为l的当成一条边,然后再i++再继续扫一下这个长度,优先减flag1[l]的数量。

注意,当减flag[l]的时候,flag1[l-1]也会减少~

就这样边扫边维护就好啦~

唔,我感觉我说的不是很清楚……

还是看我呆呆的代码吧

~\(≧▽≦)/~啦啦啦,这道题完啦

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 4000100
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff;   //无限大
const int inf=0x3f3f3f3f;
/**/
//**************************************************************************************
inline ll read()
{int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
int flag[maxn];
int flag2[maxn];
map<int,int> s;
int main()
{int n;n=read();int m=0;for(int i=0;i<n;i++){int x=read();flag[x]++;flag2[x-1]++;m=max(m,x);}ll ans2=0;ll ans1=0;ll ans=0;for(int i=m;i>=0;i--){if(flag[i]+flag2[i]>=2){if(ans1==0){ans1=i;if(flag2[i]==0){flag[i]-=2;flag2[i-1]-=2;}else if(flag2[i]==1){flag2[i]-=1;flag[i]-=1;flag2[i-1]-=1;}elseflag2[i]-=2;}else{if(flag2[i]==0){flag[i]-=2;flag2[i-1]-=2;}else if(flag2[i]==1){flag2[i]-=1;flag[i]-=1;flag2[i-1]-=1;}elseflag2[i]-=2;ans+=ans1*i;ans1=0;}i++;}}cout<<ans<<endl;return 0;
}

转载于:https://www.cnblogs.com/qscqesze/p/4371809.html

Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心相关推荐

  1. 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 MB Submit: xxx  ...

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

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

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

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

  4. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

  5. Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题

    A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...

  6. Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs

    题目链接: http://codeforces.com/contest/525/problem/D 题意 给你一个n*m的田地,有一些*的地方是可以移除变成"."的,然后问你移除最 ...

  7. Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心

    E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

  8. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列

    链接:http://codeforces.com/problemset/problem/447/D 题意:一个n*m的矩阵.能够进行k次操作,每次操作室对某一行或某一列的的数都减p,获得的得分是这一行 ...

  9. Codeforces Round #301 (Div. 2) B. School Marks 构造/贪心

    B. School Marks Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/probl ...

最新文章

  1. 2017还有29天,你的目标实现了吗?|内有彩蛋
  2. AME_Oracle自带AME审批链详解AME Standard Handler(概念)
  3. 今天分享个用Python爬虫爬取Bilibili弹幕的小例子解析
  4. socket 获取回传信息_基于netty框架的socket长连接负载均衡解决方案 oswl
  5. 织梦调用css的标签,织梦dede常用的调用标签(个人总结)
  6. 未能加载文件或程序集“xxx”或它的某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集...
  7. 【目标检测_解耦】 Revisiting the Sibling Head in Object Detector_2020
  8. Storm概念学习系列之Topology拓扑
  9. 18 ubuntu 远程原生_CNCF公布中国云原生调查报告:49%使用容器技术,Kubernetes 应用率达 72%...
  10. mysql的常用命令总结
  11. 相机java程序_以编程方式在Android上用相机拍照
  12. 第三部分:Android 应用程序接口指南---第一节:应用程序组件---第一章1-1.Fragment...
  13. 匹配数据库 帆软 查询条件_帆软报表学习笔记①——根据参数查询
  14. 女生可以做软件测试吗?
  15. Windows系统下R语言环境搭建及高级图表绘制
  16. 22处令人叹为观止的景观
  17. Unity3D中Rigidbody.velocity和Addforce的区别
  18. 融合CV和NLP的视觉语义导航
  19. MSF编码+VS编译木马免杀
  20. ftp客户端android版,Primitive FTPd(FTP客户端)

热门文章

  1. 全国计算机等级考试——二级公共基础知识辅导讲义 卿勇军主讲
  2. 在LINUX下架设防火墙
  3. Python 通过ctypes调用 ICTCLAS3.0.DLL
  4. 安装Exchange2003时出0XC1037AE6错误的解决方法.
  5. 好书推介---Windows Server 2003企业部署原理与实践
  6. hust sci列表
  7. Node.js中模块加载机制
  8. 整合mybatis——使用纯注解整合、使用Mapper+Mapper.xml整合、使用mybatis.cfg.xml整合
  9. 配置 postCSS 自动添加 css 的兼容前缀||打包样式表中的图片和字体文件||打包处理 js 文件中的高级语法
  10. JS删除数组元素的函数介绍