[CF1119E] Pavel and Triangles

Description

给定 (n) 种木棍,第 (i+1) 种有 (a_i) 个,长度为 (2^i),求用这些木棍可以同时拼出多少个三角形(不可重复使用同一根)

Solution

容易发现三角形一定要满足 (ABB(Ale B)) 的结构,于是我们从大到小考虑所有依次打包成 (BB),如果有多余的就可以当做 (A) 与一个已有的 (BB) 匹配,答案 (+1);最后如果有没有匹配完的 (BB),则自己拆掉一些 (BB) 来匹配,设个数为 (c),则答案 (+[2c/3])

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

int n,a[N],c,ans;

signed main() {
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=n;i>=1;--i) {
        c+=a[i]/2;
        if(a[i]%2 && c>0) ++ans,--c;
    }
    cout<<ans+2*c/3;
}
原文地址:https://www.cnblogs.com/mollnn/p/12929635.html