CodeForces 245C-Game with Coins
题意:给你一个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的更多相关文章
- Codeforces D. Sorting the Coins
D. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...
- CodeForces - 876D Sorting the Coins
题意:有n个数的序列,n个数都为0,每次指定某个数变为1,当序列中第i个数为1,第i+1个数为0时,这两个数可交换,将序列从头到尾进行一次交换记为1次,直到某一次从头到尾的交换中没有任何两个数交换.序 ...
- Codeforces Gym - 101102A - Coins
A. Coins 题目链接:http://codeforces.com/gym/101102/problem/A time limit per test 3 seconds memory limit ...
- codeforces 876 D. Sorting the Coins
http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...
- 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 ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins
http://codeforces.com/contest/876/problem/D 题意: 最开始有一串全部由"O"组成的字符串,现在给出n个数字,指的是每次把位置n上的&qu ...
- Codeforces Round #441 D. Sorting the Coins(模拟)
http://codeforces.com/contest/876/problem/D 题意:题意真是难懂,就是给一串序列,第i次操作会在p[x](1<=x<=i)这些位置放上硬币,然后从 ...
- Codeforces Round #207 (Div. 1) D - Bags and Coins 构造 + bitset优化dp + 分段查找优化空间
D - Bags and Coins 思路:我们可以这样构造,最大的那个肯定是作为以一个树根,所以我们只要找到一个序列a1 + a2 + a3 .... + ak 并且ak为 所有点中最大的那个,那么 ...
- Codeforces Round #523 (Div. 2) A. Coins
A. Coins 题目链接:https://codeforc.es/contest/1061/problem/A 题意: 给出n和s,要在1-n中选数(可重复),问最少选多少数可以使其和为s. 题解: ...
- codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))
题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...
随机推荐
- HDU 4508
祼的完全背包问题 #include <iostream> #include <cstdio> #include <cstring> #include <alg ...
- xpath元素查找提示is not clickable
1.用xpath可以在chrome找到 $x("//mandatory-config-dialog[@is-show='isShowMandatoryConfig']/div/div[2]/ ...
- monitor weblogic server ,Very simple to use, weblogic监控、巡检、故障简单小工具
1. 开发了一个简单的监视weblogic执行情况的小程序.各位朋友下载下来试试,不用登陆console就能够知道server的执行状况,包含了jvm.线程.jdbc.状态jms等:另一个更简 ...
- Windowns 无法启动 Office Software Protection Platform 服务,系统找不到指定的文件
导致该服务无法启动的原因是,用kms8激活了win7后又用oem8激活试了下,结果就这样,然后就无法激活了,状态ID都不可用.试过禁用计划任务项目,重建MBR,重建PBR,都无效果.最后在这里找到了解 ...
- 新手对ASP.NET MVC的疑惑
习惯了多年的WEB FORM开发方式,突然转向MVC,一下子懵了,晕头转向,好多不习惯,好多不明白,直到现在也没弄明白,只好先记下来,在应用中一一求解. 主要集中在视图(View)这里. 1.@Htm ...
- Android中关闭DatePicker、TimePicker、NumberPicker的可编辑模式
DatePicker.TimePicker.NumberPicker这三个控件在使用的过程中,用户点击数字会弹出键盘,有时候会造成布局被挤压不好看,也有其他的需求. 我看了网上很多文章的解决办法都无效 ...
- hdu 1874(最短路 Dilkstra +优先队列优化+spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Django day07 (二)单表操作
单表操作 -mysql数据库:settings里配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME ...
- ubuntu Ngin Install
安装gcc g++的依赖库 #apt-get install build-essential #apt-get install libtool 安装 pcre依赖库 #sudo apt-get upd ...
- NoSQL概念
NoSQL是非关系型数据库,即not only sql,key/value键值对存储. 现有Nosql DB产品:Redis/MongoDB/Memcached等等. SQL Sever是关系型数据库 ...