Codeforces 1119E Pavel and Triangles (贪心)
Posted wulitaotao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1119E Pavel and Triangles (贪心)相关的知识,希望对你有一定的参考价值。
题目链接:
Pavel has several sticks with lengths equal to powers of two.
He has (a_0) sticks of length (2^0=1), (a1) sticks of length (2^1=2), ..., (a_{n?1}) sticks of length (2^{n?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 (1le nle 300000)) — the number of different lengths of sticks.
The second line contains (n) integers (a_0, a_1, ..., a_{n?1} (1le a_ile 10^9)), where ai is the number of sticks with the length equal to (2^i).
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): ((2^0,2^4,2^4), (2^1,2^3,2^3), (2^1,2^2,2^2)).
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): ((2^0,2^0,2^0), (2^1,2^1,2^1), (2^2,2^2,2^2)).
Solution
题意
给定 (n) 个数,第 (i) 个数 (a[i]) 表示长度为 (2^i) 的木棒的数量,求最多可以拼成多少个三角形。
题解
贪心
本来以为要 FFT 的,结果贪心就完事了。
构成三角形只有两种情况:等腰三角形和等边三角形。优先采用等边三角形,剩下的边去凑等腰三角形,贪心一下就可以了。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
ll ans = 0, k = 0;
for(int i = 0; i < n; ++i) {
ll a;
cin >> a;
if(k) {
ll t = min(k, a / 2);
a -= t * 2;
ans += t;
k -= t;
}
ans += a / 3;
k += a % 3;
}
cout << ans << endl;
return 0;
}
以上是关于Codeforces 1119E Pavel and Triangles (贪心)的主要内容,如果未能解决你的问题,请参考以下文章
cf-Global Round2-E. Pavel and Triangles
Pavel and barbecue - CF756A DFS 思维