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.
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;
}
2051C — Preparing for the Exam
The problem revolves around checking if Monocarp knows all the questions in each list. Observe that each list has to miss less than two questions.
If $$$k + 1 < n$$$, it is impossible for Monocarp to pass any list, because he cannot know $$$n-1$$$ questions simultaneously. Output $$$m$$$ zeros.
If $$$k = n$$$, Monocarp knows all questions, so he passes every list. Output $$$m$$$ ones.
Otherwise, sort the known questions $$$q_1, q_2, \dots, q_k$$$. For each $$$a_i$$$, check if $$$a_i$$$ is in the sorted list of known questions. If $$$a_i$$$ is known, Monocarp cannot pass this list, so append '0' to the result. Otherwise, append '1'.
Time Complexity: $$$O(k \log k + m \log k)$$$ per test case
Space Complexity: $$$O(m + k)$$$
#include <bits/stdc++.h>
using namespace std;
void solve () {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(m), q(k);
for (int i = 0; i < m; i++) {
cin >> a[i];
}
for (int i = 0; i < k; i++) {
cin >> q[i];
}
if (k + 1 < n) {
cout << string(m, '0') << endl;
} else if (k == n) {
cout << string(m, '1') << endl;
} else {
sort(q.begin(), q.end());
string res;;
for (int i = 0; i < m; i++) {
if (binary_search(q.begin(), q.end(), a[i])) {
res += '0';
} else {
res += '1';
}
}
cout << res << endl;
}
}
int32_t main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
To determine the number of interesting pairs, observe that removing elements at positions $$$i$$$ and $$$j$$$ leaves a sum of $$$S - a_i - a_j$$$, where $$$S$$$ is the total sum of the array.
First, compute the total sum $$$S = \text{sum}(a)$$$. The condition for an interesting pair translates to $$$l \leq a_i + a_j \leq r$$$, where $$$l = S - y$$$ and $$$r = S - x$$$. Sort the array to facilitate efficient pair counting. For each $$$a_i$$$, use binary search to find the range of indices $$$j > i$$$ where $$$a_i + a_j$$$ satisfies the bounds $$$[l, r]$$$. Specifically, use $$$\text{lower_bound}$$$ to find the first $$$a_j \geq l - a_i$$$ and $$$\text{upper_bound}$$$ to find the first $$$a_j > r - a_i$$$. The difference between these indices gives the number of valid pairs for the current $$$i$$$. Iterate through all $$$i$$$ to compute the total count efficiently. This approach, combining sorting and binary search, ensures the solution is fast enough for the input constraints.
Time Complexity: $$$O(n \log n)$$$ per test case
Space Complexity: $$$O(n)$$$
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve () {
int n, x, y;
cin >> n >> x >> y;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int sum = accumulate(v.begin(), v.end(), 0LL);
sort(v.begin(), v.end());
int l = sum - y, r = sum - x, cnt = 0;
for (int i = 0; i < n; i++) {
int a = upper_bound(v.begin() + i + 1, v.end(), r - v[i]) - v.begin();
int b = lower_bound(v.begin() + i + 1, v.end(), l - v[i]) - v.begin();
cnt += a - b;
}
cout << cnt << endl;
}
int32_t main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}