Thanks for participating in the contest. I hope you enjoyed the problems :)
| Predictor | A | B | C | D | E | F | G | H |
|---|---|---|---|---|---|---|---|---|
| Argentum47 | 800 | 800 | 900 | 1200 | 1500 | 1700 | 1900 | 2200 |
| simplelife | 800 | 800 | 1000 | 1300 | 1500 | 1800 | 1800 | — |
| lina_os | 800 | 800 | 1000 | 1100 | 1400 | — | — | — |
| -firefly- | 800 | 800 | 900 | 1400 | 1600 | 1900 | 2000 | 2300 |
| Syrianooo | 800 | 800 | 900 | 1200 | 1300 | — | — | — |
| Mousa | 800 | 800 | 1000 | 1300 | 1600 | 2000 | 2300 | 2100 |
| yse | 800 | 800 | 1000 | 1200 | 1400 | 1600 | 1900 | 2100 |
A — Koshary
Without short steps, we can only visit coordinates where $$$x$$$ and $$$y$$$ are both even.
Using only long steps, we can only visit coordinates where $$$x \equiv 0 \pmod 2$$$ and $$$y \equiv 0 \pmod 2$$$. The extra short step allows us to change either $$$x$$$ or $$$y$$$ to become odd (i.e. $$$x \equiv 1 \pmod 2$$$ or $$$y \equiv 1 \pmod 2$$$), but not both.
Thus, we can say that all coordinates are reachable except for ones where both $$$x$$$ and $$$y$$$ are odd, since that would require more than one short step.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int x, y;
cin >> x >> y;
cout << (x % 2 == 0 || y % 2 == 0 ? "YES" : "NO") << endl;
}
}
B — Party Monster
It can never be harmful to choose the entire string as the substring.
As mentioned in the hint, choosing the entire string to be the substring, that we perform the operation on, is always optimal. Let's assume a smaller substring yields a valid RBS, we can just choose the entire string, and only move around the characters which are in the smaller substring. Thus, choosing the entire string is always optimal.
Now, the problem reduces to checking if we can rearrange a bracket sequence to become an RBS. We want each '$$$\texttt{(}$$$' to pair with a '$$$\texttt{)}$$$', leaving no unpaired brackets. This is equivalent to checking whether the number of occurrences of '$$$\texttt{(}$$$' is equal to the number of occurrences of '$$$\texttt{)}$$$' in the string.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for(int i = 0; i < n; i++) cnt += s[i] == '(';
cout << (2 * cnt == n ? "YES" : "NO") << endl;
}
}
C — Snowfall
A subarray should have both $$$2$$$ and $$$3$$$ as prime factors of the product to be divisible by $$$6$$$. Each element can either contribute $$$2$$$, or $$$3$$$, or both.
From the previous hint, we can split the elements into $$$4$$$ groups.
A subarray has product divisible by $$$6$$$ if it contains both $$$2$$$ and $$$3$$$ as prime factors. Therefore, every element falls into one of $$$4$$$ groups:
- $$$S_6$$$: Elements divisible by $$$6$$$
- $$$S_2$$$: Elements divisible by $$$2$$$ but not $$$3$$$
- $$$S_3$$$: Elements divisible by $$$3$$$ but not $$$2$$$
- $$$S_1$$$: Elements neither divisible by $$$2$$$ nor $$$3$$$
Elements in $$$S_6$$$ already make any subarray divisible by $$$6$$$, so we want them packed together at an end to minimize the number of subarrays they are included in. For the next 2 paragraphs, let's forget about $$$S_6$$$.
For the remaining elements, if we place all $$$S_2$$$ before all $$$S_3$$$, then the only divisible subarrays are the ones that start in $$$S_2$$$ and end in $$$S_3$$$. Any other order would only create more such subarrays.
What do we do with elements of $$$S_1$$$? Inserting them anywhere can never decrease the number of divisible subarrays, but we can avoid increasing by placing them between $$$S_2$$$ and $$$S_3$$$. Subarrays divisible by $$$6$$$ would still be ones that start in $$$S_2$$$ and end in $$$S_3$$$, so the number of divisible subarrays is still as minimal as possible.
Therefore, an optimal construction is:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<int> a, b, c, d;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
if(x % 6 == 0) a.push_back(x);
else if(x % 2 == 0) b.push_back(x);
else if(x % 3 == 0) c.push_back(x);
else d.push_back(x);
}
vector<int> ans;
for(auto it : a) ans.push_back(it);
for(auto it : b) ans.push_back(it);
for(auto it : d) ans.push_back(it);
for(auto it : c) ans.push_back(it);
for(int i = 0; i < n; i++) cout << ans[i] << " \n"[i == n - 1];
}
}
D — Palindromex
The minimum answer is $$$1$$$, because a $$$0$$$ always exists, and we can take a $$$0$$$ individually as a palindromic subarray with $$$\operatorname{mex} = 1$$$.
Any valid subarray with a $$$\operatorname{mex} \gt 1$$$ will still contain at least one $$$0$$$. So we know the optimal subarray will always contain a $$$0$$$.
There are several valid approaches to this problem. Here, I will explain the intended approach.
We can claim that the optimal subarray will always contain the element $$$0$$$. This is because, if it did not contain $$$0$$$, then $$$\operatorname{mex} = 0$$$. But $$$\operatorname{mex} = 1$$$ can be achieved by considering a $$$0$$$ element as its own subarray (it is palindromic). So, the optimal subarray must always contain the element $$$0$$$.
This gives us only $$$3$$$ candidate centers to expand the palindrome from. Let $$$x$$$ be the position of the first $$$0$$$ in the array, and $$$y$$$ be the position of the second $$$0$$$:
- $$$x$$$ can be the center of the optimal palindrome.
- $$$y$$$ can be the center of the optimal palindrome.
- $$$x$$$ and $$$y$$$ can be on opposite corresponding sides of the palindrome. For instance, $$$[\dots, 0, \dots, j, k, j, \dots, 0, \dots]$$$ for odd palindromes, $$$[\dots, 0, \dots, j, k, k, j, \dots, 0, \dots]$$$ for even palindromes.
We can try each of these choices, and expand from the candidate center as long as the subarray is still palindromic, while keeping track of $$$\operatorname{mex}$$$. The answer is the maximum $$$\operatorname{mex}$$$ among all $$$3$$$ candidates.
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v;
int solve(int l, int r) {
set<int> s;
for(int i = 0; i <= n; i++) s.insert(i);
while(l >= 0 && r < 2 * n && v[l] == v[r]) {
s.erase(v[l]);
l--, r++;
}
return *s.begin();
}
int main() {
int t;
cin >> t;
while(t--) {
cin >> n;
v = vector<int> (2 * n);
for(auto &it : v) cin >> it;
int x = -1, y;
for(int i = 0; i < 2 * n; i++) {
if(!v[i]) {
if(~x) y = i;
else x = i;
}
}
cout << max({solve(x, x), solve(y, y), solve((x + y) / 2, (x + y + 1) / 2)}) << endl;
}
}
Instead of only expanding from the $$$3$$$ possible candidates, we can brute force over all centers (even and odd palindrome centers) and only expand as long as the subarray will stay palindromic. Can you prove why this fits the time limit?
E — It All Went Sideways
Look at one height at a time. A cube stays only if every column to its right is tall enough for that height.
The number of cubes that stay in column $$$i$$$ is exactly the suffix minimum $$$s_i$$$.
Let $$$s_i = \min(a_i, a_{i+1}, \dots, a_n)$$$ be the suffix minimum of the array.
The key observation is that a cube at position $$$(i, h)$$$ stays in the same column after the gravity shift if every column to its right also has a cube on height $$$h$$$. In other words, the row $$$h$$$ is already a suffix of occupied cells. This happens exactly when $$$h = s_i$$$.
So, column $$$i$$$ contributes exactly $$$s_i$$$ cubes that do not move, and therefore it contributes $$$a_i - s_i$$$ cubes that do move. Without any operation, the total number of moving cubes is $$$\sum_{i=1}^{n} (a_i - s_i)$$$. Now we try the operation. If $$$a_i \gt s_i$$$, then this decrease does not change any suffix minimum at all, because there is already a smaller somewhere to the right. Such a choice is useless. The only useful positions are those where $$$a_i = s_i$$$, which are exactly the positions where the suffix minimum array changes (i.e., $$$s_i \neq s_{i+1}$$$).
Since suffix minima are non-decreasing from left to right, equal values form contiguous blocks. If we decrease the rightmost element of such a block, then the suffix minimum on the whole block drops by $$$1$$$. This means the number of cubes that stay in place decreases by the length of that block, while the total number of cubes decreases by only $$$1$$$. Consequently, the number of moving cubes increases by $$$\text{block length} - 1$$$.
Therefore, the best strategy is to pick the longest block of equal values in the suffix minimum array. The final formula for the maximum number of moving cubes is:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<ll> v(n);
ll sum = 0;
for(auto &it : v) {
cin >> it;
sum += it;
}
vector<ll> suf_mn(n);
suf_mn[n - 1] = v[n - 1];
sum -= suf_mn[n - 1];
for(int i = n - 2; i >= 0; i--) {
suf_mn[i] = min(suf_mn[i + 1], v[i]);
sum -= suf_mn[i];
}
ll mx = -1, cur = 1;
for(int i = 1; i < n; i++) {
if(suf_mn[i] == suf_mn[i - 1]) cur++;
else {
mx = max(mx, cur);
cur = 1;
}
}
mx = max(mx, cur);
cout << sum + mx - 1 << endl;
}
}
F — It Just Keeps Going Sideways
Thanks to chromate00 for suggesting a modification to problem E, which resulted in this problem.
Cubes on the same height never interact with other heights.
For a fixed height $$$h$$$, the cubes end up in the rightmost $$$\texttt{cnt}[h]$$$ columns, where $$$\texttt{cnt}[h]$$$ is the number of cubes at height $$$h$$$.
After decreasing one column by $$$1$$$, only one height level changes, so the gain value is easy to calculate locally.
Fix some height $$$h$$$. A cube at height $$$h$$$ exists in column $$$i$$$ exactly when $$$a_i \ge h$$$. When gravity shifts to the right, all cubes on the same height behave independently from the other heights, they just slide as far right as possible without overlapping. Therefore, on height $$$h$$$, all cubes occupy the rightmost $$$\texttt{cnt}[h]$$$ columns, where $$$\texttt{cnt}[h]$$$ is the number of columns with $$$a_i \ge h$$$. So the whole problem can be reduced to counting, for each height, how many cubes start there and where they end up.
Let
Then the sum of final column indices of the cubes on height $$$h$$$ is simply the sum of an arithmetic progression:
The sum of initial column indices of the cubes on height $$$h$$$ is the sum of all indices $$$i$$$ such that $$$a_i \ge h$$$. If we sum this over all heights, we get the total movement distance of all cubes. An easy way to compute the initial sum is column-wise instead of height-wise. A cube in column $$$i$$$ contributes $$$i$$$ to the initial column-sum for every height from $$$1$$$ to $$$a_i$$$. So the total initial sum is $$$\sum_{i=1}^n i \cdot a_i$$$. For the final sum, we use the $$$\texttt{cnt}[h]$$$ values above. So if no operation is performed, the answer is $$$\text{final} - \text{initial}$$$.
Now consider the allowed operation. Suppose $$$a_i = x$$$, then the initial sum decreases by exactly $$$i$$$, because one cube from column $$$i$$$ disappears. What happens to the final sum? Only height $$$x$$$ changes, because the column still contributes to all heights $$$1, 2, \dots, x-1$$$, but no longer contributes to height $$$x$$$. So $$$\texttt{cnt}[x]$$$ decreases by $$$1$$$, and all other heights stay unchanged.
For height $$$x$$$, the final sum of occupied columns changes from the rightmost $$$\texttt{cnt}[x]$$$ columns to the rightmost $$$\texttt{cnt}[x]-1$$$ columns. The difference is exactly $$$n - \texttt{cnt}[x] + 1$$$. So the total gain from decreasing $$$a_i$$$ is:
That means we should compute this value for every index $$$i$$$ and take the maximum.
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<int> v(n + 1), cnt(n + 1);
for(int i = 1; i <= n; i++) {
cin >> v[i];
cnt[v[i]]++;
}
vector<int> have(n + 1);
have[n] = cnt[n];
for(int i = n - 1; i >= 1; i--) have[i] = have[i + 1] + cnt[i];
int init = 0, aft = 0;
for(int i = 1; i <= n; i++) {
init += i * v[i];
aft += have[i] * (2 * n - have[i] + 1) / 2;
}
int cur = aft - init, mx = 0;
for(int i = 1; i <= n; i++) mx = max(mx, i - n + have[v[i]] - 1);
cout << cur + mx << endl;
}
}
G — Drowning
The length of the array decreases by $$$2$$$ after each operation, so only odd-length subarrays are possibly good.
Think about alternating sum. What can you notice about it after applying some operations?
Alternating sum is invariant. A good subarray must be a positive single element at the end, so the alternating sum must be positive. Is this a sufficient condition?
Let's define:
which is the alternating sum of an array $$$c$$$.
First, every operation keeps the parity of the length unchanged, because the length only decreases by $$$2$$$. Therefore, if an array can be reduced to length $$$1$$$, then its length must be odd.
Now, let's look at the operation. Suppose we choose an index $$$i$$$ and perform the replacement:
In the alternating sum $$$S(c)$$$, the signs of these three positions are either $$$(+, -, +)$$$ or $$$(-, +, -)$$$, depending on the parity of $$$i-1$$$. Their contribution to the total sum is:
This is exactly the contribution of the new element $$$x$$$ at that position. Therefore, the alternating sum is invariant under the operation.
Since the final array has length $$$1$$$, its alternating sum is just that single remaining number, which is positive. Therefore, every good array must have positive alternating sum. So far we have proved a necessary condition: a good array must have odd length and positive alternating sum.
It turns out, the condition is actually sufficient.
To prove sufficiency, we show that if an odd-length array has no valid moves, its alternating sum cannot be positive. Let the array be $$$c_1, c_2, \dots, c_{2k+1}$$$.
Now suppose no move is possible. Then for every middle even position $$$2j$$$,
Since $$$-c_{2j}$$$ appears in the alternating sum, we can upper-bound it by
Writing out the alternating sum for length $$$2k+1$$$:
Substituting the inequalities $$$-c_{2j} \le -(c_{2j-1} + c_{2j+1})$$$:
Thus, if $$$S(c) \gt 0$$$, at least one move must exist.
Now the problem is reduced to counting subarrays with odd-length and positive alternating sum. Let:
For a subarray $$$[l, r]$$$, if $$$r − l + 1$$$ is odd, then $$$l$$$ and $$$r$$$ have the same parity, and the subarray is good exactly when the corresponding prefix values satisfy:
- If $$$l$$$ is odd, the subarray sum is $$$\texttt{pref}[r] - \texttt{pref}[l-1]$$$. We need $$$\texttt{pref}[r] \gt \texttt{pref}[l-1]$$$.
- If $$$l$$$ is even, the subarray sum is $$$\texttt{pref}[l-1] - \texttt{pref}[r]$$$. We need $$$\texttt{pref}[r] \lt \texttt{pref}[l-1]$$$.
So while scanning $$$r$$$ from left to right, we only need to count previous prefix indices of the opposite type with a smaller or larger prefix value. This is a standard task, which can be done using Fenwick Tree and coordinate compression, or using an ordered set with order statistics.
// written by omsincoconut
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pair<ll, int>, null_type,less<pair<ll, int>>, rb_tree_tag,tree_order_statistics_node_update>
const bool HAVE_CASE = true;
void test_case_run() {
ll n;
cin >> n;
ll a[n+1];
for (int i = 1; i <= n; i++) cin >> a[i];
ll qc[n+1];
qc[0] = 0;
ordered_set oset[2];
oset[0].insert({0LL, 0});
ll ans = 0;
for (int i = 1; i <= n; i++) {
qc[i] = a[i] - qc[i-1];
ans += oset[(i+1)%2].order_of_key({qc[i], -1});
oset[i%2].insert({-qc[i], i});
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
if (HAVE_CASE) cin >> t;
while (t--) test_case_run();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct FenwickTree {
int n;
vector<int> bit;
FenwickTree(int n) : n(n + 1), bit(n + 1) {}
int query(int r) {
int ret = 0;
while(r > 0) {
ret += bit[r];
r -= (r & -r);
}
return ret;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
void update(int idx, int delta) {
while(idx < n) {
bit[idx] += delta;
idx += (idx & -idx);
}
}
};
signed main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<int> v(n);
for(auto &it : v) cin >> it;
vector<int> pre(n + 1);
for(int i = 1; i <= n; i++) {
if(i & 1) pre[i] = pre[i - 1] + v[i - 1];
else pre[i] = pre[i - 1] - v[i - 1];
}
vector<int> h = pre;
sort(h.begin(), h.end());
h.erase(unique(h.begin(), h.end()), h.end());
auto get = [&](int x) { return lower_bound(h.begin(), h.end(), x) - h.begin() + 1; };
FenwickTree fen_ev(h.size()), fen_od(h.size());
int ans = 0;
fen_ev.update(get(0), 1);
for(int i = 1; i <= n; i++) {
int x = get(pre[i]);
if(i & 1) {
ans += fen_ev.query(x - 1);
fen_od.update(x, 1);
}
else {
ans += fen_od.query(x + 1, h.size());
fen_ev.update(x, 1);
}
}
cout << ans << endl;
}
}
H — Fallen Leaves
For a fixed edge, when does it contribute $$$1$$$ to the answer?
Root the tree anywhere. Let $$$\texttt{cnt}[u]$$$ be the number of leaves in the subtree of $$$u$$$, and $$$u \neq \texttt{root}$$$. The edge $$$\texttt{parent}_u \to u$$$ is used if $$$\texttt{cnt}[u]$$$ is odd.
There can be an unchosen leaf. How to choose which leaf to ignore?
Let's pick any vertex as the root. For every edge connecting a parent to a child $$$u$$$, let $$$\texttt{cnt}[u]$$$ be the total number of leaves in the subtree rooted at $$$u$$$.
Consider this edge alone:
- If $$$\texttt{cnt}[u]$$$ is even, all leaves inside this subtree can be paired with each other internally. This edge does not need to be crossed by any path.
- If $$$\texttt{cnt}[u]$$$ is odd, after pairing as many leaves as possible within the subtree, exactly one leaf will be left over. This leaf must cross the edge to find a pair outside the subtree. Therefore, this edge must be used exactly once.
If the total leaf count is even, every leaf must be paired. An edge is used if and only if its child's subtree contains an odd number of leaves. Hence:
But what if the total leaf count was odd? Exactly one leaf will remain unchosen. Leaving a leaf $$$x$$$ unchosen allows us to save or lose the cost of some edges. Specifically, for every edge on the path from the root to $$$x$$$, the parity of the number of leaves in the corresponding child subtree changes:
- If the subtree had an odd number of leaves, it becomes even, so that edge is no longer needed;
- If the subtree had an even number of leaves, it becomes odd, so that edge becomes needed.
Therefore, if the total number of leaves is odd, we want to choose a leaf $$$x$$$ such that the value
is maximized.
Let's define $$$\texttt{mx}[u]$$$ as:
Thus, the answer for the odd leaves case is:
We can compute $$$\texttt{cnt}[u]$$$ for every vertex using a DFS. Then, in the same DFS or in a separate one, we compute $$$\texttt{mx}[u]$$$. The transition is:
and for a leaf, $$$\texttt{mx}[u] = 0$$$.
Therefore,
- If $$$\texttt{cnt}[\texttt{root}]$$$ is even, the answer is just the number of edges whose child subtree has odd $$$\texttt{cnt}[u]$$$;
- If $$$\texttt{cnt}[\texttt{root}]$$$ is odd, subtract $$$\texttt{mx}[\texttt{root}]$$$ from that answer.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int n;
vector<int> adj[N];
int mx[N], cnt[N];
void dfs(int u, int par) {
cnt[u] = (adj[u].size() == 1);
for(int v : adj[u]) {
if(v == par) continue;
dfs(v, u);
cnt[u] += cnt[v];
}
int cur = 0;
for(int v : adj[u]) {
if(v == par) continue;
int w = (cnt[v] & 1) ? 1 : -1;
cur = max(cur, mx[v] + w);
}
mx[u] = cur;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) {
cin >> n;
for(int i = 1; i <= n; i++) adj[i].clear();
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
int ans = 0;
for(int u = 2; u <= n; u++) {
if(cnt[u] & 1) ans++;
}
if(!(cnt[1] & 1)) cout << ans << endl;
else cout << ans - mx[1] << endl;
}
return 0;
}









how 11 days ago?
same question
They kept this editorial private before then contest ended.
blog was originally posted 11 days ago, however it was not public, now they opened it
delete cmt
i don't really think that my delta is enough for my peak
after seeing your heatmap it definitely isn't!
I created video editorial for G. Drowning.
Can u please create video solutions for E,F,H
I just thought this was ironic
The example note in C made me question reality for a moment...
yeah, same..
loll, what was it?
see the changes in statement. basically only 6 possible subsequences were given for an example while 12 were possible
Problem E is similar to the problem Gravity Falls. You may check it out!
Problem Link of Gravity Falls: https://mirror.codeforces.com/problemset/problem/2148/F
This problem E seems very interesting to me. After all the contest was of great fun really! Thank you Codeforces.
Really grateful for getting this problem. I thought it would be below 1500 rating, but after solving it on my own, I found out it was actually rated 1800.
This is my first 1800 rated problem solved, completely by myself without looking at the editorial and honestly, the satisfaction feels amazing.
Yeah, exactly!! When I was trying to solve problem E on contest, I was thinking "wait, I solved similar type of problem which came in a recent Div4 round". And, you know, because of solving 2148F - Gravity Falls, I got the intuition very fast.
Fast editorial
Mann ..Stack saved me at the last minute in E
your submission for E looks like AI-generated code bro.
it is -- i mean not exactly but only 2 minutes was left and i had the whole code in mind but my typing speed was slow i was able to code like half but i knew i wouldn't be able to complete .. i know its not right but..i fell to my temptation
that is not good bro!
i myself feel awful man ... i feel like reporting myself to the community
don't do it after this contest , so good luck
Thank God Codeforces, what would we do without you.
لا إله إلا الله سبحانه وتعالى عز وجل
please share the link of ur solution
Anyone managed to solve H with some greedy simulation ?
I dont know can it be called greedy. I just did simple dp, like we are not deleting leaves, and rerooted it.
I mostly meant simulating the process moving the leaves up by one and when they meet adding the pair, the problem is that sometimes its not optimal to remove a pair since one of the leaves might be the one we should eventually leave off.
oh yeah! This is literally what I did. Addiotoinally if we leave the longest leaf without pair in our dp, it will work. We could delete not the optimal one, to avoid it, we just check for every root (reroot) (sorry for late answering)
Thanks for the contest , I just participated out of speed-running first few problems.
btw C is a nice problem.
I looked other problems , They look interesting too.
Congrats on this successful contest .
wtf we have nearly identical solution even the naming convention for C this is my submission holy shit i hope i don't get banned lol.
373106639
my sol almost exactly same bruh
Mine is exactly same???
i hope we don't get reported then bruv!!
ah hell nah
My submission
dont be worry, as you can see its not a particularly difficult problem, so its just normal for ideas or coding styles to overlap (at least there many people do in that way).
Thanks for the insanely fast editorial, and for the good problems you have put up in the contest!
Agree!!
I'm genuinely impressed with problem G, how can one come up with that idea?
glad to know that there's solution without using Fenwick Tree/Segment Tree in problem F.
who used complex Manacher algorithm for D or just dumb me :D
Almost did, then halfway through that I realised "wait, 0 must be in the subarray always"
Atleast you used, I was sat their reading and understanding the algo( I had heard about Manacher's Algo and what it was used for but the details eluded me), and did zilch....
i just learned that existed after the contest :(
I kept getting wa even after applying it :(
At first glance, i also thought that Manacher's would work here. but since it was a Div 3 C, it made me reconsider.
I ended up using the editorial based idea of expanding around 3 centers :)
Me too, I forgot each number appears exactly twice
Answer to the $$${Bonus}$$$ $$${Problem}$$$ $$${D:}$$$
My approach uses greedily finding the longest palindromic subsegments, but the concept of this approach works for the idea discussed in the Editorial as well.
Since there are $$${exactly}$$$ $$${2}$$$ occurrences of each element in the array, therefore the number of pairs which are equal and of the form $$$a_{i}=a_{j}$$$ is only $$${n}$$$.
Also, since the $$${positions}$$$ of each element is $$${fixed}$$$, therefore, for each $$${center}$$$, if you find palindrome with that center, there are $$${unique}$$$ set of palindromic pairs.
If we iterate on the array, and at the current index we try to find the palindromic pair of this element. If the palindromic pair is at an index $$${j \geq i+1}$$$, then we check for each pair $$${(i, j)}$$$ $$${,}$$$ $$${(i+1, j-1)}$$$ $$${,}$$$ $$${(i+2, j-2)}$$$ $$${,}$$$ $$${....}$$$ $$${,}$$$ $$${(i+k, j-m)}$$$, where $$${i+k \leq j-m}$$$. While checking we will also mark them as visited in a visited array.
If at any point we get to know that this selected subsegment is not palindromic we simply just increment $$${i}$$$ to the index where the palindromic condition first failed, i.e., $$${i=x}$$$, where $$${a[x] \neq a[y]}$$$ for some indices $$${x}$$$ and $$${y}$$$ during the checking of palindromic condition. After incrementing, check for other possible palindromic subsegments by repeating the process.
But, if we find that the selected subsegment was a palindrome, then we iterate on the visited array from $$${0}$$$ to $$${n}$$$, and store the maximum $$${mex}$$$ found yet. Then we increment the $$${i}$$$ to $$${j+1}$$$, where $$${j}$$$ was the index of the palindromic pair of the element at index $$${i}$$$.
We increment $$${i}$$$ to $$${j+1}$$$ because we have found the longest palindromic subsegments greedily, and since there are $$${exactly}$$$ $$${2}$$$ occurrences for each element in the array, therefore $$${any}$$$ subsegment of the palindromic subsegment $$${(i, j)}$$$ cannot be present in any other palindromic subsegment.
Hence, we only are iterating on the array just $$${once}$$$ and we visit each and every element just $$${once}$$$. Therefore, only $$${O(n)}$$$ operations are performed in greedily finding the longest palindromic subsegment.
Now, talking about the finding of $$${mex}$$$. Since $$${0}$$$ can be present in $$${at}$$$ $$${most}$$$ $$${2}$$$ palindromic subsegments, therefore, for all other palindromic subsegments the $$${mex}$$$ can be found in just $$${one}$$$ operation $$${( mex}$$$ would be zero in that case $$${)}$$$.
$$${At}$$$ $$${most}$$$ $$${2}$$$ subsegments will take $$${O(n)}$$$ time to find their $$${mex}$$$, therefore in total it takes $$${O(2*n-2+2*n)}$$$ $$${=}$$$ $$${O(n)}$$$.
Therefore, the total number of operations turn out to be $$${O(n) + O(n)}$$$ $$${=}$$$ $$${O(n)}$$$.
My submission uses the same idea.
can fentree be used in E like in F
Problem F was a modification/extension of problem E. This is the first time I have seen this sort of "multi part" problem in a codeforces round despite it being quite common in national/international Olympiads. Personally, I find these sorts of problems quite enjoyable. Kudos to the problem authors.
for the bonus of problem D
I exactly solved the problem like that but for the proof my intuition told me this is correct <3
..
It was a big mistake to tell people to check your solution; they hacked you And tbh, I wanted to hack you but wasn't fast enough
for the bonus of problem D
This fits the time limit because i got Accepted for same idea
Instant tutorial, the contest was great, and the author is Egyptian!!!
I can't ask for a better thing, thank you, yse
Why is it not necessary to have at least one of $$$a_l$$$ or $$$a_r$$$ to be positive in a good subarray? (Problem G)
instead of making suffix min array in problem E, we can simply track the max gain by maintaining two state variables...
Please add
<spoiler summary="My Code">before your code and<spoiler> with ("/") before the word spoilerafter your code for better readability, and the comment looks betternoted...and added the spoiler tag. thanks for the tip!
You're welcome budddyyyyy
hmm... what if we want to maximize the number of subarrays that are divisible by 6 in problem C? And what if the number by which the product of the array numbers should be divided is arbitrary? It sounds interesting. Thanks for the contest.
interesting
To maximize the number of subarrays containing at least one occurrence of $$$x$$$, you can formulate the problem as follows:
Let the number of occurrences of $$$x$$$ be $$$k$$$. These $$$k$$$ elements divide the array into $$$k+1$$$ segments of lengths $$$x_1, x_2, \dots, x_{k+1}$$$. The number of subarrays that include at least one $$$x$$$ is:
This is a known optimization problem where we use the identity:
Since the total sum of elements $\sum_{i=1}^{k+1} x_i = n-k$ is constant, maximizing the sum of pairwise products $$$\sum_{1 \le i \lt j \le k+1} x_i x_j$$$ is equivalent to minimizing the sum of squares $$$\sum_{i=1}^{k+1} x_i^2$$$ which is minimized when the elements are distributed as evenly as possible ($$$x1 \approx x2 \approx x3 \approx \dots \approx x_{k+1}$$$).
As for the divisors of $$$x$$$, i believe grouping them together in contiguous blocks to act as new singular elements is optimal though i couldn't prove it.
I realised the solution for E in the last 10 minutes but ran out of time :( Solved it 15 minutes later though :P
This was a fun first contest
contest was fun . feels like a div 3 contest. C was interesting for me. couldnt solve d and e but i saw e problem earlier contest. overall the contest was fun. i hope everyone was satisfied with contest
Let's call the largest palindromic subarray with center at index $$$i$$$ a candidate.
Notice that, since every integer in $$$a$$$ appears exactly twice, $$$a_i$$$ is considered at most two times: during center bruteforce, and when a subarray is checked if it can be expanded further (if $$$a_i=a_j$$$, then the center of the subarray is located at index $$$\frac{i+j}{2}$$$). This means the sum of lengths of candidates is at most $$$2*2n=4n$$$, which means the average length of all candidates in $$$a$$$ is $$$O(1)$$$.
We can do MEX on candidates only, since every other subarray is guaranteed to be unoptimal. The total complexity becomes $$$O(nlogn)$$$, which is good enough.
373106063
In F I used difference array to track the sum of all position which contains height x, and after it I check how many blocks finally will be in right side, we can easily find their positional sum. and the answer without removing any block will be sum of all height's answer which is their final positional sum — initial sum.
for the problem G isn't it just counting inversion with few more conditions, so can't it just be done using merge sort yse ?
I have Done D using Brute
Suffix minimum idea in E is actually neat !
I can't code, can't find a job, can't have GF, what I can do god, Please save me.
Started CP at the age of 24 being a unemployed, who is aiming to become CMaster at CF.
Fingers crossed :X)
My apparch for F (Segment Trees)
Used a lazy segment tree to track, for each element, how much distance it can move based on how many columns to the right support its height.
While iterating right → left, updated this tree to maintain:
for each height h → count of future columns with ≥ h
and used it to compute total movement (ans).
Used a second segment tree to maintain frequency of heights (prefix counts).
This second tree is used to simulate removing one element, by counting:
how many elements (heights ≥ a[i]) gain +1 movement
(inversion-like counting).
Final answer:
base movement + best gain from removing one index
code in java:- https://mirror.codeforces.com/contest/2227/submission/373194545
D can be solved using binary search. We can find if ans is atleast x+1.in O(n)
If ans is atleast x+1 , means all ele 0 to x are present , thus get both the positions of x. Now 2 possiblities , Both x are present in our palindrome or Only one.
For both x case , we have both position of x , hence find its centre and expand palindrome as much as possible and check if all elements from 0 to x are present. For single x , do same , now centre is positon of x itself. (Check for Both positions of x)
Link -(https://mirror.codeforces.com/contest/2227/submission/373144878)
H is great, the edge-perspective idea is interesting.
I think my solution is the same as Bonus Problem D: 373133653, instead of brute-forcing all over the center, i did brute-force from the boundary.
Since each element appears exactly twice. Each value can only be in at most 3 different situations (they can be the matching pairs, or they can be the center). If they are the matching pairs, they can only be visited exactly one times. If they are the center, they can be visited by other element at most 2 times. This gives us O(N) solution.
Can anyone explain the solution to the Bonus question of D ?
By the way, G is solvable without having to use PBDS / segtree structures, which is helpful for easier implementation
If we twist the perspective a little bit from the provided solution, then pref[l-1] < pref[r] when l is odd, and pref[r] < pref[l-1] when l is even. Notice how in both cases, the smaller side (l-1 and r respectively) are even, and the larger sides are odd. And in each case, r appears later than l-1 and vice versa. Since l-1 and r are different parity, they cannot be equal and hence each case goes either in the 'smaller' or 'larger' category, which these two criteria fit exactly. This means that we can just search for the number of EVERY pref[#odd] (regardless if the index is larger or smaller than that of the even index) that are larger than each pref[#even]. Since this requires no element addition or subtraction, this can be done with sorting + binary search.
Implementation
G's easier version on LC 2426. Number of Pairs Satisfying Inequality
E and F are amazing!
this was my first contest, letsgo
solved a and b, and almost solved c, didn't know how to sort numbers that aren't divisible directly by 6, gotta work on my math
what am i doing i tried to maximise the subarrays in C
same
the persons who use ai , their family all died already btw, an excellent contest
a similar problem after c?
an elegant solution for F :) 373205287
in problem D, why couldn't we check if there is a palindrom bitween the tow zeros ,then if there is we check the number on the tow sides of it to make it longer, otherwise we assume that every zero is the center of the palindrom and the answer will be the maximum mex of each of the palindrims? I hope you understand my quastion '_' ^_^ 2227D - Palindromex
i got braingasm after solving A and B
Wow, this was my first contest. Solved problems A, B and C smoothly, struggled in D.
But a great experience overall!!!
can it be said for D given the constraints that two palindromic subarrays cannot intersect unless one is completely inside the other, and then we can do a brute force on all palindromic subarrays which cant be extended by adding one element on each side? the only exception i can think of is a subarray of the form abab which can still be handled within the code
My post contest discussion stream.
https://mirror.codeforces.com/blog/entry/153409
Youtube VOD
yse Why has my rating not updated for this round? Why does it show a delta of 0?
The problems are interesting.
guys i made it :DD
I think there's a typo in the "Hint 2" of problem E, it should be "The number of rows/cubes that stay in column
iis exactly the suffix minimums_i."Fixed, thank you.
good contest
I used a Fenwick Tree for all of problems E, F and G.
SegmentTreeforces
in G what sort of intuition would lead one to guess that alternatingsum>0 is a sufficient condition?
I did problem F completly differently
If you look at $$$a[i]$$$ and some block at height $$$y \leq a[i]$$$, the number of times that that block contributes to the total motion is the number if $$$j \gt i$$$ such that $$$a[j] \lt y$$$. If we loop through the array backwards and mantain a histogram $$$h[x]$$$ of the number of times $$$a[j] = x$$$ for $$$j \gt i$$$, then the number of times a block at height $$$y$$$ contributes to the motion is
$ Adding this up for all $$$y \leq a[i]$$$ ands, the contribution is
$ Finally we can mantain two fenwick trees representing sums of $$$h[k]$$$ and $$$kh[k]$$$ to get the answer. You can get the maximum increase from decrementing one $$$a[i]$$$ with the same method
KAN MikeMirzayanov
Cant we do anything about blatant cheaters?
We can easily spot cheaters in this contest.
For example, cookingDSA is clearly using AI solutions in this contest.
Are there going to be any updates in the near future regarding anti-cheat tools, etc.?
If not, we can float a blog where people can share their ideas so that cheaters will be reduced.
First time upsolved all the problems of a Div-3 round!! Problem H was most interesting and only painful problem for me.
How to solve H if the set S contains all vertices of the tree?
the only way for a tree to be only leaves is if $$$n=1$$$ or $$$n=2$$$ but the problem gives $$$3 \leq n$$$ so you don't have to think about that
i meant the case where S is defined to include all vertices of the tree, not just the leaves, this leads to a different problem
For problem H, there is also a sneaky dp solution which somehow passes without TLE
I basically try to root the tree at each non leaf node and then try to calculate the best possible cost for that root.
373383493
This solution TLEs if you try to root the tree also on leaf nodes
Hacked, I used a tree that looks like this:
Vertex $$$1$$$ would be forced to sort its neighbors $$$O(n)$$$ times, making the complexity $$$O(n^2 \log n)$$$.
Thanks!
H is so confused
the solution for H is so confused you said If cnt[u], is even, all leaves inside this subtree can be paired with each other internally. This edge does not need to be crossed by any path. so what about the sons of vertex u get used already, then u become the new leaf ? and
cnt[u] = (adj[u].size() == 1);is so confused , what ru cal for ??? obviously not cal for the num of leavesyou said: let cnt[u] be the total number of leaves in the subtree rooted at u but yoru cnt[1] equal to 3 on sample 1, obviously the num of leaves is 2 who can explain this for me plz!!!
root in 1 . if 1 sons of vertex u get used , another sons of this vertex u have been paired with this . every node (expect root) have 1 father , so if adj = 1 , we expect father node cause adj = 0 , this is leaf
thank you! now i have solved the problem, i misunderstood the definition of the leaf node, , ans the cnt of nodes is only, no matter with the root, thanks a lot btw, i dont read this sentence of the problem : this set is determined from the original tree and does not change, my bad XD
I love problem G
C