Codeforces Round #832 (Div. 2) A-D
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,前缀和。
几个结论:
- 操作的区间不会交叉。因为交叉一定可以合并成一个完整的区间操作,答案不变,所以操作一定是互不相交的。
- 区间能操作至 \(0\) 的必要条件是异或和为 \(0\) 。因为操作本质是异或和,能合并,如果操作可行则合并得到整个区间的异或和也是 \(0\) 。
考虑处理出前缀和,和前缀异或和方便计算。在满足必要条件下分类讨论,不满足的无解:
区间全是 \(0\) ,不需要操作。
否则,区间长度为奇数,整个操作一次。
否则,若首或尾有 \(0\) 元素,则可以拆一个出来得到情况2,操作一次即可。
否则,找到区间内某个分割点,使得区间划分成两个长度为奇数异或和为 \(0\) 的区间,回到情况2,操作两次即可。
注意,不需要考虑划分成两个偶数长度区间,如果有偶数长度划分可行,则一定分别能再被划分成两个奇数长度区间,即得到四个奇数长度异或和为 \(0\) 的区间,取前 \(3\) 个合并最后变成两个奇数区间,因此一定存在奇数划分。
其他情况无解。
最后考虑情况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的更多相关文章
- Codeforces Round #832 (Div. 2) A~C题解
目录 A B C A 思路:这个题的话我们把负数和整数分别求出来,比较绝对值的大小,用较大的那个减去较小的那个就可以了. #include <cstring> #include <i ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
随机推荐
- JavaScript基础回顾知识点记录4-正则表达式篇(介绍基本使用)
js 中 正则表达式使用 创建正则对象和test方法使用 /* 创建正则表达式的对象 语法: var 变量 = new RegExp("正则表达式","匹配模式" ...
- 给你的博客加个aplayer
1.在 layout.ejs 中 body 标签内粘贴入以下 <!--音乐--> <link rel="stylesheet" href="https: ...
- Shell第三章《for循环》
Shell循环:for 语法结构: for 变量名 [ in 取值列表 ] do 循环体 done 需求:自动创建10个用户 #!/bin/bash read -p "请输入你要创建的用户名 ...
- 「雅礼集训 2017 Day7」跳蚤王国的宰相(树的重心)
题面 来源 「 雅 礼 集 训 2017 D a y 7 」 跳 蚤 王 国 的 宰 相 传 统 2000 m s 1024 M i B {\tt「雅礼集训 2017 Day7」跳蚤王国的 ...
- java基础———break,continue
break通常用在循环语句之中用来跳出循环: continue终止某次循环过程,跳过尚未执行的语句:接着执行下次是否执行循环的判定:
- 经纬度转换为距离单位km的方法
function rad(d){ return d * Math.PI /180.0; }; GetDistance(lat1, lng1, lat2, lng2){ var radLat1 =rad ...
- uniapp小程序新版授权登录
1.授权按钮: <view> <button class='login-btn' type='primary' @click="bindGetUserInfo"& ...
- KingbaseES lag 和 lead 函数
1.简介 lag与lead函数是跟偏移量相关的两个分析函数,通过这两个函数可以在一次查询中取出同一字段的前N行的数据(lag)和后N行的数据(lead)作为独立的列,从而更方便地进行进行数据过滤. 2 ...
- KingbaseES V8R6C5禁用root用户ssh登录图形化部署集群案例
案例说明: 对于KingbaseES V8R6C5版本在部集群时,需要建立kingbase.root用户在节点间的ssh互信,如果在生产环境禁用root用户ssh登录,则通过ssh部署会失败:在图形化 ...
- LibTorch 自动微分
得益于反向传播算法,神经网络计算导数时非常方便,下面代码中演示如何使用LibTorch进行自动微分求导. 进行自动微分运算需要调用函数 torch::autograd::grad( outputs, ...