比赛链接

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;
}

最新文章

  1. 11个提问频率最高的PHP面试题
  2. VPS拨号主机自动拨号脚本(centos7)
  3. Linux修改密码passwd用法
  4. AspectJ的基本使用
  5. poj 1845 Sumdiv 约数和定理
  6. 三款精美的html5及css3的源码插件
  7. JavaScript要点 (三) 保留关键字
  8. ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)
  9. Redis 缓存 + Spring 的集成示例(转)
  10. 在vc正在使用xtremetoolkit接口库-----使用简单的控制
  11. Stash安装和破解
  12. Qt551.主窗体Margin
  13. #2019-2020-4 《Java 程序设计》第九周总结
  14. qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
  15. Postman高级应用——串行传参和动态传参详解
  16. Python接口测试实战5(下) - RESTful、Web Service及Mock Server
  17. ajax跨域的解决办法
  18. [转]James Bach:测试人员的角色
  19. ruby underscore
  20. Java调度线程池ScheduleExecutorService(续)

热门文章

  1. dentry的引用计数不对导致的crash
  2. CF662C Binary Table (快速沃尔什变换FWT)
  3. 在DispatcherServlet.class中的,获取方法值中获取到的都是arg值
  4. c语言_二叉树的建立以及3种递归
  5. Linux常用基础命令三
  6. Hint 使用--leading
  7. 自定义View5 -塔防小游戏:第二篇防御塔随意放置
  8. 2021年1月-第02阶段-前端基础-HTML+CSS阶段-Day01
  9. 2020年12月-第01阶段-前端基础-HTML CSS 项目阶段(二)
  10. 谷歌MapReduce经典论文翻译(中英对照)