Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2)
A. Prison Break
题意:给出 n×m 的矩阵,每个点表示一个囚犯,移动一次时间为1,可同时移动,每个点上可以同时有多个囚犯,求让所有点移动到 (r,c) 点的最短的时间。
思路:输出 (1,1)、(1,m)、(n,1)、(n,m) 四个点到 (r,c) 的最小距离的最大值。
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main() {
int T;
cin >> T;
while (T--) {
ll n, m, r, c;
cin >> n >> m >> r >> c;
cout << max(abs(1 - r) + abs(1 - c),
max(abs(1 - r) + abs(m - c),
max(abs(n - r) + abs(1 - c), abs(n - r) + abs(m - c)))) << endl;
}
return 0;
}
B. Repainting Street
题意:有1到n的序列,每次可以把长度为k的任意区间里的所有数修改为同一个数,每次改成的数可以不一样,问使得n个数都相同的最小修改次数。
思路:暴力。先记下序列中有那些数,每次全部修改为这个数,从前往后遍历数组,遇到不是这个数的就往后跳k长度,代表这个长度为k的区间被修改。
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
int a[100005];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
set<int> set;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
set.insert(a[i]);
}
int ans = 1000000000;
for (auto it : set) {
int cnt = 0;
int i = 1;
while (i <= n) {
if (a[i] != it) {
cnt++;
i += k;
} else {
i++;
}
}
ans = min(ans, cnt);
}
cout << ans << endl;
}
return 0;
}
C. Bouncing Ball
题意:给你长度为n的串 a1,a2,a3…an,ai=1代表有箱子,ai=0代表空,只有是1的位置才能站上去。p表示你一开始在第p个位置上,然后跳到 p+k,p+2k… 直到跳出n以外去。你可以花费y秒删去第一个位置,或者花费x秒把这个位置修改为1。最少要剩余p个位置。求最少花费多少,能从p跳出n且每个落点都是1。
思路:一开始写的是二维暴力,然后发现,删除的是一段前缀,而且从某个位置一直跳到最后可以边处理边做。从后往前遍历,用sum[i]记录第一个跳到的是i点且往后跳到最后经过的点中是0的个数,那么需要修改的点的个数就是sum[i]个,需要删除的前缀的个数就是i-p个。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
int sum[200005];
char s[200005];
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(sum, 0, sizeof(sum));
int n, p, k, x, y;
scanf("%d%d%d%s%d%d", &n, &p, &k, s + 1, &x, &y);
ll ans = 1000000000000;
for (int i = n; i >= p; --i) {
sum[i] += sum[i + k] + (s[i] == '0');
ans = min(ans, 1ll * sum[i] * x + 1ll * (i - p) * y);
}
printf("%lld\n", ans);
}
return 0;
}
D. XOR-gun
题意:给出长度为n的非递增数列,一次操作可以将两个相邻的数合并成一个数,问最少几次操作能使这个数列变成不再是非递减的。
思路:数列中的数最大值是1e9,二进制位数最多30位,那么根据鸽巢原理,当n大于60时,肯定存在三个数的最高位1(high_bit)相同,那么后两个数异或肯定比前一个数小(要考虑到数列是非递增的,也就是排好序的,所以它们是连续的),答案就是1。否则枚举相邻的两个区间(左区间、有区间)即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[100005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
a[i] ^= a[i - 1];
}
if (n > 60) {
cout << 1 << endl;
return 0;
}
int ans = 0x3f3f3f3f;
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
int x = j + 1;
for (int y = x; y <= n; ++y) {
int lx = (a[j] ^ a[i - 1]);
int rx = (a[y] ^ a[x - 1]);
if (lx > rx) {
ans = min(ans, j - i + y - x);
}
}
}
}
if (ans < 0x3f3f3f3f) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
return 0;
}
E. New Game Plus!
待补题