题意:给你一个n,紧接着n个正数,然后有一种操作:选择一个x满足(x*2+1<=n)一次可以把下标为  x,2*x,2*x+1的三个数同时减一;

问,最少几次操作可以使n个数字变为零(已经是0的就不会再减1了)

这里一个坑点就是    即使下标为x的数已经是零了,你还是可以选择  x,并且 让x, 2*x , 2*x+1中不为零的减一;

这个题说要最少的次数使所有数字减为零,因为选择前面的x可以同时让后面的2*x,2*x+1都减少,所以从后面开始,先让后面的减为零,依次往前推减少的数量;

这样也是用的操作最少的,简而言之就是让一次操作,作用尽量多的下标;

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
int a[maxn];
int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
}
int m=(n-)/;
if(m<=||(m*+<n))
{
printf("-1\n");
return ;
}
ll sum=;
for(int i=n;i>;i-=)
{
if(a[i]!=||a[i-]!=)
{
sum+=max(a[i],a[i-]);
if(a[i>>]>=max(a[i],a[i-]))
a[i>>]-=max(a[i],a[i-]);
else a[i>>]=;
}
}
sum+=a[];
cout<<sum<<endl; return ;
}

CodeForces 245C-Game with Coins的更多相关文章

  1. Codeforces D. Sorting the Coins

    D. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...

  2. CodeForces - 876D Sorting the Coins

    题意:有n个数的序列,n个数都为0,每次指定某个数变为1,当序列中第i个数为1,第i+1个数为0时,这两个数可交换,将序列从头到尾进行一次交换记为1次,直到某一次从头到尾的交换中没有任何两个数交换.序 ...

  3. Codeforces Gym - 101102A - Coins

    A. Coins 题目链接:http://codeforces.com/gym/101102/problem/A time limit per test 3 seconds memory limit ...

  4. codeforces 876 D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...

  5. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  6. Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D 题意: 最开始有一串全部由"O"组成的字符串,现在给出n个数字,指的是每次把位置n上的&qu ...

  7. Codeforces Round #441 D. Sorting the Coins(模拟)

    http://codeforces.com/contest/876/problem/D 题意:题意真是难懂,就是给一串序列,第i次操作会在p[x](1<=x<=i)这些位置放上硬币,然后从 ...

  8. Codeforces Round #207 (Div. 1) D - Bags and Coins 构造 + bitset优化dp + 分段查找优化空间

    D - Bags and Coins 思路:我们可以这样构造,最大的那个肯定是作为以一个树根,所以我们只要找到一个序列a1 + a2 + a3 .... + ak 并且ak为 所有点中最大的那个,那么 ...

  9. Codeforces Round #523 (Div. 2) A. Coins

    A. Coins 题目链接:https://codeforc.es/contest/1061/problem/A 题意: 给出n和s,要在1-n中选数(可重复),问最少选多少数可以使其和为s. 题解: ...

  10. codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))

    题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...

随机推荐

  1. hdu3371 Connect the Cities (MST)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. luogu1273 有限电视网

    题目大意 有一棵有根树,每个结点有一个收益,每条边有一个花费.如果要选择一个叶子结点,则根节点到该叶子结点的路径上的所有结点都必须被选择.求当总收益大于等于总花费的情况下,最多能选择多少个叶子结点. ...

  3. user和userdebug区别

    user:不可以root userdebug:可以root

  4. hdu 2586(Tarjan 离线算法)

    How far away ?                                                                             Time Limi ...

  5. B1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 数学

    今天净做水题了,这个题还不到十五分钟就搞定了,思路特别简单,就是直接按照线性求因子个数的思路就行了. 题干: Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜 ...

  6. Balloons(DFS)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2248 题意:(1)求图中四连块(有公共边的方块 ...

  7. selenium3 + python - css定位

    一.css:属性定位 1.css可以通过元素的id.class.标签这三个常规属性直接定位到 2.如下是百度输入框的的html代码: <input id="kw" class ...

  8. 题解报告:hdu 1850 Being a Good Boy in Spring Festival(尼姆博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1850 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里 ...

  9. js 事件冒泡、事件捕获、stopPropagation、preventDefault

    转自:http://www.jb51.net/article/42492.htm (1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: ...

  10. 【VB】时间戳转日期

    DateAdd("s", TimeStamp / 1000, "1970-01-01 00:00:00")