博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Codeforces Round #452 (Div. 2)] Splitting in Teams
阅读量:6614 次
发布时间:2019-06-24

本文共 2065 字,大约阅读时间需要 6 分钟。

There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.

The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can’t use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.

Input

The first line contains single integer n (2n2105) — the number of groups.
The second line contains a sequence of integers a1, a2, …, an (1ai2), where ai is the number of people in group i.

Output

Print the maximum number of teams of three people the coach can form.

Examples

Input
4
1 1 2 1

Output

1

Input

2
2 2

Output

0

Input

7
2 2 2 1 1 1 1

Output

3

Input

3
1 1 1

Output

1

Note

In the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.
In the second example he can’t make a single team.
In the third example the coach can form three teams. For example, he can do this in the following way:
The first group (of two people) and the seventh group (of one person),
The second group (of two people) and the sixth group (of one person),
The third group (of two people) and the fourth group (of one person).

题目大意

就是给你一堆2和一堆1,问最多能凑出几个3

思路

如果有一个2和一个1,那么把他们选出来凑3。直到没有一个2或没有一个1为止。
如果还有剩余的1,那么选31出来凑3,知道选不出31为止。
这样的方法是最优的,因为凑3的方案只有这两种,而第一种需要用两个数字,而且涉及到了2,而第二种则需要用三个数字,并且没有涉及到2,所以优先考虑第一种方案。

代码

#include 
const int maxn=200000;int a,b,n;int main(){ scanf("%d",&n); for(register int i=1; i<=n; ++i) { int x; scanf("%d",&x); if(x==1) { ++a; } else { ++b; } } if(a<=b) { printf("%d\n",a); } else { printf("%d\n",b+(a-b)/3); } return 0;}

转载于:https://www.cnblogs.com/Canopus-wym/p/10376266.html

你可能感兴趣的文章
MVC自定义路由的配置,必须把自己的路由写在前面
查看>>
[翻译]Java Swing(1)
查看>>
基于suse linux系统的cacti系统部署——rpm包方式
查看>>
解密jQuery内核 DOM操作的核心buildFragment
查看>>
重建索引提高SQL Server性能<转>
查看>>
大公司的流量变现
查看>>
Linux进程管理(2)
查看>>
将eclipse中项目的Text File Encoding设置成为GBK
查看>>
对control file的学习笔记
查看>>
JavaScript与有限状态机
查看>>
Sharepoint 2010 以及Office 2010 RTM
查看>>
php优化
查看>>
jQuery之each方法
查看>>
RequireJS源码初探
查看>>
【hibernate】 hibernate的主键策略
查看>>
单表代替密码原理及算法实现
查看>>
如何让VS检查函数和类Comment的添加情况
查看>>
Linq案例
查看>>
23.3. 身份证校验
查看>>
web开发未解之谜
查看>>