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连试试水的深浅..... ...
随机推荐
- WebShell代码分析溯源(七)
WebShell代码分析溯源(七) 一.一句话变形马样本 <?php $e = $_REQUEST['e'];$arr = array($_POST['POST'],);array_map(ba ...
- C# List 根据对象属性去重的四种方法对比
测试代码: private void TestDistinct() { Task.Run(() => { //生成测试数据 DateTime dt = DateTime.Now; Random ...
- 用Python6种方法:给定一个不超过5位的正整数,判断有几位
方法一:作比较 a=int(input(">>>>")) if a<10: print(1) elif a<100: #第一个条件已经过滤了大于 ...
- ES-入门
https://es.xiaoleilu.com/010_Intro/10_Installing_ES.html 1. 安装 https://www.elastic.co/cn/downloads/ ...
- Git实战指南----跟着haibiscuit学Git(第三篇)
笔名: haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...
- web.xml的常见配置
web.xml的常见配置 <!-- 配置全局的编码过滤器 --> <filter> <description>编码过滤器</description> & ...
- 华为开发者联盟 方舟编译器 DevEco IDE
华为开发者联盟 https://developer.huawei.com/consumer/cn/ 方舟编译器是为支持多种编程语言.多种芯片平台的联合编译.运行而设计的统一编程平台,包含编译器.工具链 ...
- Centos8尝鲜
Centos 8阿里云下载地址https://mirrors.aliyun.com/centos/8.0.1905/isos/x86_64/ Centos8的一些变化 网络服务: 在/etc/sysc ...
- 《HTTPS权威指南》读书笔记——PKI
互联网公钥基础设施 基于可信的第三方机构(CA,certification authority)实现不同成员在不见面的情况下进行安全通信 订阅人 需要证书来提供安全服务的团体 登记机构(RA) 完成证 ...
- win10配置linux子系统使用python绘图并显示--WSL使用GUI输出
默认情况下,Win10的linux子系统(WSL)是只能使用命令行程序的.所有图形界面的程序都无法执行. 通过为Win10安装XWindows协议的终端应用,可以让Win10成为一台XWindow终端 ...