Codeforces Round #578 (Div. 2)
A. Hotelier
Violence can be.
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
greedy. For each position, if we can move to the next location, definitely try to get the most \ (block \) to the bag.
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
Order \ (G = GCD (n-, m) \) , then each of the inner ring is \ (\ frac {n} { g} \) a, an outer ring per \ (\ frac {m} { g} \) a .
After the judge to see if the same block on the line.
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
Consider enumerate each position as a top-left corner of the rectangle, and then update the answer.
In this case it is necessary to maintain some of the prefix information, such as code \ (sum [i] [j ] \) says in section \ (J \) as the starting point, \ (. 1 \) ~ \ (I \) line the number of rows meet the conditions. "Condition" means that if the rectangle on the \ ((i, j) \ ) in this position, the whole line can be dyed.
Therefore, direct enumeration of chaos on the line = =
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
Suppose we have now an answer string \ (RES \) , now came back and \ (S \) strings spliced with it, is easy to analyze \ (RES \) string used up \ (min (len_ {res} , len_ {s}) \) length. So we remove this part to carry out his back.
Directly on the extension \ (kmp \) , you can also use \ (kmp \) . Direct \ (KMP \) , then first obtains \ (S \) string \ (Next \) , then the \ (RES \) string rear portion (assumed to be \ (tmp \) string), matched to the \ ( tmp \) at the end of time, \ (S \) longest matched prefix string its rear determined.
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;
}