Thank you everyone for participating in the round! We hope you enjoyed the problems :)
2280A - Are They the Same?
Problem author: conqueror_of_tourist
What happens when $$$k$$$ is even? What happens when $$$k$$$ is odd?
for _ in range(int(input())):
n, k = list(map(int, input().split()))
a = list(map(lambda x: int(x) % 2, input().split()))
b = list(map(lambda x: int(x) % 2, input().split()))
if k % 2 == 0:
print("YES" if a.count(0) <= b.count(0) else "NO")
else:
print("YES" if b.count(0) <= a.count(0) else "NO")
2280B - Secret String Patterns
Problem author: applepi216
What must all strings whose answer is ``NO'' look like?
import sys
for _ in range(int(input())):
n = int(input())
print("?", 1, n)
sys.stdout.flush()
ct = int(input())
if ct == 0:
print("! NO")
continue
print("?", 1, ct)
sys.stdout.flush()
x = int(input())
if x == ct:
print("! NO")
else:
print("! YES")
2279A - Reverse and Double
Problem author: conqueror_of_tourist
Where is it useful to "cut" the array to decrease the gap value?
Consider separately the cases where the gap value is unique, occurs twice, and occurs three or more times.
#include <bits/stdc++.h>
using namespace std;
template <class T, T (*f)(T, T), T (*unit)()> struct Seg {
vector<T> s; int n;
Seg(int n = 0, T def = unit()) : s(2*n, def), n(n) {}
void update(int pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e) { // query [b, e)
T ra = unit(), rb = unit();
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
T get(int x) { return s[x+n]; }
};
using T = int;
T f(T a, T b) { return max(a, b); }
T unit() { return 0; }
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
Seg<T, f, unit> rmq(n - 1);
vector<int> mx;
int val = -1;
for (int i = 0; i < n - 1; i++) {
rmq.update(i, abs(a[i + 1] - a[i]));
if (abs(a[i + 1] - a[i]) > val)
mx = {i}, val = abs(a[i + 1] - a[i]);
else if (abs(a[i + 1] - a[i]) == val)
mx.push_back(i);
}
if (mx.size() >= 3) {
cout << val << "\n";
continue;
}
auto calc = [&](int i, int j) {
int v2 = 2 * rmq.query(i, j);
if (i > 0) v2 = max({v2, rmq.query(0, i - 1), abs(a[i - 1] - 2 * a[j])});
if (j < n - 1) v2 = max({v2, rmq.query(j + 1, n - 1), abs(a[j + 1] - 2 * a[i])});
return v2;
};
if (mx.size() == 2) {
int i = mx[0], j = mx[1];
cout << min(val, calc(i + 1, j)) << "\n";
continue;
}
int i = mx[0];
for (int j = 0; j <= i; j++) {
val = min(val, calc(j, i));
}
for (int j = i + 1; j < n; j++) {
val = min(val, calc(i + 1, j));
}
cout << val << "\n";
}
}
2279B - Balanced Subsets
Problem author: conqueror_of_tourist
Suppose that $$$|S|\ge 2$$$. Can you say anything about the minimum number of unstable elements in it?
Consider DP.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
const ll MOD = 998244353;
mt19937 rng(time(0));
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n);
rep(i,0,n) cin >> a[i];
vector<bool> s;
s.push_back(true);
for (int i = 1; i < n; i++) {
if (a[i] == a[i - 1] + 1) {
s.push_back(1);
} else if (a[i] == a[i - 1] + 2) {
s.push_back(0); s.push_back(1);
} else {
s.push_back(0); s.push_back(0); s.push_back(1);
}
}
s.push_back(0);
int m = sz(s);
assert(m < 3 * n);
ll ans = (1ll * (n + 1) * n / 2) % MOD; // n + (n C 2)
vector<ll> dp1(m + 1, 1); // ((1110)|(110))*
vector<ll> dp2(m + 1, 0); // 10((1110)|(110))*
for (int i = 1; i < m; i++) {
if (s[i - 1]) dp2[i + 1] = 1;
if (i >= 2 && s[i - 1] && s[i - 2]) {
dp1[i + 1] += dp1[i - 2];
dp2[i + 1] += dp2[i - 2];
}
if (i >= 3 && s[i - 1] && s[i - 2] && s[i - 3]) {
dp1[i + 1] += dp1[i - 3];
dp2[i + 1] += dp2[i - 3];
}
dp1[i + 1] %= MOD;
dp2[i + 1] %= MOD;
ans += dp1[i + 1] - 1 + dp2[i + 1];
}
for (int i = 1; i < m; i++) {
if (s[i - 1]) { // can end in 10
ans += dp1[i - 1] + dp2[i - 1];
}
}
s.push_back(0); s.push_back(0);
// we've overcounted 2 * 10, 110, 101 substrings
for (int i = 0; i < m; i++) {
if (s[i]) ans -= 2;
if (s[i] && s[i + 1]) ans -= 1;
if (s[i] && s[i + 2]) ans -= 1;
}
cout << ((ans + MOD) % MOD) << "\n";
}
}
2279C - Anti-Productive
Problem author: conqueror_of_tourist
What should the array look like after applying your operations? (Equivalently, what operations should you apply?)
Consider casing on the second largest element.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) {
int n; ll L; cin >> n >> L;
vector<ll> a(n);
rep(i,0,n) cin >> a[i];
sort(all(a));
if (L == 1) {
cout << accumulate(all(a), 0ll) - a[n - 1] << "\n";
continue;
}
if (a[n - 1] * a[n - 2] < L) {
cout << "0\n";
continue;
}
ll x = a[n - 1];
ll y = 1;
while ((y + 1) * (y + 1) < L) ++y;
int start = n - 2;
ll curr = 0;
auto mx = [&](ll z) {
if (z == 0) return 0ll;
return max(0ll, x - (L - 1) / z);
};
while (start >= 0 && a[start] >= y) {
curr += (a[start] - y);
--start;
}
ll ans = curr + mx(y);
--y;
for (; y >= 0; --y) {
curr += (n - 2 - start);
while (start >= 0 && a[start] >= y) --start;
ans = min(ans, curr + mx(y));
}
cout << ans << "\n";
}
}
2279D - Meta Graph
Problem author: FlakeLCR
We apologize for the weak test data on this problem.
The graph is quite huge. Do you need all of it?
Try to show that you only need to keep around $$$O(m + n)$$$ edges while preserving the shortest path.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(time(0));
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) {
int n, m; cin >> n >> m;
int w, x, y, z; cin >> w >> x >> y >> z; --w, --x, --y, --z;
vector<array<int, 2>> edges(m);
vector<array<int, 2>> v;
rep(i,0,n) {
v.push_back({w, i});
v.push_back({y, i});
v.push_back({i, i});
}
rep(i,0,m) {
int a, b; cin >> a >> b; --a, --b;
edges[i] = {a, b};
v.push_back({a, b});
v.push_back({b, a});
}
sort(all(v));
v.resize(unique(all(v)) - v.begin());
int N = sz(v);
auto find = [&](int i, int j) -> int {
return lower_bound(all(v), array{i, j}) - v.begin();
};
vector<vector<int>> h(N);
for (auto [a, b] : edges) {
int wa = find(w, a), wb = find(w, b),
ya = find(y, a), yb = find(y, b),
aa = find(a, a), ab = find(a, b),
ba = find(b, a), bb = find(b, b);
for (auto [p1, p2] : vector{array{wa, wb}, {ya, yb}, {aa, ab}, {ba, bb}, {ab, ba}}) {
h[p1].push_back(p2);
h[p2].push_back(p1);
}
}
vector<int> dist(N, -1);
queue<pair<int, int>> q;
q.emplace(find(w, x), 0);
while (!q.empty()) {
auto [vtx, d] = q.front();
q.pop();
if (dist[vtx] >= 0) continue;
dist[vtx] = d;
for (auto a : h[vtx]) {
if (dist[a] < 0) q.emplace(a, d + 1);
}
}
cout << dist[find(y, z)] << "\n";
}
}
2279E - Save Me!
Problem author: conqueror_of_tourist
Can you upper bound the answer?
The answer is at most $$$2\cdot 25 - 1 = 49$$$. Can you use this fact to construct a solution running in time $$$O(n\cdot 2^{49})$$$?
Please refer to the previous hint first. Can you make your solution faster to $$$O(n\cdot \varphi^{49})$$$?
Consider Meet In The Middle.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define sq(a) ((a)*(a))
#define per(i,a,b) for (int i = (b)-1; i >= a; i--)
#define rep(i,a,b) for (int i=a; i<b; i++)
#define FOR(i,a,b) for (int i=a; i<b; i++)
#define F0R(i,a) for (int i=0; i<a; i++)
#define ROF(i,a,b) for (int i = (b)-1; i >= a; i--)
#define R0F(i,a) for (int i = (a)-1; i >= 0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << endl, 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << endl;
#define _ << " _ " <<
#define ll long long
template<class T> bool ckmin(T&a, T&b) { bool B = a > b; a = min(a,b); return B; }
template<class T> bool ckmax(T&a, T&b) { bool B = a < b; a = max(a,b); return B; }
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ld,ld> pld;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<vector<int>> vvi;
const ld PI = acos(-1.0);
const ld EPS = 1e-7;
const int MOD = 998244353;
const int MX = 25;
const int BIG = 100;
void solve() {
int n; cin >> n;
vector<int> a(n);
rep(i, 0, n) {
cin >> a[i];
}
if (*min_element(all(a)) == 0) {
cout << "0" << "\n";
return;
}
vector<pair<int, int>> seen;
auto left = [&](auto && self, int i, int cost, int v) {
int op = v ^ a[i];
seen.push_back({op, cost});
if (i == 0) return;
if (cost + 1 <= MX) {
self(self, i - 1, cost + 1, op);
}
if (cost + 2 <= MX) {
self(self, i - 1, cost + 2, v);
}
};
int best = BIG;
auto right = [&](auto && self, int i, int cost, int v) {
int op = v ^ a[i];
auto it = upper_bound(all(seen), mp(op, -1));
if (it != seen.end()) {
auto [u, v] = *it;
if (u == op) best = min(best, cost + v - 1);
}
if (i + 1 == n) return;
if (cost + 1 <= MX) {
self(self, i + 1, cost + 1, op);
}
if (cost + 2 <= MX) {
self(self, i + 1, cost + 2, v);
}
};
if (n > MX) {
best = 2 * MX - 1;
}
rep(i, 0, n - 1) {
seen.clear();
seen.reserve(514228);
left(left, i, 1, 0);
assert (sz(seen) <= 514228);
sort(all(seen));
right(right, i + 1, 1, 0);
}
if (best == BIG) best = -1;
cout << best << endl;
}
int32_t main() { FAST
int t;
cin >> t;
while (t--) solve();
return 0;
}
2279F - The Stones of Nim
Problem author: applepi216
Recall how one wins in Nim. Next, note that this problem essentially reduces to solving each prefix quickly.
This is, under the hood, a matroid problem. For each new value, try to construct an "exchange" graph using the matroid basis exchange property to determine if it's possible to insert the element.
Please refer to the previous hint first. It gives an $$$O(n^3\log^2 n)$$$ time algorithm. To improve this, consider if you need the full graph or if you can "thin" it.
Make sure your XOR basis operations are of optimal complexity.
#include <bits/stdc++.h>
using namespace std;
constexpr int LOG = 14;
struct xor_basis {
vector<int> inner;
vector<int> base;
vector<int> comb;
xor_basis(): base(LOG), comb(LOG) {}
int represent(int mask) {
int c = 0;
for (int i = 0; i < LOG; i++) {
if (!(mask & (1 << i))) continue;
if (!base[i]) return -1;
mask ^= base[i], c ^= comb[i];
}
return c;
}
bool independent(int mask) {
for (int i = 0; i < LOG; i++) {
if (!(mask & (1 << i))) continue;
if (!base[i]) return true;
mask ^= base[i];
}
return false;
}
void swap(int out, int in) {
int pos = find(inner.begin(), inner.end(), out) - inner.begin();
int c = represent(in);
inner[pos] = in;
for (int i = 0; i < LOG; i++) if (comb[i] >> pos & 1) comb[i] ^= c ^ (1 << pos);
}
bool insert(int mask) {
int sv = mask, c = 1 << inner.size();
for (int i = 0; i < LOG; i++) {
if (!(mask & (1 << i))) continue;
if (!base[i]) {
base[i] = mask, comb[i] = c, inner.push_back(sv);
return true;
}
mask ^= base[i], c ^= comb[i];
}
return false;
}
};
void solve() {
int n; cin >> n;
vector<int> a(n), s(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<xor_basis> bases;
bases.emplace_back();
auto add = [&](int idx) {
for (int i = 0; i < bases.size(); i++) {
if (bases[i].independent(s[idx])) {
bases[i].insert(s[idx]); // special case
return;
}
}
int N = 2*n + bases.size();
vector<int> smap(N, -1);
for (int i = 0; i < bases.size(); i++) for (auto x : bases[i].inner) smap[x - 1] = i;
for (int i = 0; i < bases.size(); i++) smap[i + 2*n] = i;
vector<int> par(N, -2);
vector<int> q;
q.push_back(s[idx] - 1); par[s[idx] - 1] = -1;
int found = -1;
for (int head = 0; head < q.size(); head++) {
int vtx = q[head];
int x = vtx + 1, next_idx = (smap[vtx] + 1) % bases.size();
if (x == s[idx] || bases.size() > 1) {
if (bases[next_idx].independent(x)) {
par[2 * n + next_idx] = vtx, found = 2 * n + next_idx;
break;
}
int z = bases[next_idx].represent(x);
for (int p = 0; p < bases[next_idx].inner.size(); p++) {
if (z >> p & 1) {
int y = bases[next_idx].inner[p] - 1;
if (par[y] == -2) par[y] = vtx, q.push_back(y);
}
}
}
}
if (found < 0) { // easy case -- no path
bases.emplace_back();
bases.back().insert(s[idx]);
return;
} else {
int p = par[found];
assert(bases[smap[found]].insert(p + 1));
while (p + 1 != s[idx]) {
int prev = par[p];
bases[smap[p]].swap(p + 1, prev + 1);
p = prev;
}
}
};
auto print_sol = [&]() {
cout << bases.size() << "\n";
for (const auto& base : bases) {
cout << base.inner.size();
for (auto x : base.inner) {
cout << " " << x;
}
cout << "\n";
}
};
for (int i = 0; i < n; i++) {
int k = (i == 0 ? 0 : bases.size());
s[i] = a[i] ^ k;
add(i);
}
print_sol();
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) solve();
}








Excellent tasks! But i don't liked, how simple was interactive >:(
Still orz round
I have a different solution to Div2C/Div1A with O(n) time.
Simply notice one of the endpoints must be at one of the indexes of the pair with the largest absolute difference, then we can do two for loops for O(n).
i did the same my code
after contest ,even i think that way to traverse from the largest difference point , but i didn't know how to compare the difference after reversing and multiplying by 2, could you explain it to me
fast editorial. before the editorial of last div2 round.
I had a totally different solution to E, diverging when you find out how to calculate the cost given some subset that xors to $$$0$$$. We can bound the answer as $$$2 \cdot B$$$, where $$$B$$$ is the smallest subarray which is not linearly independent. Then for each subarray you can bruteforce all subsets there in $$$O(2^{\text{len} - B})$$$, and then choose the basis elements to make the entire xor $$$0$$$, and the subarray size can be at most $$$2 \cdot B$$$, giving an $$$O(n 2^{25})$$$ solution with $$$O(n)$$$ memory if implemented properly. You can do some constant optimizations / pruning on this and that makes it very fast.
Ok, so I think with a similar argument my pruning makes sure that the complexity becomes $$$O(n \varphi^{25})$$$
When you say you can bruteforce all subsets in $$$2^{len-B}$$$, you mean subsets of what? the elements that can themselves be written as a xor of elements of the basis? How does one such set map 1-1 to a solution that is guaranteed to include the optimal one?
You loop over all subsets of the non-basis elements in the interval. Then the elements you take from the basis are forced, to make sure that the total xor is $$$0$$$. Or put another way, for a number $$$x$$$ that is in the subspace of the basis, there's a unique way to choose a subset of the basis vectors to get xor equal to $$$x$$$, and to make the total xor $$$0$$$.
The casework in the C editorial is unnecessary, even if we assume there is only 1 max absolute difference the algorithm will obviously find the best answer
orz round but i did not expect B as an interactive problem....
Wonderful round! Solved D with 5D dp like dp[i][k][i picked][i-1 picked][i-2 picked] and C with sparse-table :)
segment tree solution for a Div2 C problem?? seriously??!!
you don't need segtree at all. But it was definitely quite a crazy question for div2C. i did it after lot of difficulty and also it can be done in O(n).
Yeah it’s definitely harder than Div.1 C for me. C < A < B
learn segtree
That was mainly for readability. It can also be done with prefix/suffix sums, as mentioned in the tutorial.
you can also solve it with binary search
The samples of Div.1 D seems to be too weak, even the bfs with bugs can pass it. Meanwhile, my friend's dijkstra which doesn't initialize dis as INF can also pass it and get a WA on test 2:)
Intuitive proof for 1D/2F:
Consider the original graph but you start with a red token at w and a blue token at x. Then, the problem is equivalent to moving both to their target positions while performing the operations:
Then clearly the best path is to move the blue token next to the red token and shuffle the red token over to its target position (if it doesn't start there already), then move the blue token to its target position, meaning we only need states where the red token is already at the start or end or they are adjacent or at the same vertex
What a beautiful interpretation! I thought about it as drawing edges between cells of the adjacency matrix
thanks for your beautiful intuition.
the code for Div2 D/ Div1 B is unnecessarily complex. A much simpler DP
Can you explain it here?
Recall that if $$$x \in S$$$, exactly two of $$$x+1$$$, $$$x+2$$$, and $$$x+3$$$ must also belong to $$$S$$$. Therefore, if we think of $$$S$$$ as a sequence, the difference between any two consecutive elements must be either $$$1$$$ or $$$2$$$. However, there are two more restrictions on these differences. We cannot perform the $$$+1$$$ operation three times consecutively, since this would result in four consecutive integers belonging to $$$S$$$. Similarly, we cannot perform the $$$+2$$$ operation twice consecutively, since this would result in $$$x$$$ and $$$x+2$$$ belonging to $$$S$$$, while neither $$$x+1$$$ nor $$$x+3$$$ belong to $$$S$$$.
This motivates our DP: define $$$dp[i][j]$$$ as the number of valid sequences ending at index $$$i$$$, where $$$j$$$ represents the number of consecutive $$$+1$$$ operations that can still be performed before a $$$+2$$$ operation is forced. That counter starts at $$$2$$$. A $$$+1$$$ operation decrements the counter and cannot be performed when it is already $$$0$$$. Similarly, a $$$+2$$$ operation resets the counter to $$$2$$$ and cannot be performed when it is already $$$2$$$. From here, the transitions are simple.
i did it via a state like dp[i][k][i is picked][i-1 is picked][i-2 is picked]
Why editorial for div2D is made so difficult to understand plus there is no mention of dp in it and then the implementation solution contains dp , Can someone explain it in simpler terms?
we can do binary search on answer in div1 A/div2 C
my submission
A brainless $$$O(m^{3/2})$$$ solution also works for D1D: 397780839
In D1F, could you formalize the proof that if a path exists, the application of these exchanges is actually valid?
The main issue I can't resolve is what happens when the path goes through multiple vertices belonging to the same $$$T_j$$$.
If this is what the editorial means when it is written that we should not revisit $$$T_j$$$, then it is unclear why a shortest path satisfies this.
Otherwise, it is really the crux of the proof behind the matroid algorithms (here, matroid partition), that exchanges along the shortest path are valid. Basically, this graph is a thinned out version of the intended graph for matroid partition, and I can't point out why this optimization is correct for our special(?) case.
For 2279A - Reverse and Double I had a slightly different approach that runs in $$$\mathcal{O}(n \log (\max a_i))$$$ time without additional data structures:
Binary search for the lowest bound on the maximum gap that is achievable.
For a given maximum gap, you can calculate if a solution is possible, by first going through the array and counting the number of gaps that are too large.
If that count is 0, you're already done.
If that count is 1, you need to flip a segment that has an endpoint at the gap, extending to the right or to the left of the gap. If you're careful you can check all options in O(n) time.
If that count is 2, you need to flip a segment exactly between the two gaps that are currently too large; this is actually easier than the previous case, because there's only 1 potential segment to consider.
If the count is 3 or more, a solution is impossible, because a single flip-and-double operation can reduce at most 2 gaps, those touching the endpoints of the segment to be reversed.
My submission is here: 397754673 (a bit messy because I had no time to clean it up)
I did the exactly same soluton here: 397796971
In problem E, where do you get the "consequence" that the optimal solution maps to that cost for a specific set from? You only proved an upper bound; in theory it could be the best solution looks very differently even if one ends up specifically xoring those said positions
I was also wondering how to show a lower bound, not just an upper bound.
Consider the bitstring $$$b=b_1b_2\dots b_n$$$ associated with $$$S$$$, where $$$b_i=1$$$ if $$$i\in S$$$ and zero otherwise. We assume $$$b$$$ contains at least one one. Define $$$g(b)$$$ as $$$2(\max(S)-\min(S))-(|S|-1)$$$ for the $$$S$$$ associated with $$$b$$$.
We want to show that $$$g(b)$$$ is both a lower and an upper bound on the required number of operations to get $$$\bigoplus_{i=1}^n(a_i\cdot b_i)$$$ at some array index. Obviously if $$$g(b)=0$$$ (when $$$b$$$ consists of a single one) then no operations are required.
Let's look at how $$$b$$$ transforms across operations. If an operation transforms $$$a\to a'$$$ then we say that the operation transforms $$$b\to b'$$$ where $$$b'$$$ is the unique bitstring such that $$$\bigoplus_{i=1}^n(a_i\cdot b_i)=\bigoplus_{i=1}^n(a'_i\cdot b'_i)$$$.
$$$b'$$$ is related to $$$b$$$ in one of the following ways:
Upper bound: Use operation 2 to make everything one between the min and max one in $$$b$$$, then use operation 3 until there is only one one remaining. This shows that we need at most $$$g(b)$$$ operations.
Lower bound: you can check that in all cases we have $$$g(b')\ge g(b)-1$$$. This shows that we need at least $$$g(b)$$$ operations until we get $$$g(b)=0$$$.
Problem B. Secret String Patterns Video Editorial Solution link: https://youtu.be/KOYEFb9xwdc?si=c_bAwYeu_13eaa63
I have a relatively simpler solution and motivation for div2D/div1B
Let dp[i][k] denote the number of balanced subsets of size at least 3 whose smallest element is i.
Since every element except the two largest must be stable, the smallest element i must also be stable. Therefore, exactly two of {i+1,i+2,i+3} must be selected in the subset.
We can represent these three possibilities using binary strings: "110", "101", and "011". Here, a 1 means that the corresponding element is selected in the subset, while a 0 means it is not selected. More explicitly it means:
Therefore the transitions are:
For k=0: The subset contains i+1 and i+2, but not i+3
For k=1: The subset contains i+1 and i+3, but not i+2
For k=2: The subset contains i+2 and i+3, but not i+1
I came up with the same idea and expected editorial solution to have just simplier DP states, but it was much, much more interesting haha.
Div 2 D was tuff, I had an idea we would need DP, but can anyone explain what should be the state for it? I mean what should dp[x] hold?
1F is just a textbook application of Edmonds’ matroid partition algorithm. Bullshit problem.
Hmm, I don't quite agree? The "textbook application" (https://math.mit.edu/~goemans/18438F09/lec13.pdf) would run in $$$O(n^3)$$$ time or worse which is not fast enough.
It turns out this is very easy to fix, though it is not immediately obvious imo (otherwise, more people would have solved it ...).
https://codeforces.me/contest/2279/submission/397776104
the following line brings the time complexity down from $$$O(n^3)$$$ to $$$O(n^2\log \max(s_i))$$$:
I love this round. Whether I'm doing a VP or playing live, Div. 2 C and D usually have a huge difficulty gap, but this round's curve was a bit gentler. I didn't get D during my VP, but the bitmask DP I had in mind for it was easy to finish after the contest, and E's threshold-enumeration idea isn't that deep either. Overall, I'm just too slow — I really enjoyed this round.
more clean solution on D1C
why there's a $$$l$$$ in the time complexity of E
if you implement a hash table, should the time complexity be $$$O(n \varphi^l)$$$
could you please elaborate?
Nice Round but I think editorial solutions are pretty overkill, my solution for Div1A/Div2C simply involves brute force over what segment should be chosen such that one of the end points involves the pair of indices with the maximum current difference, my code is definitely messy but it's still simpler than coding a sparse or a segtree for a problem C, same with problem Div1B/Div2D, my solution is also Dp but my code is much simpler,
and that's basically the whole logic, (ans initial value is all the cases involving 1 or 2 elements and the ans-- is for compensating over count) Still nice round.
How can I calculate the maximum adjacent element difference for a range (L, R) using prefix and suffix sums as required in 2C? I tried myself for some time with ideas like prefix[i] = rank of maximum adjacent element differences in array A[0...i] But that doesn't quite work..
Let
gap[i] = |a[i] - a[i+1]|and store prefix maxpre[k]and suffix maxsuf[k]ofgap. These cover everything outside the flipped segment.Inside the segment, one end is always next to the biggest gap, so only the other end moves. Keep a running
inner = max(inner, gap[new])as it grows.New max gap for segment
[l, r]:max(pre[l-1], suf[r+1], |a[l-1] - 2*a[r]|, |2*a[l] - a[r+1]|, 2*inner)O(1) per segment O(n) total.
i do not get the dp part of 2279B — Balanced Subsets . can someone give me defination of state and transition in plain english : )
https://codeforces.me/contest/2280/submission/398473185
i hope this helps. the code was written by ai but i have clearly stated my logic in the comments . give it a try !!
also have verified the code myself . it is exactly the logic i have stated in the comment so you will hopefully be able to match the code and the text well
good luck :>
Thank you! This tutorial is really helpful for me!