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 sticks of length , sticks of length , ..., sticks of length .
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】