CF_525C_IlyaAndSticks
2015-08-06 23:16
369 查看
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, 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 test(s)
Input
Output
Input
Output
Input
Output
简单的模拟,另外贪心
优先取边长最长的两组组成的长方形一定比其他取法的长方形面积大
注意结果是total 不是只构成一个矩形
a*b+c*d其大小关系为a>b>c>d
若改成a*c+b*d
上下做差a*(b-c)+d*(c-b)
(a-d)(b-c)显然是个正值
若改成a*d+b*c
上下做差a*(b-d)+c*(d-b)
(a-c)(b-d)显然也是正值,因此方案一最大
其他的只需要记下数就可以了
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, 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 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
简单的模拟,另外贪心
优先取边长最长的两组组成的长方形一定比其他取法的长方形面积大
注意结果是total 不是只构成一个矩形
a*b+c*d其大小关系为a>b>c>d
若改成a*c+b*d
上下做差a*(b-c)+d*(c-b)
(a-d)(b-c)显然是个正值
若改成a*d+b*c
上下做差a*(b-d)+c*(d-b)
(a-c)(b-d)显然也是正值,因此方案一最大
其他的只需要记下数就可以了
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; typedef long long LL; const int M=1e6+5; int len[M]; int main() { int n; int maxl; int max1,max2; int le; long long tot; while(scanf("%d",&n)!=EOF) { memset(len,0,sizeof(len)); tot=0; maxl=max1=max2=0; for(int i=0;i<n;i++) { scanf("%d",&le); len[le]++; maxl=max(maxl,le); } for(int i=maxl;i>=2;i--) { while(len[i]+len[i+1]>=2)//每次找两边 { if(len[i+1]==1) { len[i+1]--; len[i]--; } else len[i]-=2; if(max1==0) max1=i; else { tot+=(LL)max1*i; //注意数据范围 max1=0; } } } printf("%I64d\n",tot); } return 0; }
相关文章推荐
- 关于就业指导和职业规划
- 第1章 Sass简介
- Hibernate工作原理及为什么要用?
- JQuery获取复选框checkbox的值
- VS2010调试程序启动时加载程序非常慢的
- B1010 一元多项式求导 (25)
- replace() MySQL批量替换指定字段字符串
- JPA和hibernate的关系
- 聚簇(或者叫做聚集,cluster)索引和非聚簇索引
- Windows Sockets 网络编程——第四章 网络应用程序工作机制
- 桶排序
- 新建的SQL Server账号无法使用跟踪功能
- java生成饼图svg
- 黑马程序员——ios学习笔记 OC self&异常
- Leetcode# 101 Symmetric Tree
- leetcode刷题,总结,记录,备忘 73
- hdu 2102
- 程序基本结构
- Codeforces Round #313 Currency System in Geraldion (枚举)
- 【80X86汇编语言学习】【字符串处理】字符串操作举例strcat