2051A — Preparing for the Olympiad
Focus on the fact that Monocarp can freely choose his training days, but Stereocarp is forced to train the day after Monocarp. Monocarp should carefully pick days where the problems he solves outweigh those Stereocarp would solve the following day.
To maximize $$$m - s$$$, Monocarp should always train on the last day, as it contributes $$$a[n]$$$ without affecting Stereocarp. For earlier days, Monocarp should train on day $$$i$$$ only if $$$a[i] > b[i+1]$$$, adding the difference $$$a[i] - b[i+1]$$$ to the total. This greedy approach ensures that Monocarp maximizes his problems solved while minimizing Stereocarp's. Iterate through the array once for each test case, adding valid differences and always including $$$a[n]$$$ from the last day.
Time Complexity: $$$O(n)$$$ per test case
Space Complexity: $$$O(n)$$$
#include <bits/stdc++.h>
using namespace std;
void solve () {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int &i : a) cin >> i;
for (int &i : b) cin >> i;
int res = 0;
for (int i = 0; i < n - 1; i++) {
res += max(0, a[i] - b[i + 1]);
}
cout << res + a.back() << endl;
}
int32_t main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Monocarp's walking pattern repeats every three days, with distances $$$a$$$, $$$b$$$, and $$$c$$$. To find the day he completes his journey, calculate how many complete $$$3$$$-day cycles he can finish before walking at least $$$n$$$ kilometers. Then, account for the remaining distance in the partial cycle, if any.
The total distance Monocarp walks in one $$$3$$$-day cycle is $$$a + b + c$$$. First, calculate how many complete cycles he can walk, which is
Subtract the distance covered by these cycles from $$$n$$$ to find the remaining distance $$$n_{\text{rem}}$$$. If $$$n_{\text{rem}} = 0$$$, he finishes at the end of a complete cycle, i.e., on day $$$3 \times \text{cycles}$$$. Otherwise, determine the day within the next cycle by checking if $$$n_{\text{rem}}$$$ is covered by $$$a$$$, $$$a + b$$$, or $$$a + b + c$$$, corresponding to days $$$1$$$, $$$2$$$, or $$$3$$$ of the partial cycle. Finally, the result is
Time Complexity: $$$O(1)$$$ per test case
Space Complexity: $$$O(1)$$$
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve () {
int n, a, b, c;
cin >> n >> a >> b >> c;
int res = n / (a + b + c);
n %= (a + b + c);
if (n == 0) cout << res * 3 << endl;
else if (a >= n) cout << res * 3 + 1 << endl;
else if (a + b >= n) cout << res * 3 + 2 << endl;
else cout << res * 3 + 3 << endl;
}
int32_t main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}