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. .NET Core 3.0之深入源码理解Host(一)

    写在前面 ASP .NET Core中的通用主机构建器是在v2.1中引入的,应用在启动时构建主机,主机作为一个对象用于封装应用资源以及应用程序启动和生存期管理.其主要功能包括配置初始化(包括加载配置以 ...

  2. 精通awk系列(1):安装新版本的gawk

    回到: Linux系列文章 Shell系列文章 Awk系列文章 安装新版本gawk awk有很多种版本,例如nawk.gawk.gawk是GNU awk,它的功能很丰富. 本教程采用的是gawk 4. ...

  3. 基于SpringBoot前后端分离的点餐系统

    基于SpringBoot前后端分离的点餐系统 开发环境:主要采用Spring boot框架和小程序开发 项目简介:点餐系统,分成卖家端和买家端.买家端使用微信小程序开发,实现扫码点餐.浏览菜单.下单. ...

  4. node error SOCKET error:10106

    上周我的node.js command prompt出错了,什么也干不了 SOCKET error:10106 纠结两天,终于搞定了,其实比较简单,就是不会弄起来好麻烦 参考: 作者:忆常  url: ...

  5. 1.2 菜单权限 ——MyRapid WinForm快速开发框架-功能介绍

    添加菜单后用户并不会看到菜单 需要经过授权后才能看到 授权界面如图 授权的数据逻辑可以理解为一个键值对 角色>>菜单 但是为了方便集中数据管理 我设计成了 角色>>资源 其中的 ...

  6. LeetCode刷题-最长公共前缀(简单)

    题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  7. mysql数据库多表查询where与内连接inner join的区别

    按理说where是对前面的笛卡尔积进行过滤,工作量大增,inner join则不会.但我实际测试了一下,两种查询耗时基本相等,甚至where还快一些,多次测试后基本如此. 如下图: where: in ...

  8. redis启动错误: Warning: no config file specified, using the default config. In order to specify a config

    redis启动错误: Warning: no config file specified, using the default config. In order to specify a config ...

  9. PHPStorm 初遇 Xdebug (xdebug代码调试及性能分析)

    centos 7 下PHP7安装xdebug # 下载xdebug wget https://xdebug.org/files/xdebug-2.7.2.tgz # 解压 tar -xf xdebug ...

  10. kaldi简介及安装

    操作系统 : Ubuntu18.04_x64 gcc版本 :7.4.0 简介 Kaldi诞生于2009年的JohnsHopkins University,刚开始项目重点是子空间高斯模型(SGMM)建模 ...