Codeforces Round 277.5 (Div. 2) - A-F 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 EditorialThis article
- 7Codeforces Round 316 (Div. 2) - A-E Editorial
- 8Codeforces Round 324 (Div. 2) - A-E Editorial
- 9Codeforces Round 388 (Div. 2) - A-E Editorial
- 10Codeforces Round 546 (Div. 2) - A-E Editorial
The fractional objective in Hiking looks global until a guessed ratio makes every jump additive. Special Matrices looks like a matrix DP until columns are grouped by remaining deficit. The early problems provide the exchange arguments and counting discipline used again in those two finales.
This post covers every problem, A-F, from Codeforces Round 277.5 (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. SwapSort | greedy, implementation, sortings |
| B. BerSU Ball | dfs and similar, dp, graph matchings |
| C. Given Length and Sum of Digits… | dp, greedy, implementation |
| D. Unbearable Controversy of Being | brute force, combinatorics, dfs and similar |
| E. Hiking | binary search, dp |
| F. Special Matrices | combinatorics, dp |
A picture of the main reduction
A. SwapSort
Problem statement · Download the complete C++17 solution
Derive the algorithm
At position i, find the minimum value in the suffix and swap it into position i. Record only nontrivial swaps. This is selection sort with the operations exposed.
Why it works
After fixing a prefix, the smallest remaining value belongs immediately after that prefix. Placing it there preserves the invariant that every fixed position equals the final sorted array. At most one swap is needed per position.
Complexity. O(n^2) time and O(n) space.
Implementation check. The problem uses zero-based indices for printed swaps.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; vector<pair<int, int>> ops; for (int i = 0; i < n; i++) { int j = min_element(a.begin() + i, a.end()) - a.begin(); if (i != j) { swap(a[i], a[j]); ops.push_back({i, j}); } } cout << ops.size() << '\n'; for (auto [i, j] : ops) cout << i << ' ' << j << '\n';}B. BerSU Ball
Problem statement · Download the complete C++17 solution
Derive the algorithm
Sort both skill lists. If the smallest remaining pair differs by at most one, match it. Otherwise discard the smaller skill, which is too small to match any remaining person from the other list.
Why it works
When a compatible smallest pair exists, any optimal matching can be uncrossed to include it without reducing its size. When it does not exist, the smaller endpoint cannot match any larger endpoint either, so discarding it cannot remove an opportunity.
Complexity. O(n log n+m log m) time and O(n+m) space.
Implementation check. After a successful match, advance both pointers.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { int n, m; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; cin >> m; vector<int> b(m); for (int &x : b) cin >> x; sort(a.begin(), a.end()); sort(b.begin(), b.end()); int i = 0, j = 0, ans = 0; while (i < n && j < m) if (abs(a[i] - b[j]) <= 1) i++, j++, ans++; else if (a[i] < b[j]) i++; else j++; cout << ans << '\n';}C. Given Length and Sum of Digits…
Problem statement · Download the complete C++17 solution
Derive the algorithm
A feasible digit sum lies between 1 and 9m, except that the single digit zero is allowed. Build the largest number by spending as much sum as possible at each position. For the smallest number, reserve one for the first digit, then spend the remaining sum from right to left.
Why it works
Lexicographic order equals numerical order at a fixed digit length. The largest construction maximizes the earliest digit while preserving feasibility. The smallest construction pushes every available unit as far right as possible, with its reserved leading one ensuring the required length.
Complexity. O(m) time and space.
Implementation check. For m=1 and s=0, both answers are 0. Every other zero-sum case is impossible.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { int m, s; cin >> m >> s; if (s == 0) { cout << (m == 1 ? "0 0" : "-1 -1") << '\n'; return 0; } if (s > 9 * m) { cout << "-1 -1\n"; return 0; } string lo(m, '0'), hi(m, '0'); int rem = s; for (char &c : hi) { int d = min(9, rem); c += d; rem -= d; } lo[0] = '1'; rem = s - 1; for (int i = m - 1; i >= 0; i--) { int d = min(9 - (lo[i] - '0'), rem); lo[i] += d; rem -= d; } cout << lo << ' ' << hi << '\n';}D. Unbearable Controversy of Being
Problem statement · Download the complete C++17 solution
Derive the algorithm
Fix the common start s. For every two-edge walk with , increment count[t]. Choosing two distinct middle vertices among these walks gives valid diamonds. Sum over starts and ends.
Why it works
In a simple directed graph a two-edge walk is uniquely identified by its middle vertex once its endpoints are fixed. Thus choosing two such walks is exactly choosing the two distinct middle vertices of a diamond. Excluding t=s enforces the four-distinct-vertices condition; the absence of self-loops handles the other endpoint coincidences.
Complexity. O(nm) time in the worst case and O(n+m) space.
Implementation check. Clear endpoint counts for each start, and use 64-bit arithmetic for the answer.
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, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0, u, v; i < m; i++) { cin >> u >> v; g[u - 1].push_back(v - 1); } ll ans = 0; vector<int> cnt(n); for (int s = 0; s < n; s++) { fill(cnt.begin(), cnt.end(), 0); for (int u : g[s]) for (int t : g[u]) if (t != s) ans += cnt[t]++; } cout << ans << '\n';}E. Hiking
Problem statement · Download the complete C++17 solution
Derive the algorithm
For a proposed ratio R, assign a jump from j to i the transformed cost . A route has frustration/picturesqueness at most R exactly when its transformed total is nonpositive.
The positions form a DAG, so ordinary quadratic DP finds the minimum transformed total to the final stop. Binary-search R between zero and the ratio of the direct jump to the last stop. Store predecessors during a final feasible DP and recover the route.
Why it works
Every route has positive total picturesqueness, so multiplying the ratio inequality by its denominator preserves its direction. The transformed costs add over jumps. DAG DP enumerates every route through its last jump, and feasibility is monotone in R, which justifies binary search and reconstruction.
Complexity. O(n^2 log(1/epsilon)) time and O(n^2) space here to precompute jump frustrations.
Implementation check. The square root applies to the absolute deviation from l. The route must end at the farthest stop; do not minimize over arbitrary final stops.
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; ll l; cin >> n >> l; vector<ll> x(n + 1), b(n + 1); for (int i = 1; i <= n; i++) cin >> x[i] >> b[i]; vector<vector<double>> w(n + 1, vector<double>(n + 1)); for (int i = 1; i <= n; i++) for (int j = 0; j < i; j++) w[j][i] = sqrt((double)llabs(x[i] - x[j] - l)); vector<int> pre(n + 1); auto check = [&](double r) { vector<double> dp(n + 1, 1e100); dp[0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j < i; j++) { double z = dp[j] + w[j][i] - r * b[i]; if (z < dp[i]) dp[i] = z, pre[i] = j; } return dp[n] <= 0; }; double lo = 0, hi = w[0][n] / b[n]; for (int it = 0; it < 70; it++) { double mid = (lo + hi) / 2; if (check(mid)) hi = mid; else lo = mid; } check(hi + 1e-12); vector<int> path; for (int v = n; v; v = pre[v]) path.push_back(v); reverse(path.begin(), path.end()); for (int v : path) cout << v << ' '; cout << '\n';}F. Special Matrices
Problem statement · Download the complete C++17 solution
Derive the algorithm
Only column deficits matter. If z columns still need two ones and o columns need one, a new row can choose two zero-count columns, one of each kind, or two one-count columns. Their multiplicities are , , and .
After r rows remain, , so o is determined by z and r. This removes one DP dimension and gives a quadratic algorithm even when the modulus is composite.
Why it works
A column’s identity affects only how many choices realize a transition, not the future structure once its deficit class is known. The three cases exhaust all pairs of distinct columns and are disjoint. Their class updates preserve the remaining-deficit equation, so the compressed DP counts each completion exactly once.
Complexity. O(n^2) time and O(n) auxiliary space.
Implementation check. Do not divide modulo mod: it need not be prime. Compute the integer binomial coefficient before reducing.
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, m; ll mod; cin >> n >> m >> mod; vector<int> cnt(n); for (int i = 0; i < m; i++) { string s; cin >> s; for (int j = 0; j < n; j++) cnt[j] += s[j] == '1'; } int z = count(cnt.begin(), cnt.end(), 0), r = n - m; vector<ll> dp(n + 1), next(n + 1); dp[z] = 1; for (; r > 0; r--) { fill(next.begin(), next.end(), 0); for (int zero = 0; zero <= n; zero++) if (dp[zero]) { int one = 2 * r - 2 * zero; if (one < 0 || one + zero > n) continue; auto add = [&](int nz, ll ways) { next[nz] = (next[nz] + dp[zero] * (ways % mod)) % mod; }; if (zero >= 2) add(zero - 2, 1LL * zero * (zero - 1) / 2); if (zero && one) add(zero - 1, 1LL * zero * one); if (one >= 2) add(zero, 1LL * one * (one - 1) / 2); } dp.swap(next); } cout << dp[0] << '\n';}Support & Share
If this article helped you, please share or support!





























































Conversation
Comments powered by Waline