Codeforces 1119E Pavel and Triangles (贪心)

Codeforces Global Round 2

题目链接:

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

原文地址:https://www.cnblogs.com/wulitaotao/p/11600685.html

时间: 2024-08-23 01:34:21

Codeforces 1119E Pavel and Triangles (贪心)的相关文章

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

Codeforces 1119E(贪心)

题目传送 贪心方法 按边从小到大扫,先凑3个,没凑足的记录一下数量,后面大的优先跟这些凑,俩带走一个,多余的再凑3个,再--就这样走到最后即可. const int maxn = 3e5 + 5; int n; ll ans, last; int main() { read(n); rep(i, 1, n) { int a; read(a); if (a >= last * 2) a -= last * 2, ans += last, last = 0; else ans += a / 2, l

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1

codeforces 343C Read Time 二分 + 贪心

http://codeforces.com/problemset/problem/343/C 题意: 有一些磁头,给出了起始的位置,给出了一些磁盘中需要访问的地点.求这些磁头移动的最小的次数. 思路: 二分找出满足要求的最小的时间,对于当前尝试的时间进行贪心判断是否可用.对于贪心,因为每一个磁头都需要读到还没有人读过的最左边的地方.如果这个都不能满足,那么这个时间是不可以的. 在这基础上,对于同样的这么多时间,尽可能的让这个磁头读到能读到的最右边,这样就可以将还没有读过的地方尽可能的往右推. 如

Codeforces 402D Upgrading Array:贪心 + 数学

题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中p是s的最小质因子: f(1) = 0 f(s) = f(s/p) + 1 (p不是坏质数) f(s) = f(s/p) - 1 (p是坏质数) 你可以任意次数地进行操作:给a[1 to i]的每个数都除以gcd(a[1 to i]). 问你 ∑ f(a[i)最大为多少. 题解: 函数f(s)的实际

cf-Global Round2-E. Pavel and Triangles

题目链接:http://codeforces.com/contest/1119/problem/E 题意:给定n个数a[i],分别表示长度为2i-1的木条的数量,问使用这些木条最多能构成多少三角形. 思路:简单分析后可以发现能构成三角形的组合只能是(i,j,j) (i<=j).利用贪心的思想,从j=0到n-1依次处理,处理j时,优先考虑(i,j,j) (i<j)的情况,然后考虑(j,j,j)的情况. AC代码: 1 #include<bits/stdc++.h> 2 using n