Codeforces Round 388 (Div. 2) - A-E Editorial
- 1Codeforces Round 162 (Div. 2) - A-E Editorial
- 2Codeforces Round 250 (Div. 1) - A-E Editorial
- 3Codeforces Round 256 (Div. 2) - A-E Editorial
- 4Codeforces Round 267 (Div. 2) - A-E Editorial
- 5Codeforces Round 271 (Div. 2) - A-F Editorial
- 6Codeforces Round 277.5 (Div. 2) - A-F Editorial
- 7Codeforces Round 316 (Div. 2) - A-E Editorial
- 8Codeforces Round 324 (Div. 2) - A-E Editorial
- 9Codeforces Round 388 (Div. 2) - A-E EditorialThis article
- 10Codeforces Round 546 (Div. 2) - A-E Editorial
The auction can be reduced to two surviving final bids. The shuffle can be reduced to how often each inversion lies inside a chosen interval. In both cases, conditioning on the right object removes a much larger simulation.
This post covers every problem, A-E, from Codeforces Round 388 (Div. 2). Each section contains a self-contained C++17 solution and its reasoning. The shorter opening problems are included so that the round is complete; the later sections carry the deeper techniques.
Round map
| Problem | Focus |
|---|---|
| A. Bachgold Problem | greedy, implementation, math |
| B. Parallelogram is Back | brute force, constructive algorithms, geometry |
| C. Voting | greedy, implementation, two pointers |
| D. Leaving Auction | binary search, data structures |
| E. Inversions After Shuffle | data structures, probabilities |
A picture of the main reduction
A. Bachgold Problem
Problem statement · Download the complete C++17 solution
Derive the algorithm
Every prime is at least two, so at most floor(n/2) primes can sum to n. Use all twos for even n, or one three and the remaining twos for odd n.
Why it works
The lower bound on a prime’s value gives the upper bound on the number of summands. The construction attains that bound and consists entirely of primes, proving optimality.
Complexity. O(n) output time and O(1) auxiliary space.
Implementation check. For odd n, replace a pair of value two plus the leftover one by a single three.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { int n; cin >> n; cout << n / 2 << '\n'; if (n % 2) { cout << 3 << ' '; n -= 3; } while (n) { cout << 2 << ' '; n -= 2; } cout << '\n';}B. Parallelogram is Back
Problem statement · Download the complete C++17 solution
Derive the algorithm
Any two of the three given non-collinear points can be adjacent to the missing corner’s opposite point. For each choice of opposite point A, the missing corner is B+C-A. Print the three resulting points.
Why it works
A parallelogram’s diagonals share a midpoint, so opposite vertices satisfy A+D=B+C. Choosing each given vertex as A yields every possible completion. Non-collinearity guarantees the three completions are distinct.
Complexity. O(1) time and space.
Implementation check. Vector addition applies to coordinates independently; no angle or slope calculation is needed.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { ll x[3], y[3]; for (int i = 0; i < 3; i++) cin >> x[i] >> y[i]; cout << 3 << '\n'; for (int i = 0; i < 3; i++) cout << x[(i + 1) % 3] + x[(i + 2) % 3] - x[i] << ' ' << y[(i + 1) % 3] + y[(i + 2) % 3] - y[i] << '\n';}C. Voting
Problem statement · Download the complete C++17 solution
Derive the algorithm
Keep a queue of the next active turn index for each party. Compare their front indices: the earlier senator bans the later one and is reinserted at its index plus n. Continue until a party has no active senators.
Why it works
At the earliest remaining turn, banning the earliest upcoming opponent is never worse than banning a later opponent, because it prevents an action sooner. With that exchange argument, the greedy ban is optimal. Adding n preserves cyclic order for the winner’s next turn.
Complexity. O(n) time and O(n) space: each duel permanently removes one senator.
Implementation check. Queue indices grow across rounds; store turn indices in a sufficiently wide integer type.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { int n; string s; cin >> n >> s; queue<ll> d, r; for (int i = 0; i < n; i++) (s[i] == 'D' ? d : r).push(i); while (!d.empty() && !r.empty()) { ll a = d.front(), b = r.front(); d.pop(); r.pop(); if (a < b) d.push(a + n); else r.push(b + n); } cout << (d.empty() ? "R" : "D") << '\n';}D. Leaving Auction
Problem statement · Download the complete C++17 solution
Derive the algorithm
A bidder’s last bid determines whether they can outlast another bidder. Keep all bidders ordered by last bid. Temporarily remove the excluded bidders for a query. The largest remaining last bid identifies the winner; the second largest last bid is the price threshold. Print the winner’s first bid strictly above that threshold, or their first bid if no competitor remains.
Why it works
Every other active bidder has stopped by the second-largest last bid. Before that threshold the eventual winner could still be challenged; its first later bid is therefore the winning price. Removing and restoring exclusions changes exactly the bidder set for that query.
Complexity. O((n+q+K) log n) time and O(n) space, where K is the total exclusion-list length.
Implementation check. Some excluded ids may have no bids. Restore only entries actually removed, and handle the no-bidder answer as 0 0.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<vector<int>> bids(n + 1); set<pair<int, int>> active; for (int i = 0, id, b; i < n; i++) { cin >> id >> b; bids[id].push_back(b); } for (int id = 1; id <= n; id++) if (!bids[id].empty()) active.insert({bids[id].back(), id}); int q; cin >> q; while (q--) { int k; cin >> k; vector<pair<int, int>> removed; while (k--) { int id; cin >> id; if (id <= n && !bids[id].empty()) { auto p = make_pair(bids[id].back(), id); if (active.erase(p)) removed.push_back(p); } } if (active.empty()) cout << "0 0\n"; else { auto it = prev(active.end()); int id = it->second, threshold = 0; if (it != active.begin()) threshold = prev(it)->first; cout << id << ' ' << *upper_bound(bids[id].begin(), bids[id].end(), threshold) << '\n'; } for (auto p : removed) active.insert(p); }}E. Inversions After Shuffle
Problem statement · Download the complete C++17 solution
Derive the algorithm
Fix the shuffled interval first. Inversions with one endpoint outside it have the same total count after shuffling, since the interval retains the same multiset. Only internal pairs change, and each internal pair is inverted with probability one half.
An original inversion (i,j) lies inside exactly intervals, using one-based indices. A Fenwick tree of previous index sums computes the weighted inversion total W. If I is the original inversion count and , then
The final term follows by summing over all pairs i<j; that sum is .
Why it works
Conditioning on the interval proves the unchanged outside contribution. Double-counting each inversion by the intervals containing both endpoints gives W. The same count applied to all index pairs gives the new expected internal contribution, so linearity of expectation yields the formula without any independence assumption between different pairs.
Complexity. O(n log n) time and O(n) space.
Implementation check. Use floating-point division only after accumulating exact integer counts. Compute the polynomial term in long double to avoid intermediate overflow.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
struct BIT { int n; vector<ll> t; BIT(int n) : n(n), t(n + 1) {} void add(int p, ll v) { for (; p <= n; p += p & -p) t[p] += v; } ll sum(int p) { ll s = 0; for (; p; p -= p & -p) s += t[p]; return s; }};int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; BIT cnt(n), weight(n); ll inv = 0, W = 0; for (int j = 1, x; j <= n; j++) { cin >> x; inv += j - 1 - cnt.sum(x); W += (n - j + 1) * (1LL * j * (j - 1) / 2 - weight.sum(x)); cnt.add(x, 1); weight.add(x, j); } long double M = (long double)n * (n + 1) / 2, ans = inv - (long double)W / M + (long double)(n - 1) * (n + 2) / 24; cout << fixed << setprecision(15) << ans << '\n';}Support & Share
If this article helped you, please share or support!





























































Conversation
Comments powered by Waline