CF1119E Pavel and Triangles 题解

题面

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 a0 sticks of length 20=1 , a1 sticks of length 21=2 , ..., an1 sticks of length 2n1 .

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;
}
posted @   ancer  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示