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';
}
}
}

Codeforces Round #573 (Div. 2) E. Tokitsukaze and Duel (博弈)的更多相关文章

  1. Codeforces Round #573 (Div. 2) D. Tokitsukaze, CSL and Stone Game (博弈,思维)

    D. Tokitsukaze, CSL and Stone Game time limit per test1 second memory limit per test256 megabytes in ...

  2. Codeforces Round #573 (Div. 1) 差F

    Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...

  3. Codeforces Round #573 (Div. 1)

    Preface 军训终于结束了回来补一补之前的坑发现很多题目题意都忘记了 这场感觉难度适中,F由于智力不够所以弃了,E的话石乐志看了官方英文题解才发现自己已经胡了一大半就差实现了233 水平下降严重. ...

  4. Codeforces Round #573 (Div. 2)

    A:Tokitsukaze and Enhancement 当时看错条件了..以为A>C>B>D.就胡写了判断条件. #include<bits/stdc++.h> us ...

  5. Codeforces Round #573 (Div. 2) Tokitsukaze and Mahjong 水题

    B. Tokitsukaze and Mahjong time limit per test1 second memory limit per test256 megabytes Tokitsukaz ...

  6. Codeforces Round 573 (Div.1) 题解

    这场怎么说呢……有喜有悲吧. 开场先秒了 A.看到 B,感觉有点意思,WA 了 2 发后也过了. 此时还在 rk 前 200. 开 C,一看就不可做.跟榜,切 D 人数是 C 的两倍. 开 D.一眼感 ...

  7. Codeforces Round #573 (Div. 2) D题题解

    一.题目 ​ Tokitsukaze, CSL and Stone Game ​ Tokitsukaze和CSL正在玩一些石头游戏. ​ 一开始,有n堆的石头,第i堆石头数记为 \(a_i\),两人轮 ...

  8. Codeforces Round #281 (Div. 2) D. Vasya and Chess 博弈

    D. Vasya and Chess   Vasya decided to learn to play chess. Classic chess doesn't seem interesting to ...

  9. Codeforces Round #668 (Div. 2) D. Tree Tag 题解(博弈)

    题目链接 题目大意 给你一颗树,Alice在a点,Bob在b点,Alice最多走da步,Bob最多走db步,两人轮流走路.要你判断经过无数次追赶后,Alice是否可以追上Bob,两人进行的都是最优策略 ...

随机推荐

  1. 《Effective Java》读书笔记 - 2.创建和销毁对象

    Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of construc ...

  2. js实现两个从input获取到的数字相加引发的问题

    从input中获取到的数据是文本类型的,如果不转化类型直接相加会变成字符串的相加. 使用Number()函数可以解决这个问题,如下 var c = Number(a) + Number(b)

  3. 如果将get请求转换成post请求

    td><a href="emp/${emp.id}">Edit</a></td> <form action="" ...

  4. SpringMvc中ModelAndView模型的应用

    /** * 目标方法的返回值可以是 ModelAndView 类型. * 其中可以包含视图和模型信息 * SpringMVC 会把 ModelAndView 的 model 中数据放入到 reques ...

  5. 十五、RF操作时间控件

    由于日期控件经常用的是readonly属性,这个属性意思是此控件为可读,明白点就是只让你看,不让你动. 解决方法就是:用js去掉这个属性,就可写了,就能输入了 导入库:DateTime #方式一 op ...

  6. 字符串在PHP比较运算中的变化

    由于PHP的弱类型性质,你可以做一些奇怪的事情,其中​​一些是好的,其中一些将使你掉到坑里面去.比如: ;   if ( $a == true && $b == false & ...

  7. python - DBUtils 连接池减少oracle数据库的连接数

    问题: 接到需求,告知项目的oracle连接次数过多,对系统造成太过大的负担,要求减少oracle数据库的连接次数 分析: 仔细分析代码以后,发现产生问题的原因,在于之前要求提升oracle监控的监控 ...

  8. gunicorn+nginx配置方法

    对于gunicorn+nginx的配置,理解他们之间的关系很重要,以及最后如何确认配置结果是正确的也很重要 nginx 配置文件: 修改这个配置文件有3个用处: 假设服务器本身的Ip是A称为ip-A, ...

  9. 设计模式(2): 响应store中数据的变化

    概述 最近最近做项目的时候总会思考一些大的应用设计模式相关的问题,我把自己的思考记录下来,供以后开发时参考,相信对其他人也有用. store里面响应数据变化 通常情况下,我们会把数据存在store里面 ...

  10. Java面试题集(131-135)

    Java程序员面试题集(131-135) 摘要:这部分内容准备重新发布为Java程序员面试题集(151-180),但这篇帖子仍然保留在这里.查看新内容请点击Java程序员面试题集(151-180) 1 ...