比赛链接

A

题解

知识点:贪心。

我们考虑把正数和负数分开放,显然把负数和正数放在一起的结果不会更优。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n;
cin >> n;
ll sum1 = 0, sum2 = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
if (x >= 0) sum1 += x;
else sum2 += -x;
}
cout << max(sum2 - sum1, sum1 - sum2) << '\n';;
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

B

题解

知识点:构造。

为了破坏每个子序列,我们把 B 扔到最后面即可,但这样太麻烦,还要考虑跳过后面本来就有的 B

因此我们选择首末 BN 交换,这样只需要进行一半的对称操作。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; bool solve() {
int n;
cin >> n;
cout << (n + 1) / 2 << '\n';
for (int i = 1, j = 3 * n;i < j;i += 3, j -= 3) {
cout << i << ' ' << j << '\n';
}
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

C

题解

知识点:博弈论。

如果 \(\forall i\in [2,n]\) 都有 \(a_1\leq a_i\) ,A 无论怎么取,不妨假设取了下标 \(i\) ,只要 B 取相同下标的,就会导致 \(a_1-1 \cdots a_i-1\cdots\) ,回到这种局面,并且数字减一,往复如此, \(a_1\) 会在 A 的回合是 \(0\) 于是输了。

如果 \(\exist i\in[2,n]\) 有 \(a_1>a_i\) ,A 取 \(a_i\) 中最小的那个,就到了 \(\forall i\in [2,n]\) 都有 \(a_1\leq a_i\) 但 B 先手的局面,B 输。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int a[100007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
bool ok = 0;
for (int i = 2;i <= n;i++) {
ok |= a[1] > a[i];
}
cout << (ok ? "Alice" : "Bob") << '\n';
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

D

题解

知识点:贪心,枚举,STL,前缀和。

几个结论:

  1. 操作的区间不会交叉。因为交叉一定可以合并成一个完整的区间操作,答案不变,所以操作一定是互不相交的。
  2. 区间能操作至 \(0\) 的必要条件是异或和为 \(0\) 。因为操作本质是异或和,能合并,如果操作可行则合并得到整个区间的异或和也是 \(0\) 。

考虑处理出前缀和,和前缀异或和方便计算。在满足必要条件下分类讨论,不满足的无解:

  1. 区间全是 \(0\) ,不需要操作。

  2. 否则,区间长度为奇数,整个操作一次。

  3. 否则,若首或尾有 \(0\) 元素,则可以拆一个出来得到情况2,操作一次即可。

  4. 否则,找到区间内某个分割点,使得区间划分成两个长度为奇数异或和为 \(0\) 的区间,回到情况2,操作两次即可。

    注意,不需要考虑划分成两个偶数长度区间,如果有偶数长度划分可行,则一定分别能再被划分成两个奇数长度区间,即得到四个奇数长度异或和为 \(0\) 的区间,取前 \(3\) 个合并最后变成两个奇数区间,因此一定存在奇数划分。

  5. 其他情况无解。

最后考虑情况4如何找到划分点。我们用 map 记录到 \(i\) 之前所有出现的异或和最后一次出现的位置,分奇数下标偶数下标分别记录。那么对于一个位置 \(i\) ,我们就能找到左侧最近的一个不同奇偶性的位置 \(last[i]\) ,使得 \([1,i]\) 和 \([1, last[i] ]\) 的异或和相同,且 \((last[i],i]\) 区间长度为奇数,于是我们就找到了一个划分点。如果划分点小于 \(l\) 则不可划分。

时间复杂度 \(O(n \log n + q)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int a[200007], xsum[200007], last[200007];
ll sum[200007];
bool solve() {
int n, q;
cin >> n >> q;
for (int i = 1;i <= n;i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
xsum[i] = a[i] ^ xsum[i - 1];
}
map<int, int> mp[2];
for (int i = 1;i <= n;i++) {
if (mp[!(i & 1)].count(xsum[i])) last[i] = mp[!(i & 1)][xsum[i]];
mp[i & 1][xsum[i]] = i;
}
while (q--) {
int L, R;
cin >> L >> R;
if ((xsum[R] ^ xsum[L - 1]) == 0) {
if (sum[R] - sum[L - 1] == 0) cout << 0 << '\n';
else if ((R - L + 1) & 1 || !a[L] || !a[R]) cout << 1 << '\n';
else if (last[R] >= L) cout << 2 << '\n';
else cout << -1 << '\n';
}
else cout << -1 << '\n';
}
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

Codeforces Round #832 (Div. 2) A-D的更多相关文章

  1. Codeforces Round #832 (Div. 2) A~C题解

    目录 A B C A 思路:这个题的话我们把负数和整数分别求出来,比较绝对值的大小,用较大的那个减去较小的那个就可以了. #include <cstring> #include <i ...

  2. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  8. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. ubantu安装中文失败的改正方法

    情况说明 一开始我也是像博主们那样安装,但是一直在这个界面安装失败,太悲伤了!! 报错内容 c Failed to fetch http://us.archive.ubuntu.com/ubuntu/ ...

  2. python(第四版阅读心得)(系统工具)(一)

    本章将会讲解python常用系统工具的介绍 python中大多数系统级接口都集中在两个模块: sys 和 os 但仍有部分其他标准模块也属于这个领域 如: 常见: glob   用于文件名扩展 soc ...

  3. 第四十篇:Vue的生命周期(一)

    好家伙,军训结束了,回归 Vue实例的生命周期 1.什么是生命周期? 从Vue实例创建,运行到销毁期间总是伴随着各种各样的事件,这些事件,统称为生命周期. 2.什么是生命周期钩子? 生命周期函数的别称 ...

  4. HTTP 优缺点

    HTTP 最凸出的优点是「简单.灵活和易于扩展.应用广泛和跨平台」. 1. 简单HTTP 基本的报文格式就是 header + body ,头部信息也是 key-value 简单文本的形式,易于理解, ...

  5. Q准则涡识别方法

    Q准则(Q Criterion)在涡识别中计算效率高,效果也不错,是一种常用的涡提取/识别方法. 了解Q准则需要从速度梯度张量入手,而速度梯度张量可以分解成两部分 \[\frac{\partial u ...

  6. winfrom,窗体抖动功能

    #region 方法一 Point first = this.Location; for (int i = 0; i < 50; i++) { Application.DoEvents(); R ...

  7. [报错]-NameError: name 'NAN' is not defined

    部分数据输出为NaN,处理这部分异常数据使用isnan()函数 from math import isnan isnan(z) 参考: https://www.cnblogs.com/itdyb/p/ ...

  8. 2020年12月-第02阶段-前端基础-CSS Day06

    CSS Day06 定位(position) 理解 能说出为什么要用定位 能说出定位的4种分类 能说出四种定位的各自特点 能说出我们为什么常用子绝父相布局 应用 能写出淘宝轮播图布局 1. CSS 布 ...

  9. 【WPF】实现动态切换语言(国际化)以及动态换肤功能

    前言:以下内容,手把手从搭建到最终实现,完成多语言切换以及换装功能. 本地系统环境:win 10 编译器环境:VS2022 社区版 .NET 环境: .NET 6 1.新建一个WPF项目 2.新建完毕 ...

  10. Elastic Stack 8.0 再次创建enrollment token

    enrollment token 在第一个 Elasticsearch 启动后的有效时间为30分钟.超过30分钟的时间上述 token 将会无效. enrollment token分两个,一个是kib ...