Codeforces Global Round 4
Contest Info
[Practice Link](https://codeforc.es/contest/1178)
Solved | A | B | C | D | E | F1 | F2 | G | H |
---|---|---|---|---|---|---|---|---|---|
6/9 | O | O | O | O | O | Ø | - | - | - |
- O 在比赛中通过
- Ø 赛后通过
- ! 尝试了但是失败了
- - 没有尝试
Solutions
A. Prime Minister
签到题。
#include <bits/stdc++.h>
using namespace std;
#define N 110
int n, a[N], b[N];
int main() {
while (scanf("%d", &n) != EOF) {
memset(b, 0, sizeof b);
int sum = 0, other = 0;
for (int i = 1; i <= n; ++i) scanf("%d", a + i), sum += a[i];
b[1] = 1;
other = a[1];
for (int i = 2; i <= n; ++i) {
if (a[1] >= a[i] * 2) {
other += a[i];
b[i] = 1;
}
}
if (other > sum / 2) {
int sze = 0;
vector <int> vec;
for (int i = 1; i <= n; ++i) {
if (b[i]) {
++sze;
vec.push_back(i);
}
}
printf("%d\n", sze);
for (int i = 0; i < sze; ++i) printf("%d%c", vec[i], " \n"[i == sze - 1]);
} else {
puts("0");
}
}
return 0;
}
B. WOW Factor
题意:
将连续的两个\(v\)可以看成一个\(w\),询问给出的字符串中只包含字符\(v\)和\(o\),询问有多少个子序列组成\(wow\)
思路:
先处理出每个\(o\)左边有多少个\(w\)以及右边有多少个\(w\),然后计算贡献即可。
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1000010
int n;
ll l[N], r[N];
char s[N];
int main() {
while (scanf("%s", s + 1) != EOF) {
n = strlen(s + 1);
l[0] = 0;
for (int i = 1; i <= n; ++i) {
l[i] = l[i - 1];
if (i > 1 && s[i] == 'v' && s[i - 1] == 'v') {
++l[i];
}
}
r[n + 1] = 0;
for (int i = n; i >= 1; --i) {
r[i] = r[i + 1];
if (i < n && s[i] == 'v' && s[i + 1] == 'v') {
++r[i];
}
}
ll res = 0;
for (int i = 1; i <= n; ++i) {
if (s[i] == 'o') {
res += 1ll * l[i - 1] * r[i + 1];
}
}
printf("%lld\n", res);
}
return 0;
}
C. Tiles
题意:
有这样的瓷砖:
用来拼\(n \cdot m\)的地板,要求任意相邻的边两边的颜色不能相同。
思路:
显然第一个位置可以有四种选择,并且第一排的后面的每个位置都有两种选择,因为不能和前面的边相同。
那么第一列后面的位置也有两种选择,那么剩下的位置都只有一种选择,因为它们相邻了两条边。
所以答案就是\(4 \cdot 2^{n + m - 2}\)
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll p = 998244353;
ll qmod (ll base, ll n) {
ll res = 1;
while (n) {
if (n & 1) {
res = res * base % p;
}
base = base * base % p;
n >>= 1;
}
return res;
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
printf("%lld\n", qmod(2, n - 1) * qmod(2, m - 1) % p * 4 % p);
}
return 0;
}
D. Prime Graph
题意:
给出\(n\)个点,要求构成一个简单图,使得边的总数是素数,并且每个点的度数也是素数。
思路:
- 首先考虑每个点的度数至少为\(2\),那么先把这些点连成一个环,所有点的度数就是\(2\)了。
- 再考虑边的数量是素数,一个显然的想法是所有数都可以表示成\(2x + 3y\)的形式,所以我们将环中的点分成两部分,每次各取一个点连边,就增加一条边,并且让两个度数为\(2\)的点变成度数为\(3\)的。
- 那么只要满足我们找一个\(>= n\)的最近的素数\(y\),使得\(y - n \leq \frac{n}{2}\)即可这样构造。
- 素数的密度很高,或者\(n\)只有\(1000\)可以打表验证一下,这样是可以的。
代码:
#include <bits/stdc++.h>
using namespace std;
#define N 1010
#define pii pair <int, int>
#define fi first
#define se second
int n;
bool isprime(int x) {
for (int i = 2; i < x; ++i) {
if (x % i == 0) return 0;
}
return 1;
}
int main() {
while (scanf("%d", &n) != EOF) {
int tot = n;
for (int i = tot; ; ++i) {
if (isprime(i)) {
tot = i;
break;
}
}
vector <pii> vec;
for (int i = 2; i <= n; ++i) vec.push_back(pii(i - 1, i));
vec.push_back(pii(1, n));
int remind = tot - n;
int pos = n / 2 + 1;
for (int i = 1; i <= remind; ++i) {
vec.push_back(pii(i, pos));
++pos;
}
printf("%d\n", tot);
for (auto it : vec) printf("%d %d\n", it.fi, it.se);
}
return 0;
}
E. Archaeology
题意:
给出一个只包含'a', 'b', 'c'的字符串,保证任意两个连续的字符都不相同,要求选出一个子序列\(t\)使得它是一个回文串,并且\(|t| \geq \left\lfloor \frac{|s|}{2} \right\rfloor\)
思路:
贪心从两端取即可。
因为左端的两个字符和右端的两个字符,必然会有两个相等,因为保证了任意两个连续的字符不同,那么这样就是说每\(4\)个字符,至少有\(2\)个有贡献,显然满足题目限制。
其实也可以枚举一样,看看:
假如左端的\(ab\),那么右端的取值有以下几种:
- \(ab\)
- \(ac\)
- \(bc\)
显然都满足要求。
代码:
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define INF 0x3f3f3f3f
int n, m, f[N][3], g[N][3], nx[3];
char s[N], t[N];
int vis[N];
bool ok() {
m = 0;
for (int i = 1; i <= n; ++i) if (vis[i]) {
t[++m] = s[i];
}
t[m + 1] = 0;
if (m >= n / 2) {
printf("%s\n", t + 1);
return 1;
}
return 0;
}
int main() {
while (scanf("%s", s + 1) != EOF) {
n = strlen(s + 1);
for (int i = 1; i <= n; ++i) vis[i] = 0;
nx[0] = nx[1] = nx[2] = 0;
for (int i = 1; i <= n + 1; ++i) {
for (int j = 0; j < 3; ++j) {
g[i][j] = nx[j];
}
if (i <= n) {
nx[s[i] - 'a'] = i;
}
}
nx[0] = nx[1] = nx[2] = n + 1;
for (int i = n; i >= 0; --i) {
for (int j = 0; j < 3; ++j) {
f[i][j] = nx[j];
}
if (i > 0) {
nx[s[i] - 'a'] = i;
}
}
int l = 0, r = n + 1;
while (l <= r) {
int Min = INF, pos = -1;
for (int i = 0; i < 3; ++i) {
if (f[l][i] < g[r][i] && f[l][i] - l + r - g[r][i] < Min) {
Min = f[l][i] - l + r - g[r][i];
pos = i;
}
}
if (pos == -1) break;
l = f[l][pos];
r = g[r][pos];
vis[l] = vis[r] = 1;
}
if (l < r - 1) {
vis[l + 1] = 1;
}
if (!ok()) printf("IMPOSSIBLE\n");
}
return 0;
}
F1. Short Colorful Strip
题意:
有一个长度为\(n\)的方格纸,现在要求做\(n\)次涂刷操作,每次选择一个区间将其刷成颜色\(i\),刚开始全都为颜色\(0\),问最后方格纸上第\(i\)格颜色为\(c_i\)的方案数。
\(n = m\)。
思路:
令\(f[l][r]\)表示完成区间\([l, r]\)的涂刷的方案数。
考虑对于一个区间\([l, r]\),那么我们第一次涂的肯定是\(c_i\)最小的,然后以\(c_i\)最小的那个格子为界,划分成两边,令\(pos[i]\)表示当前区间\(c_i\)最小的格子的下标,那么有转移方程:
f[l][r] = \sum\limits_{a} \sum\limits_{b} f[l][a - 1] \cdot f[a][pos[i] - 1] \cdot f[pos[i] + 1][i - 1] \cdot f[i +1][r]
\end{eqnarray*}
\]
注意到划分出两个区间后,左边右边独立,所以可以分开计算,最后乘起来即可。
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 510
const ll p = 998244353;
int n, m, a[N];
ll f[N][N];
void add(ll &x, ll y) {
x += y;
if (x >= p) x -= p;
}
ll dp(int l, int r) {
if (l > r) return 1;
if (f[l][r] != -1) return f[l][r];
if (l == r) return f[l][r] = 1;
f[l][r] = 0;
int pos = l;
for (int i = l + 1; i <= r; ++i) {
if (a[i] < a[pos]) pos = i;
}
ll L = dp(l, pos - 1), R = dp(pos + 1, r);
for (int i = l; i < pos; ++i) {
add(L, dp(l, i) * dp(i + 1, pos - 1) % p);
}
for (int i = pos + 1; i <= r; ++i) {
add(R, dp(pos + 1, i) * dp(i + 1, r) % p);
}
return f[l][r] = L * R % p;
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
memset(f, -1, sizeof f);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
printf("%lld\n", dp(1, n));
}
return 0;
}
Codeforces Global Round 4的更多相关文章
- 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 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^( ...
- 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题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- 【Codeforces Round 1110】Codeforces Global Round 1
Codeforces Round 1110 这场比赛只做了\(A\).\(B\).\(C\),排名\(905\),不好. 主要的问题在\(D\)题上,有\(505\)人做出,但我没做出来. 考虑的时候 ...
- 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案
Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...
随机推荐
- 二十、网卡框架分析、虚拟网卡驱动和DM9621驱动分析
一.网络设备驱动的结构 网卡设备不同于字符设备和块设备, 网络设备并不对应于/dev目录下的文件,它存放在/sys/class/net目录下. Linux系统对网络设备驱动定义了四个层次: 1. 网络 ...
- PB Event ID 含义 内容浅析
Event ID 含义 内容浅析 event可以用pb自带的id,自动触发事件,而function就需要你去调用了,返回值多种多样 单选或多选按钮消息(前缀:pbm_bm) pbm_bmgetchec ...
- netty--buffer分配策略
AdaptiveRecvByteBufAllocator 动态分配buffer大小的类. 如果前一次读取完全填满了分配的缓冲区,它将逐渐增加预期的可读字节数.(增加的方式:初始化类的时候,会预先设置好 ...
- Inline Hook 钩子编写技巧
Hook 技术通常被称为钩子技术,Hook技术是Windows系统用于替代中断机制的具体实现,钩子的含义就是在程序还没有调用系统函数之前,钩子捕获调用消息并获得控制权,在执行系统调用之前执行自身程序, ...
- Luogu5285 [十二省联考2019] 骗分过样例
题目分析: 观察前3个点,$361=19*19$,所以可以发现实际上就是快速幂,然后模数猜测是$998244353$,因为功能编号里面有这个数字,用费马小定理处理一下. $pts:12$ 观察第4个点 ...
- AQS独占式同步队列入队与出队
入队 Node AQS同步队列和等待队列共用同一种节点结构Node,与同步队列相关的属性如下. prev 前驱结点 next 后继节点 thread 入队的线程 入队节点的状态 INITIAl 0 初 ...
- 基于【 SpringBoot】一 || QQ授权流程
一.准备工作 1.qq开放平台应用申请,获取APP ID和APP Key 2.qq开放平台配置回调地址 二.服务器端生成授权链接 1.请求地址 https://graph.qq.com/oauth2. ...
- 【转载】C#使用Split函数根据特定分隔符分割字符串
在C#程序开发过程中,很多时候可能需要将字符串根据特定的分割字符分割成字符或者List集合,例如根据逗号将字符串分割为数组,或者根据竖线将字符串分割成数组,C#中提供了Split()函数来快速将字符串 ...
- jquery事件委托详解
jQuery事件委托处理流程 上一章分析jQuery.event.add的时候已经分析了事件绑定,再把绑定的部分源码抽出来 if ( !(eventHandle = elemData.handle) ...
- 学习到目前,自己封装的db类和pdo类
DB封装类 <?php class DBDA { public $host = "localhost"; public $uid = "root"; pu ...