E - Tokitsukaze and Duel CodeForces - 1190C (博弈 + 窗体移动)
"Duel!"
Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.
There are nn 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 kk 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 nn 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 nn and kk (1≤k≤n≤1051≤k≤n≤105).
The second line contains a single string of length nn that only consists of 00 and 11, representing the situation of these nn cards, where the color side of the ii-th card faces up if the ii-th character is 11, or otherwise, it faces down and the ii-th character is 00.
Output
Print "once again" (without quotes) if the total number of their moves can exceed 109109, 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
4 2
0101
quailty
6 1
010101
once again
6 5
010101
tokitsukaze
4 1
0011
once again 解题思路:先手和后手要么一出手就赢,要么就会平局,因为两个人都聪明绝顶(暗示脱发),都想赢,如果一出手赢不了,然后两个人就可以重复同一步操作,抵消对方带来的不好影响,因此导致平局
先手必赢条件:1.不用翻转就可以赢
2.翻转长度 k >= n ,那么只要翻转一次就可以赢
3.k < n ,但是只要一次翻转就可以赢
后手赢的条件:那就是先手第一次无论怎么翻转都不能赢,但是到了后手无论何种情况下都能赢
1.k!=1。因为如果先手赢不了,那他就相当于不动序列,留给后手,后手也赢不了
2.2*k>=n,因为后手一出手必须赢,不然先手就可以抵消后手对于序列的影响而导致平局。
3.2*k>=n的时候,那必定会存在一个区间k内的字符一定是相同的,考虑到先手(聪明绝顶,肯定不希望后手赢),那么我们只要让先手无论如何都会输就好了
首尾肯定是不会选的,毕竟先手聪明绝顶。
那么先手肯定会选择中间的k个连续字符,那么我们就枚举中间k个连续字符区间的左右的剩余区间(例如左边的是a区间,右边的是b区间。)我们判的是否a区间为都为0或者1,以及b。 因为先手决定聪明,所以他会尽量不让后手赢,所以就要枚举区间,判定没有一个情况下a和b不是合法的。
并且区间a和区间b也必须是不同的
注意:判断后手赢得条件时,如果暴力判断(n2)会超时,因此用到里“窗体移动”的方法(这个是我在网上看到的,应该是这个叫法),区间k不断向右移动,同时保证左区间a和右区间b也满足条件
要满足条件的话,去加入区间的元素,只要和它的上一位元素(或是下一位元素)比较就可以知道时候满足条件,这样的复杂度就降到了O(n),还有就是注意边界情况的处理。就是左区间或者右区间有且仅有1一个元素的时候,这个时候区间中没有东西可以进行比较,要满足的是区间a和区间b也必须是不同的。
#include<bits/stdc++.h>
using namespace std;
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
const int P = ;
const int inf = 0x3f3f3f3f;
const int maxn = ;
vector<int>one,zero;
int n,k;
int str[maxn];
char s;
int check1(){
if(one.size() == || zero.size() == || k >= n){
return ;
}
int lz = -,rz = -,lo = -,ro = -;
for(int i = ; i <= n ; i++){
if(lz == - && str[i] == ){
lz = i;
}
if(lo == - && str[i] == ){
lo = i;
}
if(lz != - && lo != -) break;
}
for(int i = n ; i >= ; i--){
if(rz == - && str[i] == ){
rz = i;
}
if(ro == - && str[i] == ){
ro = i;
}
if(rz != - && ro != -) break;
}
if(k >= min(ro - lo + ,rz - lz + )) return ;
return ;
}
int check2(){
if(k == ) return ;
if( * k < n) return ;
int len = n - k;
for(int i = ; i < len ; i++){
if(str[i - ] != str[i - ] || str[i + k] != str[i + k + ]) return ;
}
if(str[] == str[n] || str[] == str[k+] || str[n] == str[n - k - ]) return ;
return ;
} int main(){
gbtb;
cin>>n>>k;
for(int i = ; i <= n ; i++){
cin>>s; str[i] = s-'';
if(str[i] == ) one.push_back(i);
else zero.push_back(i);
}
if(check1()){
cout<<"tokitsukaze"<<endl;
}else if(check2()){
cout<<"quailty"<<endl;
}else{
cout<<"once again"<<endl;
}
return ;
}
一个从很久以前就开始做的梦。
E - Tokitsukaze and Duel CodeForces - 1190C (博弈 + 窗体移动)的更多相关文章
- Tokitsukaze and Duel CodeForces - 1191E (博弈论)
大意: 给定01串, 两人轮流操作, Tokitsukaze先手. 每次操作可以选择长为$k$的区间, 全部替换为$0$或$1$, 若替换后同色则赢. 求最后结果. 先判断第一步是否能直接赢, 不能的 ...
- Codeforces Round #573 (Div. 2) E. Tokitsukaze and Duel (博弈)
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 1190C. Tokitsukaze and Duel
传送门 注意到后手可以模仿先手的操作,那么如果一回合之内没法决定胜负则一定 $\text{once again!}$ 考虑如何判断一回合内能否决定胜负 首先如果最左边和最右的 $0$ 或 $1$ 距离 ...
- Codeforces 1190C Tokitsukaze and Duel game
题意:有一个长为n的01串,两个人轮流操作,每个人可以把某个长度为m的区间变成相同颜色,谁在操作后整个串颜色相同就赢了.问最后是谁赢?(有可能平局) 思路:容易发现,如果第一个人不能一击必胜,那么他就 ...
- Codeforces - 1191E - Tokitsukaze and Duel - 博弈论 - 尺取
https://codeforc.es/contest/1191/problem/E 参考自:http://www.mamicode.com/info-detail-2726030.html 和官方题 ...
- C. Tokitsukaze and Duel 前缀维护
枚举每一个连续的K的第一个位置,如果是先手胜利,那么前[1 , i-1 ]和[ i+k , n ]区间要么全是0,要么全是1 如果能够平局,那么肯定是[1,i-1],以及[ i+k , n]中有两种情 ...
- Codeforces 354B 博弈, DP,记忆化搜索
题意:现在有一个字符矩阵,从左上角出发,每个人交替选择一个字符.如果最后字符a数目大于字符b,那么第一个人获胜,否则b获胜,否则平均.现在双方都不放水,问最后结果是什么? 思路:这题需要注意,选择的字 ...
- Ticket Game CodeForces - 1215D 博弈题
题目描述 Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even ...
- Codeforces Round #573 (Div. 1)
Preface 军训终于结束了回来补一补之前的坑发现很多题目题意都忘记了 这场感觉难度适中,F由于智力不够所以弃了,E的话石乐志看了官方英文题解才发现自己已经胡了一大半就差实现了233 水平下降严重. ...
随机推荐
- BAT 五路internet负载均衡
一个网上下载的bat文件 也不记得从那里下载的了 记得似乎需要管理员权限运行 依稀记得测试有效 放在这里做个记录 @echo off echo. echo ╭─────────╮ echo ╭──── ...
- NIO组件Channel
基本介绍 NIO的通道类似于流, 但有些区别: 通道可以同时进行读写, 而流只能读或者只能写 通道可以实现异步读写数据 通道可以从缓冲区(Buffer)读数据, 也可以写数据到缓冲区 BIO中的str ...
- 开源免费的安卓投屏工具-Scrcpy
最近需要使用安卓投屏在桌面上操作,一开始使用Vysor,免费版画质无法直视,发现一个开源的工具,Scrcpy,貌似效果不错,但没有GUI,命令行安装,整起(Mac) 1.安装 homebrew: 通过 ...
- 吴裕雄--天生自然java开发常用类库学习笔记:线程操作案例——生产者与消费者
class Info{ // 定义信息类 private String name = "李兴华"; // 定义name属性 private String content = &qu ...
- java正则表达式校验密码必须是包含大小写字母、数字、特殊符号的8位以上组合
一.需求:密码必须是包含大写字母.小写字母.数字.特殊符号(不是字母,数字,下划线,汉字的字符)的8位以上组合 二.方案:利用正则表达式来校验 三.思路:排除法 1.排除大写字母.小写字母.数字.特殊 ...
- 自制spring中bean加载机制,仅做笔记自用
- P1040 有几个PAT
转跳点:
- springcloud--zuul(过滤器)
在zuul添加过滤器 新建类继承ZuulFilter类. public class MyFilter extends ZuulFilter{ //是否需要过滤 @Override public boo ...
- 一百零六、SAP的OOP面向对象编程,OO-ALV的简介
面向对象编程,如图 基本概念: 1.对象(Object)是一个现实实体的抽象.一个对象可被认为是一个把数据(属性)和程序(方法)封装在一起的实体,这个程序产生该对象的动作或对它接受到的外界信号的反应. ...
- 8. Redis 持久化对生产环境的灾难恢复的意义
1.故障发生的时候会怎么样2.如何应对故障的发生 很多同学,自己也看过一些redis的资料和书籍,当然可能也看过一些redis视频课程 所有的资料,其实都会讲解redis持久化,但是有个问题,我到目前 ...