Codeforces 1119E Pavel and Triangles (贪心)
题目链接:
Pavel has several sticks with lengths equal to powers of two.
He has \(a_0\) sticks of length \(2^0=1\), \(a1\) 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.
Input
The first line contains a single integer \(n (1\le n\le 300000)\) — the number of different lengths of sticks.
The second line contains \(n\) integers \(a_0, a_1, ..., a_{n−1} (1\le a_i\le 10^9)\), where ai is the number of sticks with the length equal to \(2^i\).
Output
Print a single integer — the maximum possible number of non-degenerate triangles that Pavel can make.
Examples
input
5
1 2 2 2 2
output
3
input
3
1 1 1
output
0
input
3
3 3 3
output
3
Note
In the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): \((2^0,2^4,2^4), (2^1,2^3,2^3), (2^1,2^2,2^2)\).
In the second example, Pavel cannot make a single triangle.
In the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): \((2^0,2^0,2^0), (2^1,2^1,2^1), (2^2,2^2,2^2)\).
Solution
题意
给定 \(n\) 个数,第 \(i\) 个数 \(a[i]\) 表示长度为 \(2^i\) 的木棒的数量,求最多可以拼成多少个三角形。
题解
贪心
本来以为要 FFT 的,结果贪心就完事了。
构成三角形只有两种情况:等腰三角形和等边三角形。优先采用等边三角形,剩下的边去凑等腰三角形,贪心一下就可以了。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
ll ans = 0, k = 0;
for(int i = 0; i < n; ++i) {
ll a;
cin >> a;
if(k) {
ll t = min(k, a / 2);
a -= t * 2;
ans += t;
k -= t;
}
ans += a / 3;
k += a % 3;
}
cout << ans << endl;
return 0;
}
Codeforces 1119E Pavel and Triangles (贪心)的更多相关文章
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- E. Pavel and Triangles dp+问题转化
E. Pavel and Triangles dp+问题转化 题意 给出n种线段,每种线段给出一定数量,其中每个线段都是 \(2^k\) 问最多能组成多少个三角形 思路 因为每个是\(2^k\)所以能 ...
- Codeforces 1119E(贪心)
题目传送 贪心方法 按边从小到大扫,先凑3个,没凑足的记录一下数量,后面大的优先跟这些凑,俩带走一个,多余的再凑3个,再--就这样走到最后即可. const int maxn = 3e5 + 5; i ...
- Codeforces Global Round 2 E. Pavel and Triangles(思维+DP)
题目链接:https://codeforces.com/contest/1119/problem/E 题意:有n种长度的棍子,有a_i根2^i长度的棍子,问最多可以组成多少个三角形 题解:dp[i]表 ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
随机推荐
- 国内网络安装ubuntu软件慢的解决方法
以安装scikit-image为例: pip3 install scikit-image==0.13.0 -i https://pypi.tuna.tsinghua.edu.cn/simple 或者 ...
- php对接极光推送
首先要明白php对接极光推送要做些什么,极光推送的大致流程是:你的服务器----->极光服务器------>app端.而php要做的就只有我们的服务器到极光服务器这个过程.极光服务器到ap ...
- svn 版本管理,trunk(主干),branch(分支),merge(合并)
svn 版本管理,主要对trunk(主干).branch(分支).merge(合并)进行说明. svn作为一个常用的版本管理工具,一些基本操作必须要会,在这里整理一下自己使用svn的一些体会: svn ...
- 63、saleforce 的 Merchandise 的简单的增删改查
自定义的controller public with sharing class MerchandiseController { public List<Merchandise__c> m ...
- 指向自身类型的成员指针的初始化,this不属于类对象的一部分
有下面的一个简单的类: class CNullPointCall{public: static void Test1(); void Test2(); void Test3(int ...
- LNMP一键安装包+Thinkphp搭建基于pathinfo模式的路由
LNMP一键安装包是一个用Linux Shell编写的可以为CentOS/RadHat/Fedora.Debian/Ubuntu/Raspbian/Deepin VPS或独立主机安装LNMP(Ngin ...
- python-面向对象中的特殊方法 ,反射,与单例模式
二,面相对象的特殊成员及相关内置函数 2.1 isinstance与issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class A: pass cl ...
- sudo之后出现“unable to resolve host ××××”
对主机IP反解即可 打开 /etc/hosts 文件, 在 127.0.0.1 localhost 那行的后面添加你自己的主机名就好啦
- Spring项目下js文件无法引用
问题:最近练习spring-mvc框架的时候,引用了jQuery,但是运行的时候,jQuery做的事情都没有响应,代码也没有任何报错,在网上找了一下,发现是由于spring配制的是/,所有的资源都被屏 ...
- ES5新增方法(数组,字符串,对象)
一.数组方法 迭代(遍历)方法:forEach().map().filter().some().every(): 1. array. forEach(function(value,index,arr) ...