• 245C Game with Coins


    C. Game with Coins
    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, x, 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.


      本题贪心,关键在于要从x最大的开始取,因为此为必取。

    #include<cstdio>
    int a[110];
    int ans;
    
    int main() {
        int n, i, temp;
        scanf("%d", &n);
        for (i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
        }
        if (n <= 2 || (n % 2 == 0 && a[n - 1])) {
            printf("-1\n");
            return 0;
        }
        if(n%2==0)
            n--;
        for (i = (n - 1) / 2; i > 0; --i) {
            temp = a[2 * i + 1] > a[2 * i] ? a[2 * i + 1] : a[2 * i];
            a[2 * i + 1] -= temp;
            a[2 * i] -= temp;
            a[i] -= temp;
            a[2 * i + 1] = a[2 * i + 1] < 0 ? 0 : a[2 * i + 1];
            a[2 * i] = a[2 * i] < 0 ? 0 : a[2 * i];
            a[i] = a[i] < 0 ? 0 : a[i];
            ans+=temp;
        }
        ans+=a[1];
        printf("%d\n",ans);
        return 0;
    }
  • 相关阅读:
    Hive 显示指定mapjoin,写法比较独特
    【Task】MySQL查看表状态命令 以及MYSQL ROW-FORMAT了解一下
    Java Map中key用可变对象会是什么后果?
    typora比较好用的markdown文档编辑器
    Sharding-JDBC是一个开源的适用于微服务的分布式数据访问基础类库,了解学习一下
    POJO与JavaBean的区别
    XML概述
    linux---安装apache
    linux基础命令练习题
    nfs网络共享和linux的进程管理
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2781368.html
Copyright © 2020-2023  润新知