Round A 2021 - Kick Start 2021
比赛链接:https://codingcompetitions.withgoogle.com/kickstart/round/0000000000436140
K-Goodness String (5pts, 7pts)
判断字符不同值,计算得分至K的差值
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
for (int i = 1; i <= T; ++i) {
int n, k, cnt = 0;
cin >> n >> k;
string s;
cin >> s;
for (int i = 0; i < n / 2; ++i)
if (s[i] != s[n - i - 1]) cnt++;
// cout << cnt << "\n";
if (cnt == k) cnt = 0;
else
cnt = abs(k - cnt);
cout << "Case #" << i << ": " << cnt << "\n";
}
return 0;
}
L Shaped Plots (8pts, 12pts)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, M, a[1005][1005], l[1005][1005], r[1005][1005], u[1005][1005],
d[1005][1005];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
for (int ca = 1; ca <= T; ca++) {
cin >> N >> M;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j) cin >> a[i][j];
memset(l, 0, sizeof(l)), memset(r, 0, sizeof(r));
memset(u, 0, sizeof(u)), memset(d, 0, sizeof(d));
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++) {
if (a[i][j])
l[i][j] = l[i][j - 1] + 1, u[i][j] = u[i - 1][j] + 1;
}
int ret = 0;
for (int i = N; i > 0; i--)
for (int j = M; j > 0; j--)
if (a[i][j]) {
r[i][j] = r[i][j + 1] + 1, d[i][j] = d[i + 1][j] + 1;
// lu
ret += max(0, min(l[i][j], u[i][j] / 2) - 1);
ret += max(0, min(l[i][j], d[i][j] / 2) - 1);
ret += max(0, min(r[i][j], u[i][j] / 2) - 1);
ret += max(0, min(r[i][j], d[i][j] / 2) - 1);
ret += max(0, min(u[i][j], l[i][j] / 2) - 1);
ret += max(0, min(u[i][j], r[i][j] / 2) - 1);
ret += max(0, min(d[i][j], l[i][j] / 2) - 1);
ret += max(0, min(d[i][j], r[i][j] / 2) - 1);
}
cout << "Case #" << ca << ": " << ret << "\n";
}
return 0;
}
Rabbit House (9pts, 15pts)
BFS 搜索
// RioTian 21/03/22
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100005;
int dx[] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int n, m, a[400][400], b[400][400];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
for (int i = 1; i <= T; ++i) {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> a[i][j];
memset(b, -1, sizeof(b));
priority_queue<pair<int, pair<int, int>>> h;
ll cnt = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
h.push(make_pair(a[i][j], make_pair(i, j)));
while (h.size()) {
auto t = h.top();
h.pop();
int x = t.second.first, y = t.second.second;
if (b[x][y] < 0) {
b[x][y] = t.first;
cnt += t.first - a[x][y];
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k], ny = y + dy[k];
if (nx > 0 && nx <= n && ny > 0 && ny <= m &&
a[nx][ny] + 1 < b[x][y])
h.push(make_pair(t.first - 1, make_pair(nx, ny)));
}
}
}
cout << "Case #" << i << ": " << cnt << "\n";
}
return 0;
}
Checksum (10pts, 17pts, 17pts)
这道题没做出来。先记录一下dalao的解法
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 100005, dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int N, M, a[505][505], b[505][505], ur[505], uc[505], fa[1005];
pair<int, int> c[505 * 505];
void init() {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) scanf("%d", &a[i][j]);
M = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
scanf("%d", &b[i][j]);
if (b[i][j] > 0) {
c[++M] = make_pair(-b[i][j], (i - 1) * N + (j - 1));
}
}
for (int i = 1; i <= N; i++) scanf("%d", &ur[i]);
for (int i = 1; i <= N; i++) scanf("%d", &uc[i]);
}
int findSet(int x) {
if (!fa[x]) return x;
fa[x] = findSet(fa[x]);
return fa[x];
}
void work() {
memset(fa, 0, sizeof(fa));
sort(c + 1, c + M + 1);
int ret = 0;
for (int i = 1; i <= M; i++) {
int x = c[i].second / N + 1, y = c[i].second % N + 1 + N;
int bx = findSet(x), by = findSet(y);
if (bx != by) {
if (rand() % 2) fa[bx] = by;
else
fa[by] = bx;
} else
ret -= c[i].first;
}
printf("%d\n", ret);
}
int main() {
// freopen("d.in", "r", stdin), freopen("d.out", "w", stdout);
int T;
cin >> T;
for (int ca = 1; ca <= T; ca++) {
printf("Case #%d: ", ca);
init();
work();
}
return 0;
}