This is the editorial for a recent contest Teamscode. The problems are open for upsolving on this gym. Problem credits are on the statements themselves.
A. What do you do when the contest starts? Are you busy? Will you solve Bingo?
- WorldEnd/SukaSuka
- Bocchi the Rock
- Thomas
- Lycoris Recoil
- Bokuben
- A Certain Scientific Railgun
- Quintessential Quintluplets
- Tensura
- Re:Zero
- Hayate the Combat Butler
- WorldEnd/SukaSuka
- Hunter x Hunter
- Onimai
- Smartphone Isekai
- Tonikawa
No code is provided. There are $$$49$$$ accepted answers to this problem. Try to find them all!
B. Mountain Climbing Easy
Iterate through the mountain from left to right and maintain a counter for the number of times the altitude increases consecutively. Make sure to reset this counter whenever the altitude does not increase. Also, ensure that each sequence of consecutive altitude increases is counted only once to avoid overcounting mountains.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int N;
cin >> N;
int cnt = 0;
int prev = -1;
int ans = 0;
for (int i = 0; i < N; i++)
{
int a;
cin >> a;
if (a > prev && i) cnt++;
else if (a <= prev) cnt = 0;
if (cnt == 2) ans++;
prev = a;
}
cout << ans << "\n";
return 0;
}
C. No Sweep
There is only $$$1$$$ scenario in any game where Thomas sweeps. So we should do complementary counting and count the total number of ways to assign winners and subtract $$$1$$$ from that. Each game there $$$(k + 1)$$$ possible winners, the $$$k$$$ problemsetters and Thomas. As there are $$$n$$$ rounds, the final result is that in $$$(k + 1)^n - 1$$$ games Thomas will not sweep.
n,k=input().split()
print((int(k) + 1) ** int(n) - 1)
D. Cyclic Shifts
How many elements change between two adjacent cyclic shifts?
First, notice that there always exists an optimal series of operations where the cycle operation, if performed at all, is done first. There are only $$$\mathcal{O}(n)$$$ possible different cycle operations, and the answer afterwards is the sum over the absolute differences between $$$a$$$ and $$$b$$$ for each index.
Consider the first and second such cyclic shift of length $$$5$$$ on an array $$$a=[1, 2, \dots, 8]$$$:
Observe that between the first and second shift, only a small number of elements change positions. In fact, for a cyclic shift of length $$$k$$$ starting at $$$i$$$, only the elements at positions $$$i$$$, $$$i+k$$$, and $$$i+k-1$$$ change positions when transitioning to the cyclic shift at $$$i+1$$$. Thus, we can loop over all possible cyclic shifts, maintaining the sum and updating the indicies accordingly. Because there are only a constant number of updates for each cyclic shift, this runs in $$$\mathcal{O}(n)$$$.
#include <bits/stdc++.h>
long long calculate_cost(int n, const std::vector<long long> &a, const std::vector<long long> &b) {
long long ret = 0;
for (int i = 0; i < n; i++)
ret += llabs(a[i] - b[i]);
return ret;
}
template<typename I>
void rotate_left(I l, I r) { std::rotate(l, std::next(l), r); }
int main() {
using namespace std;
int TC;
cin >> TC;
while (TC--) {
int N, K;
cin >> N >> K;
vector<long long> A(N), B(N);
for (auto &a : A)
cin >> a;
for (auto &b : B)
cin >> b;
long long ans = calculate_cost(N, A, B);
rotate_left(A.begin(), A.begin() + K);
long long cur = calculate_cost(N, A, B);
ans = min(ans, cur + 1);
for (int i = 1; i + K - 1 < N; i++) {
cur -= llabs(A[i - 1] - B[i - 1]) +
llabs(A[i + K - 2] - B[i + K - 2]) +
llabs(A[i + K - 1] - B[i + K - 1]);
auto tem = A[i - 1];
A[i - 1] = A[i + K - 2];
A[i + K - 2] = A[i + K - 1];
A[i + K - 1] = tem;
cur += llabs(A[i - 1] - B[i - 1]) +
llabs(A[i + K - 2] - B[i + K - 2]) +
llabs(A[i + K - 1] - B[i + K - 1]);
ans = min(ans, cur + 1);
}
cout << ans << '\n';
}
}