CodeForces - 1119E【 Pavel and Triangles 】做题总结(贪心)

Pavel and Triangles

Pavel has several sticks with lengths equal to powers of two.

He has a0 sticks of length 20=1, a1 sticks of length 21=2, …, an−1 sticks of length 2n−1.

Pavel wants to make the maximum possible number of triangles using these sticks. The triangles should have strictly positive area, each stick can be used in at most one triangle.

It is forbidden to break sticks, and each triangle should consist of exactly three sticks.

Find the maximum possible number of triangles.
Input

The first line contains a single integer n (1≤n≤300000) — the number of different lengths of sticks. The second line contains n integers a0, a1, …, an−1 (1≤ai≤109), where ai is the number of sticks with the length equal to 2i.

Output

Print a single integer — the maximum possible number of non-degenerate triangles that Pavel can make.

Examples

Input
5
1 2 2 2 2
Output
3
Input
3
1 1 1
Output
0
Input
3
3 3 3
Output
3

Note

In the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): (20,24,24), (21,23,23), (21,22,22).

In the second example, Pavel cannot make a single triangle.

In the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): (20,20,20), (21,21,21), (22,22,22)

题意:
帕维尔有一些木棒,他们的长度分别为2的i次方(i>=0&&i<=n-1)给你他们的数量,尽可能用这些木棒组成最多的三角形。
思路:
众所周知,三角形三边满足:a+b>c因此此题只有如下两种情况才能组成三角形。
1.两根长木棒和一根短木棒(第一类型
2.三根同长的木棒(第二类型
接着我们看如何组成才能保证答案最大。
当这根木棒的数量x=2时(假设存在比这根木棒小的木棒,x>=2)
只能组成一个第一类型的三角形。
x=3时,可以组成一个第一类型的三角形,或是一个第二类型的三角形。
x>3时可以组成n/3个第二类型的三角形,或是n/2个第一类型的三角形。明显看出n/2>n/3。因此先选第一类型的三角形为最优解。
最后写代码的时候再注意一下比当前木棍短的木棍的数量。
代码:

#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
int n,x;
int main()
{
	while(cin>>n)
	{
		int b=0,t=0;
		ll ans=0;
		for(int i=1;i<=n;i++)
		{
			cin>>x;
			t=x/2;
			if(b<=t)
			{
				t=b;
			}
			x-=2*t;
			ans+=t;
			b-=t;
			b+=x%3;//看是否能组成第二类型的三角形
			ans+=x/3;
		}
		cout<<ans<<endl;
	}
}

均为本人理解,如有纰漏欢迎指出。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值