A
暴力
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
char str[maxn];
int room[20];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n >> str;
for (int i = 0; i < n; ++i) {
if (str[i] == 'L') {
for (int j = 0; j <= 9; ++j) {
if (room[j] == 0) {
room[j] = 1;
break;
}
}
}
else if (str[i] == 'R') {
for (int j = 9; j >= 0; --j) {
if (room[j] == 0) {
room[j] = 1;
break;
}
}
}
else {
int id = str[i] - '0';
room[id] = 0;
}
}
for (int i = 0; i <= 9; ++i) cout << room[i];
cout << endl;
}
B
贪心:能装就装起来,如果i->i+1块的hi>=h(i+1)-k,就把多余的部分装起来;否则拿出来
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
int h[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n, m, k; cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 2; i <= n; ++i) {
if (h[i - 1] >= max(h[i] - k, 0)) {
m += h[i - 1] - max(0, h[i] - k);
}
else {
m -= max(0, h[i] - k) - h[i - 1];
}
if (m < 0) break;
}
if (m >= 0) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
C
分析可得:gcd(n, m)既是形成的封闭块数,判断给出的坐标是否在一块即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n, m, q; cin >> n >> m >> q;
ll gcd = __gcd(n, m);
n /= gcd, m /= gcd;
ll sx, sy, ex, ey;
while (q--) {
cin >> sx >> sy >> ex >> ey;
sy--, ey--;
if (gcd == 1) cout << "YES" << endl;
else {
ll poss, pose;
if (sx == 1) poss = sy / n;
else poss = sy / m;
if (ex == 1) pose = ey / n;
else pose = ey / m;
if (poss == pose) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
}
D
分类讨论:
若该行、列全为白色,对橡皮擦选点位置无影响。
若该行的黑色位置最右与最左之间相差大于k,则无法对答案做出贡献(列同理);否则在能放置橡皮擦能做出贡献的位置累加贡献
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2000 + 5;
char in[maxn][maxn];
int sum[maxn][maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> in[i][j];
}
}
int presum = 0;
for (int i = 1; i <= n; ++i) {
int b = 0, e = 0;
for (int j = 1; j <= n; ++j) {
if (in[i][j] == 'B') {
if (!b) b = j;
e = j;
}
}
if (!b) presum++;
else if (e - b + 1 <= k) {
for (int j = max(1, i - k + 1); j <= i; ++j) {
for (int jj = max(1, e - k + 1); jj <= b; ++jj) {
sum[j][jj]++;
}
}
}
}
for (int i = 1; i <= n; ++i) {
int b = 0, e = 0;
for (int j = 1; j <= n; ++j) {
if (in[j][i] == 'B') {
if (!b) b = j;
e = j;
}
}
if (!b) presum++;
else if (e - b + 1 <= k) {
for (int j = max(1, i - k + 1); j <= i; ++j) {
for (int jj = max(1, e - k + 1); jj <= b; ++jj) {
sum[jj][j]++;
}
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
ans = max(ans, sum[i][j]);
}
}
cout << ans + presum << endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2000 + 5;
char in[maxn][maxn];
int sum[maxn][maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> in[i][j];
}
}
int presum = 0;
for (int i = 1; i <= n; ++i) {
int b = 0, e = 0;
for (int j = 1; j <= n; ++j) {
if (in[i][j] == 'B') {
if (!b) b = j;
e = j;
}
}
if (!b) presum++;
else if (e - b + 1 <= k) {
for (int j = max(1, i - k + 1); j <= i; ++j) {
for (int jj = max(1, e - k + 1); jj <= b; ++jj) {
sum[j][jj]++;
}
}
}
}
for (int i = 1; i <= n; ++i) {
int b = 0, e = 0;
for (int j = 1; j <= n; ++j) {
if (in[j][i] == 'B') {
if (!b) b = j;
e = j;
}
}
if (!b) presum++;
else if (e - b + 1 <= k) {
for (int j = max(1, i - k + 1); j <= i; ++j) {
for (int jj = max(1, e - k + 1); jj <= b; ++jj) {
sum[jj][j]++;
}
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
ans = max(ans, sum[i][j]);
}
}
cout << ans + presum << endl;
}