2108A - Permutation Warm-Up
Author: eugenechka.boyko.2_0-0
Let’s start with $$$p = [1, 2, 3, …, n]$$$. For this $$$p$$$, $$$f(p) = 0$$$.
Let’s move $$$n$$$ to the beginning one position at a time. It’s easy to see that we increase the answer by $$$2$$$ at each step.
Now, let’s move $$$n - 1$$$ to position $$$2$$$, then $$$n - 2$$$ to position $$$3$$$, and so on, until we reach $$$p = [n, n - 1, …, 2, 1]$$$.
For this $$$p$$$, $$$f(p) = \lfloor \frac{n^2}{2} \rfloor$$$.
While doing this, we obtained every even value from $$$0$$$ to $$$\lfloor \frac{n^2}{2} \rfloor$$$, since we were adding $$$+2$$$ at each step.
Let’s prove that we can’t get any other values.
First of all, it’s easy to see that we can’t obtain a value less than $$$0$$$ or greater than $$$\lfloor \frac{n^2}{2} \rfloor$$$.
Second, we can only obtain even values, because each swap changes the answer by an even number.
So the answer is $$$\lfloor \frac{n^2}{4} \rfloor + 1$$$
Time complexity : $$$\text{O}(1)$$$.
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
#define SPEEDY std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);
#define forn(i, n) for (ll i = 0; i < ll(n); ++i)
void solution(){
ll n;cin>>n;
ll k=n/2;
if (n%2){cout<<k*(k+1)+1;return;}
cout<<k*k+1;
}
int main() {
SPEEDY;
int t; cin>>t;
while (t--){
solution();
cout << '\n';
} return 0;
}
2108B - SUMdamental Decomposition
Author: eugenechka.boyko.2_0-0
Let $$$x \gt 1$$$, and let $$$c$$$ denote the number of 1-bits in its binary representation. Clearly, when $$$n \le c$$$, it is optimal to simply distribute distinct powers of two across the elements of the array, resulting in the minimum achievable sum of $$$x$$$. If, however, $$$n \gt c$$$, then it is obviously beneficial to add only ones to the extra $$$n - c$$$ elements. In the case where $$$n - c$$$ is odd, we will also need to add one more one to one of the $$$c$$$ blocks with powers of two so that the $$$\text{XOR}$$$ of all ones equals $$$x$$$ $$$\text{mod}$$$ $$$2$$$.
If $$$x = 1$$$, then for odd $$$n$$$ we can clearly just fill all array elements with ones. Otherwise, we need to use the pair $$$[2, 3]$$$, whose $$$\text{XOR}$$$ is $$$1$$$, resulting in the minimum score of $$$n + 3$$$.
In the remaining case of $$$x = 0$$$, the situation is nearly identical to the previous one, with the exception that no valid example exists for $$$n = 1$$$ (this is the only case with an answer of $$$-1$$$). So for even $$$n$$$, the answer is simply $$$n$$$, and otherwise it’s $$$n + 3$$$ (since we use the triple $$$1 \oplus 2 \oplus 3 = 0$$$).
The time complexity is $$$\text{O}(1)$$$ per test.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
void solution(){
int n,x;cin>>n>>x;
int bits=__builtin_popcountll(x);
if (n<=bits){cout<<x;return;}
if ((n-bits)%2==0)cout<<x+n-bits;
else{
if (x>1){cout<<x+n-bits+1;return;}
if (x==1){cout<<n+3;return;}
else{
if (n==1){cout<<-1;return;}
else cout<<n+3;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
cin>>t;
while (t--){
solution();
cout << '\n';
} return 0;
}
2108C - Neo's Escape
Author: suprend
Note that consecutive buttons with the same weight do not affect the result, so we will keep only one of them in such sequences.
In the resulting array, we find peaks (local maxima — elements that are strictly greater than both of their neighbors). The number of such peaks is the answer, because:
— Each peak is separated from others by smaller elements. Therefore, the only way to reach a peak is by creating a clone there.
— If a button was visited, we can return to it.
— Any element other than a peak can be reached from a larger neighbor, since we have already visited it. Thus, creating clones in all other elements is not necessary.
Complexity: $$$\text{O}(n)$$$
#include <iostream>
#include <vector>
using namespace std;
int main() {
int tt = 1;
cin >> tt;
while(tt--){
int n; cin >> n;
vector<int> a;
a.push_back(-1e9);
for (int i = 0; i < n; i++){
int x; cin >> x;
if (a.back() == x);
else a.push_back(x);
}
a.push_back(-1e9);
int ans = 0;
for (int i = 1; i < a.size() - 1; i++)
if (a[i - 1] < a[i] && a[i] > a[i + 1]) ans++;
cout << ans << endl;
}
}
2108D - Needle in a Numstack
Author: m3tr0
Let $$$k = 4$$$ and the following array is guessed:
[ 2 4 3 1 2 4 3 1 2 1 3 2 4 1 3 2 4 1 ]
For clarity, let's divide it into groups of $$$k$$$ elements:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 ]
By statement, the lengths of the left and right sides are at least $$$k$$$. Let's request the left $$$k$$$ and right $$$k$$$ elements. We will mark all the elements of the left array in red, and all the elements of the right array in blue:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 ]
Let's add two numbers to our array on the right (for clarity of permutations):
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 3 2 ]
We got the permutations in a "normalized" form: $$$[2, 4, 3, 1]$$$ and $$$[4, 1, 3, 2]$$$. Let's take any position where the elements in the permutations differ. Let it be the second position. Note all the elements at the selected positions in the guessed array:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 3 2 ]
Among them, we find boundary elements via binary search for $$$\log\frac{n}{k}$$$ queries:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 3 2 ]
Next, consider the segment between these boundary elements:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 3 2 ]
We are not interested in positions that match in both permutations (in our case, this is only the third position). Let's discard the elements in these positions:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 3 2 ]
Using binary search, we find the boundary elements for $$$\log k$$$ queries:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 3 2 ]
If there are "no-man's land" between the boundary elements, then there is no single solution and we output $$$-1$$$. In our case, there are no such elements, the problem is solved:
[ 2 4 3 1 ] [ 2 4 3 1 ] [ 2 1 3 2 ] [ 4 1 3 2 ] [ 4 1 ]
Total complexity: $$$2k + \log\frac{n}{k} + \log k$$$ queries.
#include <cstdio>
#include <cstdlib>
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
typedef long long l;
l a[55], b[55], ui[55];
l ask(l v) {
printf("? %lld\n", v + 1);
fflush(stdout);
l t; scanf("%lld", &t);
return t;
}
void noans() {
printf("! -1\n");
fflush(stdout);
}
void ans(l a, l b) {
printf("! %lld %lld\n", a, b);
fflush(stdout);
}
void solve() {
l n, k; scanf("%lld %lld", &n, &k);
for (l i = 0; i < k; ++i) a[i] = ask(i);
for (l i = n - k; i < n; ++i) b[i % k] = ask(i);
l uc = 0;
for (l i = 0; i < k; ++i) if (a[i] != b[i]) ui[uc++] = i;
if (!uc) {
if (n == k * 2) ans(k, k);
else noans();
return;
}
l le = ui[0], ri = ui[0] + (n - 1) / k * k;
while (le + k != ri) {
l mid = le + (ri - le) / k / 2 * k;
if (ask(mid) == a[ui[0]]) le = mid;
else ri = mid;
}
l lee = 0, rii = uc;
while (lee + 1 != rii) {
l mid = (lee + rii) / 2;
if (ask(le - ui[0] + ui[mid]) == a[ui[mid]]) lee = mid;
else rii = mid;
}
l pos1 = MAX(le - ui[0] + ui[lee], k - 1);
l pos2 = MIN(le - ui[0] + ((rii == uc) ? (ui[0] + k) : ui[rii]), n - k);
if (pos1 + 1 != pos2) { noans(); return; }
ans(pos2, n - pos2);
}
int main() {
l t; scanf("%lld", &t);
while (t--) solve();
}
2108E - Spruce Dispute
Author: eugenechka.boyko.2_0-0
Let’s assume we have already removed one of the edges from the original tree. Then, we are left with a tree containing an even number of vertices, namely $$$n - 1$$$.
Note that the maximum possible sum of distances between same-colored balls in this tree will be achieved if, for every edge, all vertices in its smaller subtree are of different colors. In that case, the edge will contribute the maximum possible number of times to the answer — specifically, $$$\min(a, b)$$$, where $$$a$$$ and $$$b$$$ are the sizes of the subtrees connected by this edge (since, obviously, no more paths can pass through this edge).
Great — now let’s root our tree at its centroid, which is a vertex that splits the tree into subtrees whose sizes do not exceed half of the total size. It can be proven that such a vertex always exists in any tree (and if there are two such vertices, we can pick either).
Now, let’s divide all vertices into groups based on which child subtree of the centroid they belong to, adding the centroid itself to the smallest group. Notice that the sizes of all groups still do not exceed $$$\frac{n - 1}{2}$$$. Therefore, we can pair up the vertices in such a way that each pair contains vertices from different groups. This can be done greedily using a heap in $$$O(n \log n)$$$, or by running DFS from the centroid and coloring vertices modulo $$$\frac{n - 1}{2}$$$ (clearly, with this method, no group will have two vertices of the same color), which has complexity $$$O(n)$$$.
Thus, we are left to determine which edge we should remove so that the total sum of the minimal subtree sizes across all edges in the original tree decreases as little as possible. Note that, since initially the tree has an odd number of vertices, its centroid is uniquely defined and will not change no matter which edge we remove. At the same time, since all paths pass through the centroid, the total sum of their lengths can be rewritten as the sum of distances to the centroid.
Now, let’s make two observations:
- It is more beneficial to remove a leaf than any other type of vertex.
- Among all leaves, it is best to remove the one closest to the centroid.
The second observation follows directly from the first and the earlier remark, so we only need to justify the first one. But this is fairly obvious: when we remove an edge, besides subtracting the depth of its nearest vertex, we also decrease the depths of all vertices in its subtree by one. Therefore, it will always be better to remove deeper edges rather than shallower ones.
As a result, after finding the centroid in $$$O(n)$$$, we get an overall complexity of $$$O(n \log n)$$$ (if we use a heap) or $$$O(n)$$$.
#include <cstdio>
#include <vector>
#define S 200005
using namespace std;
typedef long long l;
l coloring[S], centroid, best, best_dist, n, color;
vector<vector<l>> g;
l search_centroid(l u, l from) {
l sum = 0;
bool f = true;
for (l v : g[u]) if (v != from) {
l t = search_centroid(v, u);
if (t > n / 2) f = false;
sum += t;
}
if (f && n - 1 - sum <= n / 2) centroid = u;
return sum + 1;
}
void make_coloring(l u, l from, l dist) {
coloring[u] = (color++) % (n / 2) + 1;
if (g[u].size() == 1 && dist < best_dist) {
best_dist = dist;
best = u;
}
for (l v : g[u]) if (v != from)
make_coloring(v, u, dist + 1);
}
void solve() {
centroid = -1, best_dist = S, color = 0;
scanf("%lld", &n);
g.assign(n, vector<l>());
for (l i = 0; i < n - 1; ++i) {
l u, v; scanf("%lld %lld", &u, &v); --u, --v;
g[u].push_back(v); g[v].push_back(u);
}
search_centroid(1543 % n, -1);
make_coloring(centroid, -1, 0);
l bbest = max(best, g[best][0]);
coloring[centroid] = coloring[bbest];
coloring[bbest] = 0;
printf("%lld %lld\n", best + 1, g[best][0] + 1);
for (l i = 0; i < n; ++i) {
if (i) printf(" ");
printf("%lld", coloring[i]);
}
printf("\n");
}
int main() {
l tc; scanf("%lld", &tc);
while (tc--) solve();
}
2108F - Fallen Towers
Author: m3tr0
It can be shown that for any array $$$ A $$$ obtained after collapsing all $$$ n $$$ towers, we can also obtain any other array $$$ B $$$ whose elements do not exceed the elements of $$$ A $$$. A strict formal proof is provided in the spoiler below; here is a brief explanation. Suppose $$$ k $$$ blocks fell onto the $$$ i $$$-th tower over the entire process. Then, for its height in the final array to be $$$ A_i $$$, $$$ A_i $$$ blocks must have fallen after its collapse, while the remaining $$$ k - A_i $$$ blocks fell before. Thus, we can always rearrange the order in which the towers collapse so that its height becomes $$$ B_i \leq A_i $$$. In other words, we ensure that $$$ B_i $$$ blocks fall onto the $$$ i $$$-th tower after its collapse, and $$$ k - B_i $$$ fall before.
From this claim about the possibility of obtaining a smaller array, two facts follow:
For any answer $$$\text{MEX} = x$$$, we can also obtain the answer $$$ x - 1 $$$. This means we can apply binary search on the answer.
For any answer $$$\text{MEX} = x$$$, we can achieve it in the form $$$[0, 0, 0, 0, \ldots, 1, 2, 3, \ldots, x - 1]$$$. Thus, at each iteration of the binary search, we need to check the possibility of obtaining such an array. To do this, we traverse the towers from left to right, tracking the number of cubes that fall onto each tower at some point and collapsing each tower after the required number of cubes have fallen onto it. The most efficient way to track the number of cubes is using the scanline method.
In total, there are $$$\text{O}(\log n)$$$ iterations in the binary search over the answer and $$$\text{O}(n)$$$ operations per iteration.
The final complexity is $$$\text{O}(n \log n)$$$.
Let the array $$$a$$$ of length $$$n$$$ be some input data for the problem.
Let $$$r$$$ and $$$r'$$$ be arrays of length $$$n$$$ such that $$$\forall i : 0 \leq r'_i \leq r_i$$$.
Assertion 1: Suppose there exists a permutation $$$p$$$ such that the tower with index $$$i$$$ collapses $$$p_i$$$-th in order, resulting in the array $$$r$$$. Then, there exists a permutation $$$p'$$$ such that the tower with index $$$i$$$ collapses $$$p'_i$$$-th in order, resulting in the array $$$r'$$$.
$$$\square$$$
Let $$$s_i$$$ be the total number of towers that ever fell onto position $$$i$$$ when collapsing the towers in the order $$$p$$$. Note that out of these, $$$r_i$$$ towers were collapsed later than the $$$i$$$-th, and the rest were collapsed earlier. The height of the $$$i$$$-th tower at the moment of its collapse is $$$a_i + (s_i - r_i)$$$.
Inductive hypothesis: There exists a permutation $$$p^{(k)}$$$ of numbers from $$$1$$$ to $$$k \leq n$$$ such that if the $$$i$$$-th tower is collapsed $$$p^{(k)}_i$$$-th in order, then:
The resulting height at the $$$i$$$-th position will be $$$r'_i$$$.
The number (denoted as $$$s^{(k)}_i$$$) of towers that fell onto position $$$i$$$ when collapsing the towers in the order $$$p^{(k)}$$$ is greater than or equal to $$$s_i$$$.
Base case: The permutation $$$p^{(1)} = [1]$$$. After collapsing, the tower's height becomes $$$0$$$, so $$$r'_1 = r_1 = 0$$$ in any resulting array. $$$s^{(1)}_1 = s_1 = 0$$$.
Inductive step: Suppose the permutation $$$p^{(k - 1)}$$$ exists. We prove the existence of $$$p^{(k)}$$$.
$$$\forall i \leq k - 1$$$, the height of the $$$i$$$-th tower at the moment of collapse under the order $$$p^{(k - 1)}$$$ is no less than under the order $$$p$$$:
From this, it follows that when collapsing in the order $$$p^{(k - 1)}$$$, at least $$$s_k$$$ towers will fall onto position $$$k$$$ (let their count be $$$x \geq s_k \geq r'_k$$$). This is because the heights at the moment of collapse for all towers $$$i \lt k$$$ have either remained the same or increased.
If $$$x \gt r'_k$$$, let tower $$$j$$$ be the $$$(x - r'_k)$$$-th tower among those that fell onto position $$$k$$$. Then, collapse tower $$$k$$$ immediately after tower $$$j$$$. If $$$x = r'_k$$$, collapse tower $$$k$$$ first. In both cases, after collapsing $$$k$$$, $$$r'_k$$$ towers will fall onto it, and its height will become $$$r'_k$$$.
Formally, for $$$x \gt r'_k$$$, the permutation $$$p^{(k)}$$$ is constructed as follows:
$$$\forall i \lt k : p^{(k - 1)}_i \leq p^{(k - 1)}_j \Rightarrow p^{(k)}_i = p^{(k - 1)}_i$$$
$$$p^{(k)}_k = p^{(k - 1)}_j + 1$$$
$$$\forall i \lt k : p^{(k - 1)}_i \gt p^{(k - 1)}_j \Rightarrow p^{(k)}_i = p^{(k - 1)}_i + 1$$$
For $$$x = r'_k$$$:
$$$p^{(k)}_k = 1$$$
$$$\forall i \lt k : p^{(k)}_i = p^{(k - 1)}_i + 1$$$
The array $$$s^{(k)}$$$ is constructed as $$$\forall i \lt k : s^{(k)}_i = s^{(k - 1)}_i$$$ and $$$s^{(k)}_k = x$$$.
The induction is proven. Thus, we set $$$p' = p^{(n)}$$$, and the assertion is proven.
$$$\blacksquare$$$
Corollary 1: If we can obtain $$$\text{MEX}(r) \gt 0$$$ as the answer to the problem for the resulting array $$$r$$$, then we can also obtain $$$\text{MEX}(r) - 1$$$ as the answer to the problem.
$$$\square$$$
Set $$$r'_i = \max(0, r_i - 1)$$$. Then, by Assertion 1, $$$r'$$$ can be obtained as the resulting array. $$$\text{MEX}(r') = \text{MEX}(r) - 1$$$.
$$$\blacksquare$$$
Corollary 2: If we can obtain $$$\text{MEX}(r)$$$ as the answer to the problem for the resulting array $$$r$$$, then we can obtain $$$\text{MEX}(r') = \text{MEX}(r)$$$ for the resulting array $$$r'$$$, where $$$r'_i = \max(0, (\text{MEX}(r) - 1) - (n - i))$$$.
$$$\square$$$
By Assertion 1, $$$r'$$$ can be obtained as the resulting array.
$$$\blacksquare$$$
We can perform binary search on the answer (from Corollary 1), and at each iteration, when checking the answer $$$x$$$, verify whether we can obtain the resulting array $$$r$$$ of the form $$$r_i = \max(0, (x - 1) - (n - i))$$$ (from Corollary 2).
#include <cstdio>
#include <algorithm>
#include <cstring>
#define S 100005
typedef long long l;
l a[S], d[S], n;
bool check(l ans) {
memset(d, 0, sizeof(l) * n);
l acc = 0;
for (l i = 0; i < n; ++i) {
acc -= d[i];
l need = std::max(0LL, i - (n - ans));
if (acc < need) return false;
l end = i + a[i] + (acc++) - need + 1;
if (end < n) ++d[end];
}
return true;
}
void solve() {
scanf("%lld", &n);
for (l i = 0; i < n; ++i) scanf("%lld", &a[i]);
l le = 1, ri = n + 1, mid;
while (ri - le > 1) {
mid = (le + ri) / 2;
if (check(mid)) le = mid;
else ri = mid;
}
printf("%lld\n", le);
}
int main() {
l tc; scanf("%lld", &tc);
while (tc--) solve();
}








Auto comment: topic has been updated by eugenechka.boyko.2_0-0 (previous revision, new revision, compare).
Shouldn’t it be, “each swap changes the answer by an even number”?
There is another nitpick as well. Sorry about these. Shouldn't it be n+3 NOT x+3 for Problem B?
Problem F is awesome, thanks for the contest!
As A participant I reeally enjoy thanks for your contest.
The solution for problem F is poetic
Problem A is an easier version of this.
Can you please tell me how you found this problem, or did you remember it from previous experience?
The funny thing is that if you look at my submissions I reviewed this problem very recently before the contest. I literally chuckled when I saw the problem in the contest.
please send some of your luck over to me via internet. :P
Can someone explain why this test case is -1?
n=12 k=4,
1 3 2 4 1 1 3 4 2 1 3 4
3 7 1 1 1 1 1 1 1 1 1 1 is also valid for this ,yes why is it -1 then ?
its not -1? assuming you're talking about b
Sorry, I am talking about D
oh mb
yes for b Sir
If you meant n=7,k=3
Then this is not valid since, there are not unique elements in the end k and start k elements.
its not -1, thats just not the optimal answer
here is correct construction
4+1, 1 (makes 4) then a bunch of 1s
thats 5+1+(12-2) = 16
Can someone explain how we "normalize" the permutation in D? I thought to use binary search to find the segment where A ends similar to the editorial in contest, but I got stuck on the fact that the last k elements are not necessarily $$$B_1, B_2, ..., B_k$$$
we are only interested in comparing elements whose indexes are modulo $$$k$$$. Therefore, for the last $$$k$$$ elements (they necessarily belong to the right array), we can take their values and indexes modulo $$$k$$$ and substitute them into a normalized permutation. That is, if $$$b'$$$ is the normalized permutation underlying the array $$$B$$$, then $$$\forall i \in \overline{n - k, n - 1} : b'[i~\%~k] = C[i]$$$. Indexing from $$$0$$$
I like sample in D
E is very good, thx for the contest
for D, also the diagrams were very helpful
Can you make the problem titles clickable such that they go to the problems, much like other editorials?
yeah, updated
I feel so bad when I read "it is easy to see" for Problem A because my dumb ass didnt see T__T
Loved the images in Problem D! I hope the other Authors take inspiration from you
317996659 Can you please tell me why this algorithm failed?
Your code fails for the following input:
Essentially, what's happening is that due to the
greater<>in your sorter, if two indices are equal, then the one on the right appears before the one on the left, which is easy to break (it sees the 1 at the 3rd position and says "Well no robot is to the left or right, so we need to place a robot here."). There is a "small" fix to your code that makes it AC though.Instead of trying to process it per element, why not when inserting, process which buttons that same robot can also press?
Shouldn’t it be, “each swap changes the answer by an even number”?
Ignore above comment. Meant to comment on the Editorial.
Thank you JaSonicPlusPlus . ACCEPTED 318064174. I got where it fails. I have to store just consecutive duplicates for one time . Again thank you & have a nice day.
D-F were amazing!
I became a pupil!
Why I think B is more difficult than C?
congrats!!
bro B was more difficult..
Just my opinion:
UPD: After an hour's thinking, I think $$$A\lt C$$$ now.
True for B
I really like the contest (in particular D and E, but finished E 5 min after the end ://).
My ($$$\mathcal O(n)$$$) solution for E (We don't need a centroid decomposition):
Removing one edge will always reduce the total score, but we want to minimize the reduction. If we remove an edge to a leaf, the "lost contribution" of that edge is $$$1$$$. The change of contribution of the other edges $$$w_1w_2$$$ is $$$-1$$$ if:
This means that we can do a simple DFS and pass the following values:
The score change can then be calculated as:
Note: This can be reduced to one score since $$$s_2 = t - 1 - s_3$$$, where $$$t$$$ is the total number of subtrees of size $$$ \gt \frac{n - 1}{2}$$$ while $$$s_3$$$ is the number of vertices with a subtree of size $$$ \gt \frac{n - 1}{2}$$$ on the path to the root, so we decrease $$$s_1$$$ if the subtree size is $$$\le \frac{n - 1}{2}$$$ and increase it if $$$ \gt \frac{n - 1}{2}$$$.
Since we are only running DFSs, the total runtime is $$$\mathcal O(n)$$$.
Submission with $$$s_1, s_2$$$ (Proof by AC)
Edit: Submission/Fixes
Thank you for the good contest.Yeah I got orange.
whoa.. so cool
My idea of E may be wrong.
First I thought of what if we don't need to delete an edge.I will find the centroid of the tree,let the centroid be root.Then just dfs to confirm there are no same color in each subtree.
And in this problem,I found the centroid,let it be root and dfs,then find the leaf with minimum height,delete the edge linking the leaf and its parent.Then just dfs to confirm there are no same color in each subtree.
I passed the sample and some tests,but was wrong on pretest 8,Could anybody explain where is wrong
318013026
I've tried first three problems.Lovely Problemset.truly!
Problem B was a good challenge for me. Thanks for this problem
2108A — Permutation Warm-Up
How to prove that max value of f(p) = floor((n^2) / 2).
From the editorial I can only understand that it is possible, but how to prove that it is max ?
For getting maximum answer we need to keep the values so that we git maximum manhattan distance between them. and for that you can see that its to put it in reverse order. thus the summation would result in maximum which is the answer.
if you want you can try all other ways for a small n .
I posted a proof lower =)
after started recently only I was given advice to attempt contests all contests, EDU, DIV2. i don't think i am even ready for that, 2 contest — was able to solve only 1 A that also from EDU, nothing from here . so, can anyone provide another advice?
Don't be disheartened—I felt this Div2A was much more difficult than the recent ones I've solved; I myself could not prove that my solution was optimal during contest time. You can try the recent Div2A's first to see if you could constantly solve them and ready for future Div2A's. Div. 3 & 4's are few and far between, so if you decide to wait for them you'll likely have to do problems entirely in practice mode.
Can some one tell me where am i wrong in D, here is my Solution
Editorial for A is so bad...
"For this p, f(p)=⌊n22⌋." Proof ?
"since we were adding +2 at each step" -> Wrong. Some steps do not change the value.
"Let’s prove that we can’t get any other values [...] it’s easy to see that" -> Is this a joke ?! Repeating your premise and claiming it's easy is no proof. This is actually the main difficulty of the problem.
"Second, we can only obtain even values, because each swap changes the answer by an odd number." Is the "odd" a typo for "even" ? Also, Proof ?
What's the point of pretending to prove anything, if what you'll say explicitely will be more trivial than what you dont bother to prove ? Just say one can guess this perm is optimal and prove by AC.
The value for the permutation given is sum(n-1-2k) with k from 0 to ceil(n-1/2)==floor(n/2)
Either you know the sum 0..N of odd numbers is N^2 or you can split the sum and use sum 0..N is N*(N+1)/2. Sure it's basic but not especially trivial for a div2A.
An optimal perm cannot have a number smaller than half in the first half and bigger than half in the second otherwise swapping them would give a higher answer. Let's consider the first half if there was a number lower than an other placed before this higher number we could increase the answer by swapping them. Same thing applies to the second half. We have proven the decreasing perm is optimal for n even. For n odd swapping the middle number doesnt change the answer if the above properties are respected (the displaced middle compensates exactly for the score lost)
I cant see anything elegant to prove that the answer is always even. We can take a permutation do a swap on it, and study each case if i and j are both greater or smaller than both pi and pj the answer is the same, if pi<=i<j<=pj or i<=pi<pj<=j we contribute to the opposite of what we used to the difference is twice the contribution. Lastly for i<=pi<=j<=pj the difference in contribution is 2pi-2j by just writing it out we can do similarly for pi<=i<=pj<=j. Since all permutations can be reached through a series of swaps for the 1..N perm, we have a proof.
Anyone has something simpler ?
We can use the fact that $$$|x| \equiv x \pmod{2}$$$ (it is either $$$x$$$ or $$$-x$$$, both of which satisfy the congruence). This gives that $$$|p_1 - 1| + |p_2 - 2| + \dots + |p_n - n| \equiv (p_1 + p_2 + \dots + p_n) - (1 + 2 + \dots + n) \equiv 0 \pmod{2}$$$.
Elegant !
This might be what Hashman was saying, but we know that if we got rid of the absolute values, then the total sum would always be 0 (we'd just be subtracting the sum of a non-permuted array from the sum of a permuted array). So, if we categorize the value of each difference (in the non-absolute value case) as either "positive," "negative," or "zero," we know that the positive and negative equal each other in magnitude. Thus, when we throw the abs-vals back in to get the problem statement, we know that the total sum must be even.
can any one explain me the "no mans land" condition given in the editorial for Problem D ?
It means that the elements between the boundaries do not definitively belong to the left nor the right array (no array claims it), because it matches the patterns for both arrays. An example is the last test case in the problem statement:
The pattern for A is 1 3 2 4, and for B is 1 3 4 2. Neither array can claim the elements between the boundaries (
1 3), because they match the pattern for the other array as well, and thus any split is valid.thx for this awesome contest fr like it yea i didnt Register but e is so haaaaaaaaaard-_____-
A and D are interesting.
Could any tell me why D need binary twice
after we binary first we ' ll get an answer range which its len less 50
then we just find it straight isn t it?
i think that will be 3k + log(n / k), could anyone proof it ? or why its not right?
yeap this is one of the possible solutions
the editorial shows the optimal solution for 125 queries, but after testing we came to the conclusion that the best way would be to increase the available number of queries
it works binary isn't necessary
For interactive problems like Problem D, how to design and test cases?
Can anyone provide a DSU implementation for C?
https://mirror.codeforces.com/contest/2108/submission/318001381
DSU idea, but implementation can be done with simple visited array.
EDIT: DSU implementation https://mirror.codeforces.com/contest/2108/submission/318141147
eugenechka.boyko.2_0-0 why the below code is running.I have no proof for this and i did brute-force for 1<=N<=10 and noticed this 1 2 3 5 7 10 13 17 21 26
respect for authors for amazing tutorial and notes. I hope such notes and tutorial will be in every round.
In Problem C,I used dp and binary search to solve it.
submission #317966737
Nice editorial.
Before this contest, I didn't even know that popcount existed, so I made a literal function to count the number of 1 bits in x
What is the proof of optimality in taking a reverse permutation in problem A? It could be a local maximma. Why can there not a be a configuration from a different algorithm of construction that achieves higher? Can someone provide a proper proof of this?
(I tried asking ChatGPT, it failed to prove it.)
problem F is insane
why was B substantially harder than C
Can anyone help me to findout mistake in my code in Problem D of this round . It is failing on some test case where answer should be -1 but my code is printing some number. Submission Id:323253380
solved ! I was checking the last condition wrong .
324755461 Can anyone tell why this code is failing?
Try this case:
Does there exist a Graph approach for
C, if yes then please comment down your approach with your accepted code.I am just curious because one of the tag of this question is showing graph.const int N = 2e5 + 10; int n, a[N]; PII b[N]; bool st[N]; **** int main(){ ** IOS; ** ** int _ = 1;** ** cin >> ;** ** while(--)** ** {** ** cin >> n;** ** for(int i = 1; i <= n; ++ i)** ** cin >> a[i], b[i] = {a[i], i}, st[i] = false;** ** sort(b + 1, b + 1 + n);** ** int cnt = 0;** ** for(int i = n; i >= 1; -- i)** ** {** ** int x = b[i].second;** ** if(!st[x])** ** st[x] = true, cnt++;** ** st[x — 1] = true, st[x + 1] = true;** ** }** ** cout << cnt << endl;** ** }** **** ** return 0;** } why C this way was wrong?
2108C - Neo's Escape I got scared after seeing the topics written in the problem tags of this question, but this is the easiest question of 1500 rating i have ever solved 368354382