比赛链接

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 。

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

时间复杂度 \(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. 使用idea remote 开发体验

    本地使用idea开发最不好的一个体验就是打开稍大的工程就非常的卡,怎么调参数都没用,现在idea推出了idea remote就赶紧来体验下. 使用方式 除了idea不需要额外下载什么包,但是因为rem ...

  2. 【Java】学习路径63-反射、类的加载-附思维导图(完结)

    这一章的知识在实际开发也没有那么重要,主要是了解即可,另外掌握如何使用反射机制. 类的使用: 在虚拟机中: 类的加载->类的连接->类的初始化 类的加载   只会加载需要用到的类,加载到内 ...

  3. k8s中ingress,service,depoyment,pod如何关联

    k8s中pod通过label标签名称来识别关联,它们的label  name一定是一样的.ingress,service,depoyment通过selector 中app:name来关联 1.查询发布 ...

  4. KingbaseES Query Mapping 查询映射功能

    有过SQL优化经历的人都知道,对于有些SQL性能问题,可能需要涉及到SQL层面的修改,这不仅麻烦,而且在已上线的系统还存在很大的风险.KingbaseES V8R6 提供了query mapping功 ...

  5. 字节跳动 DanceCC 工具链系列之Xcode LLDB耗时监控统计方案

    作者:李卓立 仲凯宁 背景介绍 在<字节跳动 DanceCC 工具链系列之Swift 调试性能的优化方案>[1]一文中,我们介绍了如何使用自定义的工具链,来针对性优化调试器的性能,解决大型 ...

  6. 高德地图与CAD图叠加显示方法汇总及优缺点分析

    前言 ​ 高德地图应用在许多领域,平常我们用的地图导航,除过正常的地图导航指引功能之外,其实还有很多实用的功能.如高德影像地图应用在包括地理.土地测量.水文学.生态学.气象学以及海洋学等方面.Auto ...

  7. Java SE note1

    1.数据类型 基本类型 低------------------------------------------------->高 byte,short,char -> int -> ...

  8. ProxySQL(6):管理后端节点

    文章转载自:https://www.cnblogs.com/f-ck-need-u/p/9286922.html 配置后端节点前的说明 为了让ProxySQL能够找到后端的MySQL节点,需要将后端的 ...

  9. Traefik开启监控,日志,追踪需要的参数

    监控 官方文档地址:https://doc.traefik.io/traefik/observability/metrics/overview/ 可以使用多种监控软件,比如Datadog,Influx ...

  10. Elasticsearch的mapping讲解

    映射是定义文档及其包含的字段的存储和索引方式的过程. 映射定义具有: 元字段 元字段用于自定义如何处理关联的文档元数据.包括文档 _index,_id和 _source领域. 字段或属性 映射包含pr ...