Codeforces 549C. The Game Of Parity[博弈論]

Candy?發表於2016-09-16
C. The Game Of Parity
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n cities in Westeros. The i-th city is inhabited by ai people. Daenerys and Stannis play the following game: in one single move, a player chooses a certain town and burns it to the ground. Thus all its residents, sadly, die. Stannis starts the game. The game ends when Westeros has exactly k cities left.

The prophecy says that if the total number of surviving residents is even, then Daenerys wins: Stannis gets beheaded, and Daenerys rises on the Iron Throne. If the total number of surviving residents is odd, Stannis wins and everything goes in the completely opposite way.

Lord Petyr Baelish wants to know which candidates to the throne he should support, and therefore he wonders, which one of them has a winning strategy. Answer to this question of Lord Baelish and maybe you will become the next Lord of Harrenholl.

Input

The first line contains two positive space-separated integers, n and k (1 ≤ k ≤ n ≤ 2·105) — the initial number of cities in Westeros and the number of cities at which the game ends. 

The second line contains n space-separated positive integers ai (1 ≤ ai ≤ 106), which represent the population of each city in Westeros.

Output

Print string "Daenerys" (without the quotes), if Daenerys wins and "Stannis" (without the quotes), if Stannis wins.

Examples
input
3 1
1 2 1
output
Stannis
input
3 1
2 2 1
output
Daenerys
input
6 3
5 20 12 7 14 101
output
Stannis
Note

In the first sample Stannis will use his move to burn a city with two people and Daenerys will be forced to burn a city with one resident. The only survivor city will have one resident left, that is, the total sum is odd, and thus Stannis wins.

In the second sample, if Stannis burns a city with two people, Daenerys burns the city with one resident, or vice versa. In any case, the last remaining city will be inhabited by two people, that is, the total sum is even, and hence Daenerys wins.


題意:背景是冰與火之歌哈哈哈哈哈

n座城人數有奇有偶,最後留下k座,偶的話我龍母win,奇的話史坦利斯win


 

很明顯的博弈論,然而一直沒有認真學過,想了想有點萌

官方題解
C. The Game Of Parity
Author: olpetOdessaONU
If n = k no moves may be done. The winner is determined with starting parity of citizens' number. Otherwise let's see that the player making the last move may guarantee his victory, if there are both odd and even cities when he makes the move. He just selects the city of which parity he should burn to obtain required parity. So, his enemy's target is to destroy all the cities of some one type. Sometimes the type does matter, sometimes doesn't. It depends on the player's name and the parity of k. So the problem's solution consists in checking if "non-last" player's moves number (n - k) / 2 is enough to destroy all the odd or even cities. If Stannis makes the last move and k is even, Daenerys should burn all the odd or all the even cities. If k is odd, Daenerys should burn all the odd cities. If Daenerys makes the last move and k is even, Stannis has no chance to win. If k is odd, Stannis should burn all the even cities.

奇+奇=偶,偶+偶=偶,奇+偶=奇

只要最後剩下奇和偶,那麼最後一個人一定贏,最後一次取可以改變奇偶也可以不改變

所以分情況討論另一個人能不能把某種奇偶性殺完行了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2e5+5;
ll read(){
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
ll n,k,a,sum,odd,even,lo,r[2],win;//0 D  1 S
void game(){
    if(n==k){if(sum%2==0) win=0;else win=1;return;}
    
    r[0]=r[1]=(n-k)/2;
    if((n-k)%2==0) lo=0;
    else lo=1,r[1]++;
    
    if(lo==1){
        win=1;
        if(k%2==0)
            if(r[0]>=odd||r[0]>=even){win=0;return;}
        if(k%2==1)
            if(r[0]>=odd){win=0;return;}
    }else{
        win=0;
        if(k%2==0) return;
        if(k%2==1)
            if(r[1]>=even){win=1;return;}
    }
}
int main(int argc, const char * argv[]) {
    n=read();k=read();
    for(int i=1;i<=n;i++){
        a=read(); sum+=a;
        if(a%2==0) even++;
        else odd++;
    }
    game();
    cout<<(win?"Stannis":"Daenerys");
    return 0;
}

 

相關文章