Swap Game

Alice 和 Bob 两个人在玩游戏。

有一个长度为 \(n\) 的序列 \(a\),Alice 和 Bob 两人轮流完成一个操作,Alice 先开始。

每个人可以将数列的第一个数减 \(1\),并将它与后面序列的一个数进行交换,如果一个人操作之前发现当前序列中的第一个数为 \(0\),这个人就输了。

问如果两人都足够聪明,最后谁会赢?

题解:

我们发现必败态是\(a_1=0\),我们考虑由于两个人都会选择最优的策略,也就是说每个人一定会挑\([2,n]\)中最小的数和\(a_1\)交换,因为这样能使得对方快速遇到\(a_1=0\),所以说必败态为\(a_1\)是最小值;

  1. 如果一开始\(a_1\)本身就是最小值,也就是说Alice开局就是必败态,那么他无论如何都走不到一个必败态,也就是说Bob始终能留给Alice必败态的局面,所以Alice必败
  2. 如果一开始\(a_1\)不是最小值,那么Alice每次都能抛给bob必败态,所以Alice必胜
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10, M = 4e5 + 10; int n; void solve()
{
cin >> n;
int res = 0;
int a;
cin >> a;
bool flag = false;
for (int i = 2; i <= n; ++i)
{
int x;
cin >> x;
if (x < a)
{
flag = true;
}
}
if (flag)
cout << "Alice" << endl;
else
cout << "Bob" << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}

Yet Another Problem

给定长度为\(n\)序列\(a\),存在\(q\)次询问,每次询问给定区间\([l,r]\),现在支持这样的操作:选定一段奇数长度的区间\(l\leq L\leq R\leq r\),可以使得这段区间的所有元素变成\(a_L\bigoplus a_{L+1}...\bigoplus a_R\),对于每次询问回答至少需要多少次操作才能使得区间\([l,r]\)中的所有元素都变成0,如果无法成功输出\(-1\)

\(1\leq a_i \leq 2^{30}\)

题解:二进制 + 思维 + 二分 + 离散化

我们发现如果区间\([l,r]\)的异或和不为0,一定不可能成功,因为每次选定的是奇数长度的区间

如果区间\([l,r]\)异或和为0,那么存在以下几种情况:

  1. 如果区间\([l,r]\)中所有元素本来全部为0,最小操作次数为0;
  2. 如果区间\([l,r]\)的长度为奇数,最小操作次数为1;
  3. 如果区间\([l,r]\)的长度为偶数:
  4. 如果\(a[l]=0\) 或者 \(a[r] = 0\),最少操作次数为1;
  5. 如果该区间存在奇数长度的区间\([l,i]\)异或和为0,那么最小操作次数为2;
  6. 不满足上面情况的都无法成功

再来讲一下如何快速得知是否存在奇数长度的区间\([l,i]\)异或和为0,我们可以邻接表维护每一个前缀异或和的奇数位置和偶数位置,如果\(l\)为奇数我们就在相同前缀异或和中找到大于\(l\)的第一个奇数位置,如果\(l\)是偶数位置同理;但是我们发现\(a_i\)太大了,所以我们需要利用哈希表离散化(映射)

#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 1e6 + 10, M = 4e5 + 10; int n, q;
int a[N];
int pre[N];
int sum[N];
unordered_map<int, vector<int>> mp1, mp2; void solve()
{
cin >> n >> q;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i <= n; ++i)
{
sum[i] = sum[i - 1] + (a[i] == 0);
pre[i] = pre[i - 1] ^ a[i];
}
for (int i = 1; i <= n; ++i)
{
if (i % 2 == 1)
mp1[pre[i]].push_back(i);
else
mp2[pre[i]].push_back(i);
}
while (q--)
{
int l, r;
cin >> l >> r;
if (pre[r] != pre[l - 1])
{
cout << -1 << endl;
continue;
}
if (sum[r] - sum[l - 1] == r - l + 1)
{
cout << 0 << endl;
continue;
}
if ((r - l + 1) % 2 == 1)
{
cout << 1 << endl;
continue;
}
if (a[l] == 0 || a[r] == 0)
{
cout << 1 << endl;
continue;
}
int x = pre[l - 1];
if (l % 2 == 1)
{
int p = lower_bound(all(mp1[x]), l) - mp1[x].begin();
if (p == mp1[x].end() - mp1[x].begin() || mp1[x][p] > r)
{
cout << -1 << endl;
continue;
}
cout << 2 << endl;
}
else
{
int p = lower_bound(all(mp2[x]), l) - mp2[x].begin();
if (p == mp2[x].end() - mp2[x].begin() || mp2[x][p] > r)
{
cout << -1 << endl;
continue;
}
cout << 2 << endl;
}
}
}
signed main(void)
{
Zeoy;
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}

Codeforces Round 832 (Div2)的更多相关文章

  1. A. Launch of Collider Codeforces Round #363 (Div2)

    A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  3. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  4. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  5. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

  6. Codeforces Round #361 div2

    ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...

  7. codeforces 572(Div2)A、B、C、D1、D2、E

    Cdoeforces 572(Div2)A.B.C.D1.D2.E 传送门:https://codeforces.com/contest/1189 A.题意: 给你一串长为n的字符串,要求你将其切割为 ...

  8. Codeforces Round #626 Div2 D,E

    比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...

  9. codeforces round 472(DIV2)D Riverside Curio题解(思维题)

    题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...

  10. Codeforces Round #626 Div2 D. Present(位掩码,二分)

    题目链接:https://codeforces.com/contest/1323/problem/D 题意:给了大小为4e5的数组a,其中1<=ai<=1e7.求所有点对和的异或和,即: ...

随机推荐

  1. yum命令提示error: rpmdb: BDB0113 Thread/process,解决方法

    最近在做RHCE的题目,yum命令装vdo时,使用yum install命令的时候,提示error: rpmdb: BDB0113 Thread/process,具体错误如下: [root@node2 ...

  2. 技术实践 | 在线 KTV 实现过程(内附demo体验)

    ​ 你在线上K过歌吗? 在线K歌自2014年兴起以来,已经发展出了无比庞大的用户群体,每两人中就有就有一人体验过在线 K歌,其前景不可小觑. 如此庞大的市场规模,以及音视频技术使用门槛逐步降低的加持, ...

  3. 合合信息扫描全能王发布“黑科技”,让AI替人“思考”图像处理问题

    现阶段,手机扫描正越来越多地进入到人们的生活中.随着扫描应用场景的不断拓宽,诸多细节的问题逐渐显露,比如使用者在拍照扫描文档时,手指不小心"入镜"了,只能重拍:拍电脑屏幕时,画面上 ...

  4. 深度学习/NLP中的Attention注意力机制

    首先是整体认知,Attention的位置: 传送门1:Attention 机制 传送门2:Attention用于NLP的一些小结 一句话概括:Attention就是从关注全局到关注重点. 借鉴了人类视 ...

  5. Angular 18+ 高级教程 – 目录

    请按顺序阅读 关于本教程 初识 Angular Get Started Angular Compiler (AKA ngc) Quick View Dependency Injection 依赖注入  ...

  6. ASP.NET Core – TagHelper

    前言 以前写的 Asp.net core 学习笔记之 Tag Helper, 这篇是整理版. 参考 Docs – Author Tag Helpers in ASP.NET Core Creating ...

  7. Google Analytics & Ads 学习笔记

    更新: 2021-09-13 Naming conversion for event category, action, label https://support.google.com/analyt ...

  8. Identity – Without Identity Framework

    前言 上一回研究 Authenticate 和 Authorization 已经是 2 年前了. 业务需求一直没有增长, 所以也没有再去提升它了. 但最近业务开始上去了. 荒废的功夫又得拾起来了. 上 ...

  9. [OI] 莫比乌斯函数与莫比乌斯反演

    9. 莫比乌斯函数与莫比乌斯反演 9.1 莫比乌斯函数 9.1.1 定义 设 \(\mu\) 为莫比乌斯函数,则有: \[\mu(x)=\begin{cases}1\qquad (n=1)\\ 0\q ...

  10. [TK] 三色二叉树 hzoi-tg#282 存图方法

    可以发现,假如在序列中遇到一个数为 \(2\) ,也就是有两个子节点,那么接下来的全部数字都是描述左树的,一直到左树被遍历完成. 这让你想到了什么? 当然是DFS啦. 根据DFS我们有下面这样的存图思 ...