codeforces 245C Game with Coins

4559 단어 탐욕스럽다
Description 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 Input Input 1 1 Output -1 Input 3 1 2 3 Output 3 Hint 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.
제목: 번호 1-n의 상자 한 무더기, 매번 세 개의 상자를 찾습니다. 번호는 n, 2*n, 2*n-1입니다. 그중의 은화 한 개를 꺼내서 최소한 몇 번을 찾으면 안에 있는 돈을 다 찾을 수 있는지 물어보세요.상자가 찾을 수 없을 때(예를 들어 n이 3보다 작거나 짝수) 마이너스 1을 출력한다.
방법: 욕심 전략.먼 곳의 상자를 찾을 기회가 가장 적고 한 번일 수도 있으며, 먼 곳의 상자를 갈 때 가까운 상자도 수익을 따라갈 수 있기 때문에 n번째 상자에서 역추한다.
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
int main()
{
    int n;
    int a[1005];
    while(scanf("%d",&n)==1)
    {
        int i;
        for(i=1;i<=n;i++) scanf("%d",&a[i]);
        if(n<3||n%2==0) printf("-1
"
); else { int cnt=0; for(i=n;i>=1;i--) { while(a[i]>0) { a[i]--; if(i%2==1) a[i-1]--; //2*i+1 else a[i+1]--; //2*i a[i/2]--; cnt++; } } printf("%d
"
,cnt); } } return 0; }

좋은 웹페이지 즐겨찾기