Codeforces Global Round 22 A-E
A
题解
知识点:贪心。
显然交错释放最好。
若两类数量不一样,那么较少的一组的一定都可以双倍,剩下的另一组就放进一个优先队列,从大到小和少的一组匹配可以双倍,剩下的直接加。
如果两类数量一样,那一定有一个不能被双倍。用上面的方法后,减去一个两组的最小值即可。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[100007], b[100007];
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) cin >> b[i];
int cnt1 = 0;
for (int i = 1;i <= n;i++) if (a[i]) cnt1++;
ll sum = 0;
bool typ = cnt1 <= n - cnt1;
int cnt = min(cnt1, n - cnt1);
priority_queue<int> pq;
for (int i = 1;i <= n;i++) {
if (a[i] == typ) sum += 2 * b[i];
else pq.push(b[i]);
}
for (int i = 1;i <= cnt;i++) {
sum += pq.top() * 2;
pq.pop();
}
while (pq.size()) {
sum += pq.top();
pq.pop();
}
if (n - cnt == cnt) sum -= *min_element(b + 1, b + n + 1);
cout << sum << '\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
题解
知识点:枚举,前缀和,贪心。
注意,给出的 \(k\) 个前缀和,能确定后 \(k-1\) 个数,其他数是可以自由分配的,那么现在只需要让倒数第 \(k\) 个数最小即可。
显然 \(a[n-k+1]\) 最小值为 \(\Bigg \lceil \dfrac{s[n-k+1]}{n-k+1} \Bigg \rceil\) ,即把 \(s[n-k+1]\) 的值平均分配给 \(n-k+1\) 个位置,就能保证 \(a[n-k+1]\) 最小。
接下来,通过这个最小的 \(a[n-k+1]\) 检验后面 \(k-1\) 项是否合法即可。
时间复杂度 \(O(k)\)
空间复杂度 \(O(k)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int s[100007];
bool solve() {
int n, k;
cin >> n >> k;
for (int i = 1;i <= k;i++) cin >> s[i];
int cur;
if (s[1] <= 0) cur = s[1] / (n - k + 1);
else cur = (s[1] + n - k) / (n - k + 1);
for (int i = 2;i <= k;i++) {
if (s[i] - s[i - 1] < cur) return false;
cur = s[i] - s[i - 1];
}
cout << "YES" << '\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 << "NO" << '\n';
}
return 0;
}
C
题解
方法一
知识点:博弈论。
考虑奇数数量 \(cnt\) 模 \(4\) 的四种情况。
- \(cnt \equiv 0 \pmod 4\) ,此时A总是能拿到偶数个奇数,必赢。
- \(cnt \equiv 1 \pmod 4\) ,若 \(n\) 为偶数,那A可以不拿多出来的那个奇数,从而必赢;否则,一定会拿多一个奇数,必输。
- \(cnt \equiv 2 \pmod 4\) ,此时A总是能拿到奇数个奇数,必输。
- \(cnt \equiv 3 \pmod 4\) ,此时A总是能拿到偶数个奇数,必赢。
时间复杂度 \(O(n)\)
空间复杂度 \(O(1)\)
方法二
知识点:博弈论,线性dp,记忆化搜索。
设 \(dp[turn][st][cnt0][cnt1]\) 当前为A/B(0/1)的回合,A手里的奇偶性,偶数剩 \(cnt0\) ,奇数剩 \(cnt1\) 时当前回合人的输赢。终止局面:A的回合且A的和为偶数时为A赢,B的回合且A的和为奇数时为B赢,其他情况均为当前回合的人输。
这样每个回合,只要下一回合奇数偶数中有一种情况是下一个回合的人输,那自己一定是赢的,每次或转移即可。
还有种设法是只看 \(A\) 的输赢,但写起来不方便,因为上面这种写法可以同一或转移,这种写法A是或转移,B是且转移。因为B只要有一种能让A输就会走,应该是且,那就需要多一个判断回合。
时间复杂度 \(O(n^2)\)
空间复杂度 \(O(n^2)\)
代码
方法一
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int n;
cin >> n;
int cnt = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
if (x & 1) cnt++;
}
if (cnt % 4 == 0 || cnt % 4 == 3) cout << "Alice" << '\n';
else if (cnt % 4 == 1) cout << (n & 1 ? "Bob" : "Alice") << '\n';
else if (cnt % 4 == 2) cout << "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;
}
方法二
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int dp[2][2][107][107];//A/B(0/1)的回合,A手里的奇偶性,偶数剩多少,奇数剩多少
bool dfs(bool turn, bool st, int cnt0, int cnt1) {
if (~dp[turn][st][cnt0][cnt1]) return dp[turn][st][cnt0][cnt1];
if (!cnt0 && !cnt1) return !st ^ turn;
bool ans = 0;
if (cnt0) ans |= !dfs(!turn, st, cnt0 - 1, cnt1);
if (cnt1) ans |= !dfs(!turn, st ^ !turn, cnt0, cnt1 - 1);
return dp[turn][st][cnt0][cnt1] = ans;
}
bool solve() {
memset(dp, -1, sizeof(dp));
int n;
cin >> n;
int cnt0 = 0, cnt1 = 0;
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
if (x & 1) cnt1++;
else cnt0++;
}
cout << (dfs(0, 0, cnt0, cnt1) ? "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
题解
知识点:构造。
首先解决 \(k\) 。注意到,\(i<b[i]\) 的情况只出现在 \(a[i] \leq k,a[j]>k(1\leq j<i)\) 的情况。而 \(a\) 是个排列,即每个数字出现且仅出现一次,因此 \(i<b[i]\) 的个数,说明 \(\leq k\) 的数有多少个,即 \(k\) 的大小。
随后处理排列顺序。观察题意可以得到, \((i,b[i]) = (a[x],a[y]),(1\leq y<x)\) 可以得到 \(b[i]\) 一定是 \(i\) 的前驱。
若多个数字的前驱相同,那么这些同前驱的数字只有其中一个可以作为其他数字的前驱,且在原排列 \(a\) 中一定出现在同前驱数字的最后,否则会互相干扰,因此不存在这样的 \(b\) 。
如果 \(b[i] = 0 \text{ 或 } n+1\) 时,代表 \(i\) 前面没有别的数字,不难发现 \(0\) 和 \(n+1\) 只会有且只有一种出现在 \(b\) 中。
因此,可以先用一个桶把 \(b[i]\) 相同的 \(i\) 存在一起,表示这些 \(i\) 的前驱相同。然后从 \(b[i] = 0 \text{ 或 } n+1\) 出发,先找到一个桶里有后继的数,把这个数放在桶的最后,再从头依次输出即可,接下来到最后一个数字,即有后继的数字,桶中重复上述操作即可。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int b[100007];
vector<int> v[100007];
bool solve() {
int n;
cin >> n;
v[0].clear();
v[n + 1].clear();
for (int i = 1;i <= n;i++) cin >> b[i], v[i].clear();
int k = 0;
for (int i = 1;i <= n;i++) if (i < b[i]) k++;
for (int i = 1;i <= n;i++) v[b[i]].push_back(i);
cout << k << '\n';
int cur = 0;
if (v[n + 1].size()) cur = n + 1;
int cnt = 0;
while (cnt < n) {
for (auto &i : v[cur]) {
if (v[i].size()) {
swap(i, v[cur].back());
break;
}
}
for (auto i : v[cur]) cout << i << ' ';
cnt += v[cur].size();
cur = v[cur].back();
}
cout << '\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;
}
E
题解
知识点:排列组合,双指针。
可以放置隔板划分连续区间,考虑区间 \((i,j)\) 的三种情况:
- 全是 \(0\) ,形如
00000000,那么任意放置隔板都能产生回文,所以生成 \(2^{j-i}\) 种方案。 - 两侧都有 \(0\) 但中间两侧有数字挡着,形如
000数字..任意..数字0000,那么考虑处理出两侧隔板方案。设左右侧分别有 \(x,y\) 个 \(0\) ,且两侧需要放相同数量的隔板,因此方案数为 \(C_x^k \cdot C_y^k,k \in [0,\min{(x,y)}]\) 。 - 两侧至少有一侧没有 \(0\) ,形如
00数字..任意..数字,这时候没有 \(0\) 可以直接处理,那就要处理出从两侧出发和相同的一段。假设和相同时,左段右端点是 \(l\) ,右段左端点是 \(r\) ,若 \(l\geq r\) ,说明两端重合了不能形成回文;否则,可以把这一段整体等效 \(0\) ,参与下一次区间的处理。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 998244353;
int fact[100007], invf[100007];
int a[100007];
int qpow(int a, int k) {
int ans = 1;
while (k) {
if (k & 1) ans = 1LL * ans * a % mod;
k >>= 1;
a = 1LL * a * a % mod;
}
return ans;
}
void init(int n) {
fact[0] = 1;
for (int i = 1;i <= n;i++) fact[i] = 1LL * fact[i - 1] * i % mod;
invf[n] = qpow(fact[n], mod - 2);
for (int i = n;i >= 1;i--) invf[i - 1] = 1LL * invf[i] * i % mod;
}
int C(int n, int m) {
return 1LL * fact[n] * invf[m] % mod * invf[n - m] % mod;
}
int f(int i, int j) {
int l = i, r = j;
while (l <= j && !a[l]) l++;
while (r >= i && !a[r]) r--;
if (l == j + 1) return qpow(2, j - i);
if (l != i && r != j) {
int ans = 0, x = l - i, y = j - r;
for (int i = 0;i <= min(x, y);i++)
ans = (ans + 1LL * C(x, i) * C(y, i)) % mod;
return 1LL * ans * f(l, r) % mod;
}
ll ls = a[l], rs = a[r];
while (l < r && ls != rs) {
if (ls < rs) ls += a[++l];
else rs += a[--r];
}
if (l >= r) return 1;
a[l] = a[r] = 0;
return f(l, r);
}
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
cout << f(1, n) << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
init(100001);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
Codeforces Global Round 22 A-E的更多相关文章
- [题解] Codeforces Global Round 22 1738 A B C D E F 题解
很久没rated打过cf的比赛了,这次打得还行,至少进前100了 点我看题 A. Glory Addicts 把类型0的数放进数组a里,类型1的数放进数组b里.如果\(|a|=|b|\),你可以把所有 ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Beta Round #22 (Div. 2 Only)
Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 #include<bits/stdc+ ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table
题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
随机推荐
- SD Host控制器的datasheet
SD-Host控制器的datasheet更多的是给嵌入式软件工作人员使用,datasheet中主要包含一些寄存器以及读写擦除流程 寄存器主要有: 控制寄存器 状态寄存器 配置寄存器 软件和硬件进行交互 ...
- Keep English Level-02
change -- n 零钱 climate change -- 气候变化 exchange -- 交换,兑换(金融) exchange rate -- 汇率 move -- 感动,改变,移动 (n) ...
- 让vs支持wsl调试
WSL安装 wsl --install -d Ubuntu 等一会提示输入用户名,不用管它,直接关闭,下次打开wsl,会以无密码的root用户打开 wsl卸载 wsl --unregister Ubu ...
- 【秘籍揭秘】如何高速下载游戏、Switch资源?省时省力一网打尽!
百度云盘SVIP合租啦亲爱的考研党和游戏玩家们,我今天要分享的是一份独家秘籍!你是不是常常为下载游戏或Switch资源而烦恼?是不是经常遇到下载速度慢.限速等问题,让你等待无尽?别担心,我有一个绝密的 ...
- [转帖]Linux 上 SQL Server 2022 (16.x) 的各版本和支持的功能
https://zhuanlan.zhihu.com/p/371869456 本文内容 SQL Server 版本 将 SQL Server 用于客户端/服务器应用程序 SQL Server 组件 ...
- [转帖]【Kafka】Kafka配置参数详解
Kafka配置参数详解 Kafka得安装与基本命令 Kafka配置参数 kafka生产者配置参数 kafka消费者配置参数 本篇文章只是做一个转载的作用以方便自己的阅读,文章主要转载于: Kafka核 ...
- [转帖]Percolator - 分布式事务的理解与分析
https://zhuanlan.zhihu.com/p/261115166 Percolator - 分布式事务的理解与分析 概述 一个web页面能不能被Google搜索到,取决于它是否被Googl ...
- [转帖]What is Pstate
https://www.jianshu.com/p/342480d917e3 When someone refers to a P-state, generally only the frequenc ...
- Oracle表数量对数据泵备份恢复速度的影响情况
Oracle表数量对数据泵备份恢复速度的影响情况 背景 随着公司产品交付后的时间越来越久. 数据库的备份恢复速度会越来越慢. 最开始一直认为是因为数据量导致的. 但是最近发现, 如果只是将数据库表的量 ...
- 从好玩到好用:程序员用AI提效的那些事儿
本片内容是[AI思维空间]ChatGPT纵横编程世界,点亮智慧火花的续作,主要记录组内开发小伙伴儿们在开发过程中的实际应用案例,记录典型案例,尽量不要和其他人重复,以解决开发过程中的实际问题为主,设计 ...