Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2) (个人题解)
Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2)
A. Prison Break
https://codeforces.com/contest/1457/problem/A
题意:给定一个n行m列的监狱(每个(i,j)中均有犯人),犯人们为了集体逃脱找到了一个洞口(r,c)点。现在请问最少需要多少秒才能全体逃脱。
思路:只要计算一下边角的犯人离出口的最大距离即可
void solve() {
int n, m, r, c;
cin >> n >> m >> r >> c;
cout << max(r - 1, n - r) + max(c - 1, m - c) << endl;
}
B. Repainting Street
https://codeforces.com/contest/1457/problem/B
题意:Tom要为一排房子粉刷颜色,每个房子的都有它的颜色,由于Tom认为相同颜色的为”美丽“,但Tom每天只能为k个连续的房子进行粉刷。请问最少需要多少天才能变美丽
思路:由于仅100种颜色,可以使用暴力枚举 + 滑动窗口解决
void solve() {
int n, k;
cin >> n >> k;
int a[n + 1];
for (int i = 0; i < n; ++i)
cin >> a[i];
int res = n;
for (int c = 1; c <= 100; ++c) { //枚举进行粉刷的颜色种类
int last = -k, curr = 0;
for (int j = 0; j < n; ++j) {
if (a[j] == c)
continue;
if (last + k <= j) {
last = j;
curr++;
}
}
res = min(res, curr);
}
printf("%d\n", res);
}
C. Bouncing Ball
https://codeforces.com/contest/1457/problem/C
string s;
void solve() {
int n, p, k, x, y;
cin >> n >> p >> k;
p--;
vector<int> b(n);
cin >> s;
cin >> x >> y;
int ans = 1 << 30;
for (int i = n - 1; 0 <= i; --i) {
b[i] = !(s[i] == '1');
if (i + k < n)
b[i] += b[i + k];
if (i >= p)
ans = min(ans, (i - p) * y + b[i] * x);
}
printf("%d\n", ans);
}
E. New Game Plus!
https://codeforces.com/contest/1457/problem/E
理解好题意,利用前缀和贪心即可
// Author : RioTian
// Time : 20/11/29
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 5;
int n, k, a[N];
ll b[N], sum, res = -(1LL << 60), curr;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; ++i)
cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) {
b[i] = b[i - 1] + a[i];
sum += 1LL * (i - 1) * a[i];
}
for (int i = 1; i <= n; ++i) {
int mx = (i - 1 + k) / (k + 1), mn = (i - 1) / (k + 1);
res = max(res, (b[n] - b[i - 1]) * mx + curr + sum);
sum -= (b[n] - b[i]);
curr += 1LL * mn * a[i];
}
res = max(res, curr);
cout << res << endl;
return 0;
}