zoukankan      html  css  js  c++  java
  • URAL2104. Game with a Strip(博弈)

    There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2nn squares on each side) is painted in one of two colors (let’s call them A and B). Alice and Bob play a game. Alice makes the first turn. On each turn, a player can bend the strip in half with the fold passing on the divisions of the squares (i.e. the turn is possible only if the strip has an even length). Bending the strip can be done either inwards or outwards. If the strip has become completely one color after the next turn, then the winner is the player whose color is it (A refers to Alice, B to Bob). If the current player has no legal moves, but no one has won, the game ends with a draw.
    Who will win if both players play optimally? This means that each player tries to win; if it is not possible, to achieve a draw.

    Input

    The first line contains an integer n that is the length of the strip (1 ≤ n ≤ 5 · 105).
    The next two lines contain two strings of letters “A” and “B” with lengths n, describing two sides of the strip. The letters that are under each other, correspond to the different sides of the same square.

    Output

    If Alice wins, output “Alice”. If Bob wins, output “Bob”. If the game ends with a draw, output “Draw”.

    Samples

    inputoutput
    4
    BBAA
    BABB
    
    Bob
    
    3
    AAA
    BBB
    
    Draw
    
    2
    AA
    BB
    
    Alice
    

    Notes

    In the first example, Alice starts the game with the strip BBAA/BABB. After her turn she can get the strip BB/AA or BB/AB. In both cases, Bob can win by getting the strip B/B.
    In the second example, Alice can’t make even the first turn, so the result is a draw.
    In the third example, Alice wins by the first move, getting the stripe A/A from the strip AA/BB.
    Problem Author: Nikita Sivukhin
    Problem Source: Ural FU Junior Championship 2016

    题意:有初始长度为2*N的字符串,上面只有‘A’或者‘B’,每次可以沿对角线翻折,如果翻折后全部为‘A’,则Alice胜利;全部为‘B’则Bob胜利。如果翻折前字符串长度不为偶数则为平局。

    思路:翻折等效于覆盖:即[1,2N]翻折后本来应该是[N,1]或者[2*N,N+1];但是我们实际上不能模拟翻转操作(复杂度有点高,虽然好像NlogN也可以过)。这里,我们考虑翻折操作等效于覆盖操作后为[1,N]或者[N+1,2*N]。

                1,判定一个区间是否颜色全部为‘A’,我们用前缀和判定,如果suma[R]-sum[L-1]==R-L+1,则全为‘A’;  ‘B’同理。

                2,然后对于当前区间,我们考虑两种子情况:两种子情况都不利,则不利;有一个有利,则有利。 (博弈的思想)

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=2000000;
    char c[maxn]; int sum1[maxn],sum2[maxn];
    int judge(int L,int R,int opt)
    {     
        if((R-L+1)%2!=0) return 3;
        if(sum1[R]-sum1[L-1]==R-L+1) return 1; //全A 
        if(sum2[R]-sum2[L-1]==R-L+1) return 2; //全B 
        int Mid=(L+R)/2;
        int lson=judge(L,Mid,3-opt);
        int rson=judge(Mid+1,R,3-opt);
        if(lson==opt||rson==opt) return opt; //至少有一个必胜态 
        if(lson==rson&&lson==3-opt) return 3-opt; //全为必输态 
        return 3; // 平局
    }
    int main()
    {
        int N; scanf("%d",&N); 
        scanf("%s",c+1); scanf("%s",c+N+1);
        for(int i=1;i<=N+N;i++){
            sum1[i]=sum1[i-1]+(c[i]=='A'?1:0);
            sum2[i]=sum2[i-1]+(c[i]=='B'?1:0);
        }
        int ans=judge(1,2*N,1);
        if(ans==1) printf("Alice
    ");
        if(ans==2) printf("Bob
    ");
        if(ans==3) printf("Draw
    ");
        return 0;
    }
  • 相关阅读:
    windows下 php-cgi.exe 0xc000007b 错误 阿星小栈
    call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败 阿星小栈
    PHP json_encode返回的json前端获取时出现unicode转码和反斜杠导致无法解析的解决办法
    Warring:POST Content-Length of 625523488 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 阿星小栈
    PHP数组分割成新数组 阿星小栈
    Laravel ajax请求419错误及解决办法(CSRF验证) 阿星小栈
    MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded... 阿星小栈
    Laravel框架发送邮件 阿星小栈
    PHP 导出Excel三种方式 阿星小栈
    mysql命令 出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8857599.html
Copyright © 2011-2022 走看看