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. HTML – HTML Tags & Semantic HTML 语义化 HTML

    前言 HTML tag 有 100 多个, 有些是功能形的, 非用不可, 有些是为了语义化对 screen reader 友好 (给眼睛有残缺的人也可以获取清晰的网站信息). 语义化是很重要的, 有些 ...

  2. docker 安装启动jenkins 以及问题剖析

    docker 安装启动jenkins 以及问题剖析   首先,你环境必须要有docker,我这里是自己本地虚拟机Vmware,我的虚拟机时linux centos7的 .如果你不知怎么安装虚拟机和命令 ...

  3. JSP——EL表达式&JSTL标签

    EL表达式          JSTL 标签         使用方法:          if 标签            foreach 标签:      <c:forEach items= ...

  4. Servlet——Request请求转发

    Request请求转发       特点:     

  5. [34](CSP 集训)CSP-S 联训模拟 1

    A 几何 重复若干次 -> 不能重叠,因此考虑直接暴力 DP 设 \(f_{i,j,k}\) 表示主串匹配到第 \(i\) 位(将前 \(i\) 位分别归为两类),其中 \(x\) 在重复了若干 ...

  6. Android Qcom USB Driver学习(九)

    本章主要是基于之前的学习,实现一个hidraw的驱动,发现有两种用于识别usb设备的方式,放别是usb_device_id和hid_device_id hid_probe (1)hid_device_ ...

  7. 活动预告 | 中国数据库联盟(ACDU)中国行定档深圳,一起揭秘数据库前沿技术

    在当今数字化时代,数据库是各行各业中最核心的信息管理系统之一.随着技术的飞速发展,数据库领域也不断涌现出新的前沿技术和创新应用.数据库运维和开发人员需要紧跟前沿技术,才能保持竞争力,并实现更高效.更智 ...

  8. js 中必须加分号的位置集合

    1. 匿名函数(自执行函数)(function (){}()) 2. 解析赋值    2 个变量交换位置

  9. 从Windows 11 23H2升级至24H2后,Git操作提示文件所有权错误的3种有效解决方案

    从Windows 11 23H2升级至24H2后,Git操作提示文件所有权错误的3种有效解决方案 在升级至 Windows 11 24H2 后,使用 git add 等命令时,可能会遇到如下错误提示: ...

  10. KubeSphere 社区双周报 | OpenFunction v1.0.0 发布 | 2023.03.03-03.16

    KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...