Codeforces Round 832 (Div2)
Swap Game
Alice 和 Bob 两个人在玩游戏。
有一个长度为 \(n\) 的序列 \(a\),Alice 和 Bob 两人轮流完成一个操作,Alice 先开始。
每个人可以将数列的第一个数减 \(1\),并将它与后面序列的一个数进行交换,如果一个人操作之前发现当前序列中的第一个数为 \(0\),这个人就输了。
问如果两人都足够聪明,最后谁会赢?
题解:
我们发现必败态是\(a_1=0\),我们考虑由于两个人都会选择最优的策略,也就是说每个人一定会挑\([2,n]\)中最小的数和\(a_1\)交换,因为这样能使得对方快速遇到\(a_1=0\),所以说必败态为\(a_1\)是最小值;
- 如果一开始\(a_1\)本身就是最小值,也就是说Alice开局就是必败态,那么他无论如何都走不到一个必败态,也就是说Bob始终能留给Alice必败态的局面,所以Alice必败
- 如果一开始\(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,那么存在以下几种情况:
- 如果区间\([l,r]\)中所有元素本来全部为0,最小操作次数为0;
- 如果区间\([l,r]\)的长度为奇数,最小操作次数为1;
- 如果区间\([l,r]\)的长度为偶数:
- 如果\(a[l]=0\) 或者 \(a[r] = 0\),最少操作次数为1;
- 如果该区间存在奇数长度的区间\([l,i]\)异或和为0,那么最小操作次数为2;
- 不满足上面情况的都无法成功
再来讲一下如何快速得知是否存在奇数长度的区间\([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)的更多相关文章
- 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 ...
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
- Codeforces Round #361 div2
ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...
- 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的字符串,要求你将其切割为 ...
- Codeforces Round #626 Div2 D,E
比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...
- codeforces round 472(DIV2)D Riverside Curio题解(思维题)
题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...
- Codeforces Round #626 Div2 D. Present(位掩码,二分)
题目链接:https://codeforces.com/contest/1323/problem/D 题意:给了大小为4e5的数组a,其中1<=ai<=1e7.求所有点对和的异或和,即: ...
随机推荐
- shell脚本字符串截取方法整理
首先先声明一个变量str,下面演示以该变量为例: str='https://www.baidu.com/about.html' 1.#号截取,删除左边字符,保留右边字符 echo ${str#*//} ...
- HTML & CSS – Styling Scrollbar
前言 Scrollbar 能 styling 的东西不多 (尤其是 IOS 基本上只能 display:none 而已),但有时候我们不得不 styling. 这里记入我自己在项目中修改过的 scro ...
- 课时04:了解HTTP网络协议
什么是HTTP协议 HTTP(HyperText Transfer Protocol)叫超文本传输协议,它是web服务器和客户端直接进行数据传输的规则,是一个无状态的应用层协议. HTTP协议工作原理 ...
- 【Simpleperf】Android的CPU分析,性能优化利器
很多时候,写代码是一件很爽的事情,但最后需要对APP进行瘦身.性能分析却是一件很棘手的事情.当需要对APP的性能进行分析时,Simpleperf是一个简单快捷的选择. 正文开始前,先奉上官方的资料: ...
- 74.数组map能干什么,会改变原数组吗
map是处理数据的方法,不会改变原数组,会返回一个新数组 : filter 也不会改变原数组,会返回新数组 : forEach 也不会改变原数组,不会返回新数组 : reduce不会改变原数组 : 是 ...
- Python之py9-py9博客情况获取
#!/usr/bin/env python # -*- coding:utf-8 -*- import os import re import datetime import requests url ...
- 分享几个实用且高效的EF Core扩展类库,提高开发效率!
前言 今天大姚给大家分享3款开源且实用的EF Core扩展类库,希望能帮助你在使用 EF Core 进行数据库开发变得更加高效和灵活,提高开发效率. EF Core介绍 Entity Framewor ...
- 关于git的安装
window平台下面: 步骤一: 首先去官网下载安装包: 官方链接:https://git-scm.com/download/win 至于选择32位还是64为的,各位就见仁见智了(根据自己的系统). ...
- redis的主从复制master/slaver
什么是Redis的复制 就是我们常说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主. 复制原理 Slave启动成 ...
- vue搜索历史记录缓存实现
思路: 1.浏览器缓存永久保存搜索历史数据. 2.页面初始化将数据保存到页面变量中. 3.对搜索历史记录的怎加和删除,要同步到缓存中. ----------------直接看代码----------- ...