CodeForces 245C Game with Coins
2015-08-14 16:23
666 查看
Game with Coins
http://codeforces.com/problemset/problem/245/C
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Two pirates Polycarpus and Vasily play a very interesting game. They have
n chests with coins, the chests are numbered with integers from 1 to
n. Chest number i has
ai coins.
Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer
x (2·x + 1 ≤ n) and take a coin from each chest with numbers
x, 2·x,
2·x + 1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.
Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his
moves, he also counts Vasily's moves.
Input
The first line contains a single integer n
(1 ≤ n ≤ 100) — the number of chests with coins. The second line contains a sequence of space-separated integers:
a1, a2, ..., an
(1 ≤ ai ≤ 1000), where
ai is the number of coins in the chest number
i at the beginning of the game.
Output
Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.
Sample test(s)
Input
Output
Input
Output
Note
In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.
In the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest.
//这个题目重要的是在删除的时候要有最少重叠
//逆向法总是被忘记呢
//反正最远方的一定要去掉,最远方的只有一次机会遇到,找遇到机会最少的
//这道题就是类似于在一个图上进行最小覆盖,点就是整数点,每次走三步。为了在最后少花力气,就从远方走,这样
//可以使近点获得最多的附加。远点可以尽快归0
每次将最右边的两次先去了,所以删除的次数count=max(a[i],a[i-1]
http://codeforces.com/problemset/problem/245/C
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Two pirates Polycarpus and Vasily play a very interesting game. They have
n chests with coins, the chests are numbered with integers from 1 to
n. Chest number i has
ai coins.
Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer
x (2·x + 1 ≤ n) and take a coin from each chest with numbers
x, 2·x,
2·x + 1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.
Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his
moves, he also counts Vasily's moves.
Input
The first line contains a single integer n
(1 ≤ n ≤ 100) — the number of chests with coins. The second line contains a sequence of space-separated integers:
a1, a2, ..., an
(1 ≤ ai ≤ 1000), where
ai is the number of coins in the chest number
i at the beginning of the game.
Output
Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.
Sample test(s)
Input
1 1
Output
-1
Input
3 1 2 3
Output
3
Note
In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.
In the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest.
//这个题目重要的是在删除的时候要有最少重叠
//逆向法总是被忘记呢
//反正最远方的一定要去掉,最远方的只有一次机会遇到,找遇到机会最少的
//这道题就是类似于在一个图上进行最小覆盖,点就是整数点,每次走三步。为了在最后少花力气,就从远方走,这样
//可以使近点获得最多的附加。远点可以尽快归0
每次将最右边的两次先去了,所以删除的次数count=max(a[i],a[i-1]
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=105; int a[maxn]; int main() { int n; while(cin>>n) { memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) cin>>a[i]; if(n==1 || n==2 || (n&1)==0){ printf("-1\n"); continue; } int Count=0; for(int i=n;i>1;i-=2) { if(a[i]!=0 || a[i-1]!=0){ Count+=max(a[i],a[i-1]); if(a[i>>1]>=max(a[i],a[i-1])) a[i>>1]-=max(a[i],a[i-1]); else a[i>>1]=0; } } Count +=a[1]; printf("%d\n",Count); } return 0; }
相关文章推荐
- Apache Shiro 使用手册(一)Shiro架构介绍 - kdboy - ITeye技术网站
- excel 补全所有空格
- 递归反转一个栈
- OS X Lion版 如果我忘记了我的账户密码 我该怎么办?
- MySQL 5.6 中 TIMESTAMP有那些变化
- 如何实现多线程下载文件
- Android中定时器的3种实现方法
- BZOJ1024
- hdu oj 1849 Rabbit and Grass
- 定时器
- 获取编译学习笔记 (十三)—— 外部中断
- asp.net mvc邮箱激活
- 有哪几种情况只能用intialization list 而不能用assignment
- net view
- Android国际化
- 关于蓝牙
- 《转》QT界面怎么使控件随窗口大小变化一直居中显示
- 基于Servlet、JSP、JDBC、MySQL登录模块(包括使用的过滤器和配置)
- GitHub指南
- SQL Server多表查询优化方案集锦