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)的更多相关文章

  1. Codeforces Round #578 (Div. 2) Solution

    Problem A Hotelier 直接模拟即可~~ 复杂度是$O(10 \times n)$ # include<bits/stdc++.h> using namespace std; ...

  2. KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words

    题目链接:https://codeforc.es/contest/1200/problem/E 题意: 有n串字符串,让你连起来:sample please ease in out   ---> ...

  3. Codeforces Round #578 (Div. 2) E. Compress Words (双哈希)

    题目:https://codeforc.es/contest/1200/problem/E 题意:给你n个单词,你需要把他合成成一个句子,相邻的两个单词,相邻部分相同的话可以把其中一个的删掉 思路:因 ...

  4. Codeforces Round #578 (Div. 2) 二维差分 可做模板

    题意: 在n*n的矩阵中,你可以选择一个k*k的子矩阵,然后将这个子矩阵中的所有B全部变为W,问你怎么选择这个子矩阵使得最终的矩阵中某一行全是W或者某一列全是W的个数最多 题解:考虑每一行和每一列,对 ...

  5. Codeforces Round #578 (Div. 2) C. Round Corridor (思维,数论)

    题意: 有一个分两层的圆盘,每层从12点方向均分插入\(n\)和\(m\)个隔板,当内层和外层的隔板相连时是不能通过的,有\(q\)个询问,每次给你内层或外层的两个点,判断是否能从一个点走到另外一个点 ...

  6. 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 ...

  7. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  8. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. Java编程思想——第17章 容器深入研究(一)

    这一章将学习散列机制是如何工作的,以及在使用散列容器时怎么样编写hashCode()和equals()方法. 一.容器分类 先上两张图 来概况完整的容器分类 再细说都为什么会有那些特性. 二.Coll ...

  2. drf请求模块分析

    一.drf请求模块 .drf的request是在wsgi的request基础上再次封装 .wsgi的request作为drf的request一个属性:_request .新的request对旧的req ...

  3. Netty服务端NioEventLoop启动及新连接接入处理

    一 Netty服务端NioEventLoop的启动 Netty服务端创建.初始化完成后,再向Selector上注册时,会将服务端Channel与NioEventLoop绑定,绑定之后,一方面会将服务端 ...

  4. Geodesic 什么是“测地线的”?

    确定是使用上椭球体(测地线)还是平地上(平面)的最短路径.强烈建议将 Geodesic 方法用于在不适合进行距离测量的坐标系(例如 Web 墨卡托或任何地理坐标系)中存储的数据,以及任何地理区域跨度较 ...

  5. SQL Server通过条件搜索获取相关的存储过程等对象

    在SQL Server中,我们经常遇到一些需求,需要去搜索存储过程(Procedure).函数(Function)等对象是否包含某个对象或涉及某个对象,例如,我需要查找那些存储过程.函数是否调用了链接 ...

  6. bayaim——达梦数据库 导入导出

    导出: E:\dmdbms\bin\dexp.exe """SYSDBA"""/"""******" ...

  7. [洛谷P1144][题解]最短路计数

    这道题可以用各种算法踩掉,我选择的是SPFA. 因为题目要求计数,所以我们开一个ans数组表示数量. 分两种情况讨论: 一:dis_v>dis_u+1 最短路被更新了,可以直接ans_v=ans ...

  8. JavaScript 7 获取可视窗口、网页元素、获取节点方式

    获取可视窗口对象 chrom *document.body firefox *document.documentElement 获取滚动条偏移量 ele.scrollTop ele.scrollLef ...

  9. ETC到底要不要办?有什么好处?

    一说到ETC,开车的朋友想必不会陌生.但很多车友却不太愿意办理ETC, 究其原因,主要是一些谣言所致,一传一十传百最后变成了真实的谎言,并且对此深信不疑, 比如下面5个广泛流传的谣言     在来看看 ...

  10. javascript截取字符串的最后几个字符

    在JavaScript中截取字符串一般是使用内置的substring()方法和substr()方法,这两个方法功能都很强大,也都能实现截取字符串中的最后几个字符. substring()方法 Java ...