题面

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;
}

原文地址:http://www.cnblogs.com/cancers/p/16930231.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性