题意:给你一个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. HDU 4519

    实现简单,但不得不说是一道好题. 当员工数少于医生数时,直接输出K,因为此时N个员工同时检查,必定是最少的时间了. 当员工数大于医生数时,可以把员工的项目看成一段一段的,每个医生对其进行切割,总能得到 ...

  2. Resources.Theme

    public final class Resources.Theme extends Object java.lang.Object    ↳ android.content.res.Resource ...

  3. luogu2278 [HNOI2003]操作系统

    题目大意 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高.如果一个进程到达的时候 ...

  4. 2749: [HAOI2012]外星人

    首先像我一样把柿子画出来或者看下hint 你就会发现其实是多了个p-1这样的东东 然后除非是2他们都是偶数,而2就直接到0了 算一下2出现的次数就好 #include<cstdio> #i ...

  5. Expression Trees (C# and Visual Basic)

    https://msdn.microsoft.com/en-us/library/bb397951.aspx Expression trees represent code in a tree-lik ...

  6. string[][]和string[,] 以及 int[][]和int[,]

    string[][]和string[,] http://www.codewars.com/kata/56f3a1e899b386da78000732/train/csharp Write a func ...

  7. 687C

    dp 以前做过 忘了. 想破脑袋不知道怎么设状态 dp[i][j][k]表示选到第i个硬币,当前和为j,能否弄出k dp[i][j][k]|=dp[i-1][j][k]|dp[i-1][j][k-c[ ...

  8. ks shell OpenStack 封装

  9. 84. ExtJS下页面显示中文乱码问题

    转自:https://blog.csdn.net/wenminhao/article/details/51198981 最近在学校extjs是,使用js脚本显示中文在html页面中时,中午出现了乱码的 ...

  10. hdu4405Aeroplane chess(概率与期望dp)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...