Codeforces Round #578 (Div. 2)
Codeforces Round #578 (Div. 2)
A. Hotelier
暴力即可。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n;
char s[N];
int res[10];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
cin >> s + 1;
for(int i = 1; i <= n; i++) {
if(s[i] == 'L') {
for(int j = 0; j < 10; j++) {
if(res[j] == 0) {
res[j] = 1;
break;
}
}
} else if(s[i] == 'R') {
for(int j = 9; j >= 0; j--) {
if(res[j] == 0) {
res[j] = 1;
break;
}
}
} else {
res[s[i] - '0'] = 0;
}
}
for(int i = 0; i < 10; i++) cout << res[i];
return 0;
}
B. Block Adventure
贪心。对于每个位置,如果能够转移到下一个位置,肯定是尽量拿最多的\(block\)到袋子里。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int t, n, m, k;
int a[N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> t;
while(t--) {
cin >> n >> m >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
bool ok = true;
for(int i = 1; i < n; i++) {
int p = max(0, a[i + 1] - k);
if(a[i] > p) {
m += a[i] - p;
} else {
if(m < p - a[i]) {
ok = false;
break;
} else {
m -= p - a[i];
}
}
}
if(ok) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
return 0;
}
C. Round Corridor
令\(g=gcd(n,m)\),那么内环就是每\(\frac{n}{g}\)一块,外环是每\(\frac{m}{g}\)一块。
之后判断一下是否在同一块就行。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
ll n, m;
int q;
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> q;
ll g = gcd(n, m);
ll d1 = n / g, d2 = m / g;
while(q--) {
ll sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
ll b1, b2;
if(sx == 1) {
b1 = (sy - 1) / d1;
} else b1 = (sy - 1) / d2;
if(ex == 1) {
b2 = (ey - 1) / d1;
} else b2 = (ey - 1) / d2;
if(b1 == b2) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
return 0;
}
D. White Lines
考虑枚举每一个位置作为矩形的左上顶点,然后更新答案。
这样的话就需要维护一些前缀信息,比如代码中的\(sum[i][j]\)就表示以第\(j\)列为起点,\(1\)~\(i\)行中满足条件的行数。“满足条件”的意思就是如果把矩形放在\((i,j)\)这个位置,这一行能够全被染色。
所以直接乱枚举一下就行= =
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2005;
int n, k;
char s[N][N];
int r[N][N], c[N][N], sum[N][N], mx[N][N];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> s[i] + 1;
for(int j = 1; j <= n; j++) {
r[i][j] = r[i][j - 1] + (s[i][j] == 'W');
c[j][i] = c[j][i - 1] + (s[i][j] == 'W');
}
}
int ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - k + 1; j++) {
sum[i][j] = sum[i - 1][j];
if(r[i][n] == n) {
if(j == 1) ans++;
} else if(r[i][j - 1] + r[i][n] - r[i][j + k - 1] == n - k) sum[i][j]++;
}
}
for(int i = 1; i <= n - k + 1; i++) {
for(int j = 1; j <= n - k + 1; j++) {
mx[i][j] = sum[i + k - 1][j] - sum[i - 1][j];
}
}
memset(sum, 0, sizeof(sum));
for(int j = 1; j <= n; j++) {
for(int i = 1; i <= n - k + 1; i++) {
sum[i][j] = sum[i][j - 1];
if(c[j][n] == n) {
if(i == 1) ans++;
} else if(c[j][i - 1] + c[j][n] - c[j][i + k - 1] == n - k) sum[i][j]++;
}
}
int res = 0;
for(int i = 1; i <= n - k + 1; i++) {
for(int j = 1; j <= n - k + 1; j++) {
res = max(res, mx[i][j] + sum[i][j + k - 1] - sum[i][j - 1]);
}
}
ans += res;
cout << ans;
return 0;
}
E. Compress Words
假设我们现在已有一个答案串\(res\),现在后面来了个\(s\)串来与它拼接,容易分析\(res\)串中最多用到\(min(len_{res},len_{s})\)的长度。所以我们就取出后面这一部分来搞即可。
可以直接上扩展\(kmp\),也可以就用\(kmp\)。直接\(kmp\)的话先求出\(s\)串的\(next\),然后将其与\(res\)串后面部分(假设为\(tmp\)串),匹配到了\(tmp\)末尾时,\(s\)串中与其后面匹配的最长前缀就确定了。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
int n;
int Next[N];
int KMP(string &s, string &t) {
int lens = s.length(), lent = t.length();
Next[0] = -1;
int j = -1;
for(int i = 1; i < lent; i++) {
while(j >= 0 && t[i] != t[j + 1]) j = Next[j];
if(t[i] == t[j + 1]) j++;
Next[i] = j;
}
int len = min(lent, lens);
j = -1;
for(int i = lens - len; i < lens; i++) {
while(j >= 0 && (j == lent - 1 || s[i] != t[j + 1])) j = Next[j];
if(s[i] == t[j + 1]) j++;
}
return j;
}
string s, res;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> s;
res = s;
for(int i = 2; i <= n; i++) {
cin >> s;
int p = KMP(res, s);
res += s.substr(p + 1);
}
cout << res;
return 0;
}
Codeforces Round #578 (Div. 2)的更多相关文章
- Codeforces Round #578 (Div. 2) Solution
Problem A Hotelier 直接模拟即可~~ 复杂度是$O(10 \times n)$ # include<bits/stdc++.h> using namespace std; ...
- KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words
题目链接:https://codeforc.es/contest/1200/problem/E 题意: 有n串字符串,让你连起来:sample please ease in out ---> ...
- Codeforces Round #578 (Div. 2) E. Compress Words (双哈希)
题目:https://codeforc.es/contest/1200/problem/E 题意:给你n个单词,你需要把他合成成一个句子,相邻的两个单词,相邻部分相同的话可以把其中一个的删掉 思路:因 ...
- Codeforces Round #578 (Div. 2) 二维差分 可做模板
题意: 在n*n的矩阵中,你可以选择一个k*k的子矩阵,然后将这个子矩阵中的所有B全部变为W,问你怎么选择这个子矩阵使得最终的矩阵中某一行全是W或者某一列全是W的个数最多 题解:考虑每一行和每一列,对 ...
- Codeforces Round #578 (Div. 2) C. Round Corridor (思维,数论)
题意: 有一个分两层的圆盘,每层从12点方向均分插入\(n\)和\(m\)个隔板,当内层和外层的隔板相连时是不能通过的,有\(q\)个询问,每次给你内层或外层的两个点,判断是否能从一个点走到另外一个点 ...
- 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连试试水的深浅..... ...
随机推荐
- Jupiter 页面环境下使用pip无法安装(AttributeError: module 'pip' has no attribute 'main')
异常: AttributeError Traceback (most recent call last) <ipython-input-5-880e5dfa627c> in <mod ...
- PHP 是如何做垃圾回收的
PHP 是如何做垃圾回收的 包含 php 5 与 php7 的变量实现和垃圾回收的对比 变量的实现 PHP 的变量是弱类型的,可以表示整数.浮点数.字符串等类型.PHP 的变量是使用结构体 zval ...
- Redis面试篇 -- Redis常见性能问题和解决方案?
master最好不要做任何的持久化工作,如RD内存快照或者AOF日志文件: 如果数据比较重要,某个slave开始AOF备份数据,策略设置为每秒同步1次: 为了主从复制的速度和连接的稳定性,master ...
- OpenStack与ZStack深度对比:架构、部署、计算、运维监控等
摘要 OpenStack从2010年开源至今,已经走过9个年头,其正在进入主流企业市场,但该项目依然面临较难部署和管理的老问题.有一点是毫无疑问的,那就是OpenStack保持着高速增长的态势,超过5 ...
- docker升级步骤及注意事项
centos系统默认安装的docker版本是1.13版本,在安装部分镜像时可能出现兼容问题,本文通过实际操作总结Docker升级最新版本步骤及可能出现的问题,供各位参考. 环境:CentOS Linu ...
- 程序员:May the Force be with you!
程序员如何理解:May the Force be with you! 我们并没有向其他人那样讨论现象级产品的生成原因,因为我们并不清楚这个原因是什么. 我们也不知道足记是否会重复过去一些现象级产品忽生 ...
- 在 Cocos2d-x 中添加自己的微博链接
配置:OS X 10.10 + Xcode 6.0 + Cocos2d-x-3.2 一.Android 端代码 1.在 Cocos2dxActivity.java 中添加openUrl函数并导入响应包 ...
- 28.分类算法---KNN
1.工作原理: 存在一个样本数据集合,也称为训练样本集,并且样本集中每个数据都存在标签,即我们知道样本集中每一数据与所属分类对应的关系.输入没有标签的数据后,将新数据中的每个特征与样本集中数据对应的特 ...
- docker工具之基本命令
docker工具之基本命令 1.docker服务的启动.停止.重启 systemctl start docker #启动docker服务 systemctl daemon-reload #守护进程重启 ...
- Linux使用之centos下安装Java环境并运行Java程序
前言 在Java中所有的程序都是在JVM上运行的.Java虚拟机(JVM)读取并处理经过编译的与平台无关的*.class文件.因为Java语言源程序编写后,先使用Java伪编译器进行伪编译,将其转换为 ...