Codeforces Round #573 (Div. 2) E. Tokitsukaze and Duel (博弈)

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
"Duel!"

Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.

There are n cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly k consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these n cards face the same direction after one's move, the one who takes this move will win.

Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes.

Input
The first line contains two integers n and k (1≤k≤n≤105).

The second line contains a single string of length n that only consists of 0 and 1, representing the situation of these n cards, where the color side of the i-th card faces up if the i-th character is 1, or otherwise, it faces down and the i-th character is 0.

Output
Print "once again" (without quotes) if the total number of their moves can exceed 109, which is considered a draw.

In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win.

Note that the output characters are case-sensitive, and any wrong spelling would be rejected.

Examples
inputCopy
4 2
0101
outputCopy
quailty
inputCopy
6 1
010101
outputCopy
once again
inputCopy
6 5
010101
outputCopy
tokitsukaze
inputCopy
4 1
0011
outputCopy
once again
Note
In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win.

In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw.

In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down.

The fourth example can be explained in the same way as the second example does.

题意:
给你一个长度为n的01字符串,和一个整数k。二人进行做博弈游戏,每个人必须选择一个连续的k个字符,把这连续的k个字符全变为0或者1

如果某次操作之后整个字符串全为1或者0,那么这个胜利,如果有无限多步要走,那么算平局,假设二人都绝顶聪明。给你初始局面,问游戏结果是什么?

思路:

首先应该明确,如果先手要赢,他一定要在第一步就赢,否则就不能再赢了。

后手要赢的话,他要在他走的第一步赢,不然也不能再赢了、因为另外一个人可以重复选择他刚刚选择的区间,使其不停的翻转,可以知道这样是一直循环下去而且无意义的。

为什么要这样呢?因为一个人如果不能赢,那么平局就是最优的决策,所以不能赢的人,会用这个方法使其平局。

然后来看什么情况下先手会一步赢?

选择一个连续的k个字符区间后,这个区间以外的字符全为1或者全为0,先手使其区间变为对应的字符就可以赢得。

我们可以通过前缀和 以及 枚举区间 进行判定先手是否能赢 ,时间复杂度是 O(n )

如果先手不能赢,接下来看后手能否一步赢?

能赢的条件如下:

1、2*k>=n

不然个人操作后无法覆盖整个字符串,先手不会让你赢的。

2、 k!=1
先手如果第一次操作没法赢,那就把字符保持原来的样子,把状态给你,你同样没法赢。

3、 因为2*k>=n 所以先手操作过之后,一定会有一个至少连续k个字符的区间是相同的。

    那么先手肯定不会从选首尾的,因为这样直接就送后手赢了。

    那么先手肯定会选择中间的k个连续字符,那么我们就枚举中间k个连续字符区间的左右的剩余区间(例如左边的是a区间,右边的是b区间。)

    我们判的是否a区间为都为0或者1,以及b。  因为先手决定聪明,所以他会尽量不让后手赢,所以就要枚举区间,判定没有一个情况下a和b不是合法的。

    以及:

     int len=n-k-1;

           if(a[len]==a[len+1]||a[n-len]==a[n-len+1]||a[1]==a[n])

                    后手也不可能赢。

先手后手都不能一步赢的话,
结果就一定是平局了。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int a[maxn];
int n,k;
bool check_sec()
{
    if(k*2<n||k==1)return false;
    int len=n-k-1;
    for(int i=2;i<=len;++i)
    {
        if(a[i-1]!=a[i-2]||a[n-i]!=a[n-i+1])return false;
    }
    if(a[len-1]==a[len]||a[n-len-1]==a[n-len]||a[0]==a[n-1])return false;
    return true;
}
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
	//freopen("D:\\common_text\code_stream\\out.txt","w",stdout);

	gbtb;
	cin>>n>>k;
	cin>>s;
	if(k>=n-1)
	{
		cout<<"tokitsukaze"<<endl;
	}else
	{
		for(int i=0;i<n;++i)
		{
			a[i]=s[i]-'0';
		}
		for(int i=1;i<n;i++)
		{
			a[i]+=a[i-1];
		}
		int isok=0;
		for(int i=k-1;i<n;i++)
		{
			if(a[n-1]-a[i]==0&&((i-k>=0&&a[i-k]==0)||(i-k<0)))
			{
				isok=1;
				break;
			}else if(a[n-1]-a[i]==n-1-i&&((i-k>=0&&a[i-k]==i-k+1)||(i-k<0)))
			{
				isok=1;
				break;
			}
		}
		if(isok)
		{
			cout<<"tokitsukaze"<<endl;
		}else
		{
            for(int i=0;i<n;++i)
            {
                a[i]=s[i]-'0';
            }
			if(check_sec())
			{
				cout<<"quailty"<<endl;
			}else
			{
				cout<<"once again"<<endl;
			}
		}
	}


    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}




posted @ 2019-07-13 17:12  茄子Min  阅读(413)  评论(0编辑  收藏  举报