CodeForces - 1119E Pavel and Triangles 贪心

题目链接:点击查看

题意 :有n种,2^0 2^1..2^n-1 种木棍,给出每种木棍的数目,求最多形成多少三角形

题解:首先构成三角形要么等边,要么等腰,等腰的话另外一边只能取小的,如果小的有剩余,当然拿出2个来构成等腰更划算,如果没有那么就本身构成等边

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[300100];
int n;
int main() {
	scanf("%d", &n);
	ll ans = 0, cnt = 0, tmp;
	for(int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		if(cnt) {
			tmp = min(a[i] / 2, cnt);
			ans += tmp;
			cnt -= tmp;
			a[i] -= tmp * 2;
		}
		ans += a[i] / 3;
		cnt += a[i] % 3;
	}
	printf("%lld\n", ans);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/89065919