题面
Pavel and Triangles
题面翻译
给定n种木棍,第i+1种有ai个,长度为2^i,求用这些木棍可以同时拼出多少个三角形(不可重复使用同一根)
输入第一行n,第二行n个整数a0,a1,a2...an−1
n≤3∗105,0≤ai≤109
输出一个整数表示个数
题目描述
Pavel has several sticks with lengths equal to powers of two.
He has $ a_0 $ sticks of length $ 2^0 = 1 $ , $ a_1 $ 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.
分析
易知每一个三角形应该都是由两条相同的边搭配另一条任意边
那么每两条相同的边编为 一组
直接统计组数,如果a[i]为奇数,那么多的一条边作为一条任意边即可
统计完后,没有任意边搭配的组,互相组成就可以了
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#define ll long long
#define rg register
using namespace std;
inline int read(){
rg int x = 0, f = 1;
rg char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * f;
}
const int N = 3e5 + 1;
int n;
ll ans, cnt;
int a[N];
inline void init(){
n = read();
for (int i(1); i <= n; i++) a[i] = read();
}
inline void doit(){
for (int i(n); i >= 1; i--){
cnt += a[i] / 2;
if (a[i] % 2 && cnt > 0) ++ans, --cnt;
}
printf("%lld\n", ans + 2 * cnt / 3);
}
int main(){
init();
doit();
return 0;
}
标签:cnt,sticks,题解,CF1119E,rg,Pavel,include,Triangles
From: https://www.cnblogs.com/cancers/p/16930231.html