当前位置: 首页 > news >正文

【逆序对 博弈】P10737 [SEERC 2020] Reverse Game|普及+

本文涉及知识点

【C++】树状数组的使用、原理、封装类、样例
博弈 逆序对

P10737 [SEERC 2020] Reverse Game

题目描述

Alice 和 Bob 在玩一个游戏,规则如下:

  • 游戏开始前给定一个字符串 sss
  • 每次行动,选择 sss 的一个子串 tttttt 只能是 101001101010 中的一个,反转 ttt 的每个字符,例如 100 翻转为 001
  • 不能操作者输掉游戏。
    Alice 先手,问双方同时采取最优策略的情况下,谁能赢。

输入格式

一个仅包含 010101 的字符串 s(1≤∣s∣≤106)s\ (1 \leq |s| \leq 10^6)s (1s106)

输出格式

如果 Alice 胜利输出 Alice,Bob 胜利输出 Bob

输入输出样例 #1

输入 #1

010

输出 #1

Alice

输入输出样例 #2

输入 #2

1111

输出 #2

Bob

输入输出样例 #3

输入 #3

1010

输出 #3

Bob

输入输出样例 #4

输入 #4

1010001011001

输出 #4

Alice

说明/提示

对于样例 111,选择子串 10 进行反转后 Bob 无法操作。

逆序对

性质一:操作一减少一个逆序对。操作二三四减少2个逆序对。
性质三:如果只有两个逆序,这两个逆序对之间不会有其它数字。
10X1010X1010X10,X无论是0还是1,都会产生新的逆序对。
性质四:只有两个逆序对的情况:
4个字符:101010101010
三个字符:100、110100、110100110
000、001、011、111000、001、011、111000001011111无逆序 010、101010、101010101一个逆序对
即:如果有两个逆序,通过操作二三四,可以一次消除。
0逆序对:先手方失败。
1个2个逆序对:先手方胜利。
3个逆序对:先手方失败。
4个先手方胜利,选取1个。
5个先手胜利,选取2个。
6个逆序,无论选取一个还是2个,都是失败。
结论:逆序对3的倍数,先手方失败,其它先手方胜利。
3×i3\times i3×i失败,3×i+13\times i+13×i+13×i+23\times i+23×i+2胜利。
用数学归纳法证明,i==0i==0i==0已证
3×i+13\times i+13×i+1 选取1,后手方变成3×i3\times i3×i必败
3×i+23\times i+23×i+2 类似。
3×i+33\times i+33×i+3,无论选取1还是2,后手方必胜。
逆序对可以用归并排序或逆序对。本题只有0和1。
b[i] = a[i…]中0的数量。
∑(b[i]∗a[i])\sum(b[i]*a[i])(b[i]a[i])就是逆序对的数量。

代码

核心代码

#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>#include <bitset>
using namespace std;template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {in >> pr.first >> pr.second;return in;
}template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t);return in;
}template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);return in;
}template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6, T7>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);return in;
}template<class T = int>
vector<T> Read() {int n;cin >> n;vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}
template<class T = int>
vector<T> ReadNotNum() {vector<T> ret;T tmp;while (cin >> tmp) {ret.emplace_back(tmp);if ('\n' == cin.get()) { break; }}return ret;
}template<class T = int>
vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}template<int N = 1'000'000>
class COutBuff
{
public:COutBuff() {m_p = puffer;}template<class T>void write(T x) {int num[28], sp = 0;if (x < 0)*m_p++ = '-', x = -x;if (!x)*m_p++ = 48;while (x)num[++sp] = x % 10, x /= 10;while (sp)*m_p++ = num[sp--] + 48;AuotToFile();}void writestr(const char* sz) {strcpy(m_p, sz);m_p += strlen(sz);AuotToFile();}inline void write(char ch){*m_p++ = ch;AuotToFile();}inline void ToFile() {fwrite(puffer, 1, m_p - puffer, stdout);m_p = puffer;}~COutBuff() {ToFile();}
private:inline void AuotToFile() {if (m_p - puffer > N - 100) {ToFile();}}char  puffer[N], * m_p;
};template<int N = 1'000'000>
class CInBuff
{
public:inline CInBuff() {}inline CInBuff<N>& operator>>(char& ch) {FileToBuf();while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车ch = *S++;return *this;}inline CInBuff<N>& operator>>(int& val) {FileToBuf();int x(0), f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行		return *this;}inline CInBuff& operator>>(long long& val) {FileToBuf();long long x(0); int f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行return *this;}template<class T1, class T2>inline CInBuff& operator>>(pair<T1, T2>& val) {*this >> val.first >> val.second;return *this;}template<class T1, class T2, class T3>inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val);return *this;}template<class T1, class T2, class T3, class T4>inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);return *this;}template<class T = int>inline CInBuff& operator>>(vector<T>& val) {int n;*this >> n;val.resize(n);for (int i = 0; i < n; i++) {*this >> val[i];}return *this;}template<class T = int>vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {*this >> ret[i];}return ret;}template<class T = int>vector<T> Read() {vector<T> ret;*this >> ret;return ret;}
private:inline void FileToBuf() {const int canRead = m_iWritePos - (S - buffer);if (canRead >= 100) { return; }if (m_bFinish) { return; }for (int i = 0; i < canRead; i++){buffer[i] = S[i];//memcpy出错			}m_iWritePos = canRead;buffer[m_iWritePos] = 0;S = buffer;int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);if (readCnt <= 0) { m_bFinish = true; return; }m_iWritePos += readCnt;buffer[m_iWritePos] = 0;S = buffer;}int m_iWritePos = 0; bool m_bFinish = false;char buffer[N + 10], * S = buffer;
};int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUG	ios::sync_with_stdio(0); cin.tie(nullptr);//CInBuff<> in; COutBuff<10'000'000> ob;string str;cin >> str;
#ifdef _DEBUG	//printf("N=%d", N);//Out(ps, ",ps=");//Out(P, ",P=");//Out(ss, ",ss=");//Out(que, ",ope=");
#endif // DEBUG		const int N = str.length();vector<int> b(N + 1);long long ans = 0;for(int i = N-1; i >=0; i--){b[i] = b[i + 1] + ('0' == str[i]);ans += ('1' == str[i]) * b[i];}cout << ((ans % 3) ?"Alice" :"Bob");return 0;
}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

http://www.xdnf.cn/news/1424539.html

相关文章:

  • 【开题答辩全过程】以 基于JSP的养生网站设计与实现为例,包含答辩的问题和答案
  • MySQL 中 InnoDB 引擎的事务隔离级别与“可重复读”隔离级别下的 SQL 编写规范
  • Linux 进程间通信(IPC)
  • 大型语言模型微调 内容预告(69)
  • 【Docker】2025版Ubuntu 22.04 安装 Docker Docker Compose 指南
  • 电力工程师的AI时代已来,这6大功能彻底颠覆传统工作模式
  • 系统性学习数据结构-第二讲-顺序表与链表
  • 金融数据安全
  • 基于单片机汽车防盗系统/汽车安全防丢系统
  • 动态代理设计模式
  • 多模态大语言模型部署
  • Java泛型通配符详解:搞懂?/extends/super用法,避开集合操作踩坑点
  • 二、感知机
  • 高防IP防护效果评估全攻略:从指标解读到实战测试
  • langgraph / openmanus / suna 对比
  • 数据安全不用愁,群晖NAS让你存得放心、用得安心
  • 深度学习环境搭建运行(二) Ubuntu22.04安装基于CUDA11.8的ONNXRuntime-gpu1.18.1详细步骤(新手入门)
  • 联邦学习的文献复现与创新思路指导
  • Qt 项目文件(.pro)中添加 UI 文件相关命令
  • 深度学习】--卷积神经网络
  • k8s--etcd
  • h5实现内嵌微信小程序支付宝 --截图保存海报分享功能
  • authentication port-control auto 和 dot1x port-control auto
  • Linux ARP老化机制/探测机制/ip neigh使用
  • Paimon MergeTreeWrite、Compaction 和 快照构建
  • 甲烷浓度时空演变趋势分析与异常值计算(附下载脚本)
  • 基于docker-compose搭建EFK(Elasticsearch+fluentd+kibana)的日志平台
  • 2025年工作后值得考的财会行业证书推荐,尤其是第二个!
  • 从网络层接入控制过渡到应用层身份认证的过程
  • 如何在SptingBoot项目中引入swagger生成API文档