Thank you for participating, and we truly hope you enjoyed the contest!
2092A - Kamilka and the Sheep
Author: k1sara
It's always possible to make Kamilka's pleasure at least $$$|x - y|$$$ for any $$$x, y \in a$$$.
First of all, it can be observed that for any two sheep with beauty levels $$$x \lt y$$$, the maximum possible pleasure cannot exceed $$$y - x$$$, since $$$\gcd(x, y) = \gcd(x, y - x)$$$.
Secondly, we can choose $$$x = \min(a)$$$, $$$y = \max(a)$$$, and $$$d = -x \bmod (y - x)$$$, achieving a pleasure of $$$\max(a) - \min(a)$$$.
Thus, the answer is $$$\max(a) - \min(a)$$$.
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
cout << a[n - 1] - a[0] << endl;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int tt; cin >> tt;
while (tt--) {
solve();
}
}
t = int(input())
for test in range(t):
n = int(input())
a = [int(i) for i in input().split()]
print(max(a) - min(a))
2092B - Lady Bug
Author: sergeev.PRO
Note that you can split strings $$$a$$$ and $$$b$$$ into two "zig-zags": $$$a_0, b_1, a_2, b_3, \dots$$$ and $$$b_0, a_1, b_2, a_3, \dots$$$
What are the necessary and sufficient conditions for each of these zig-zags?
Note that you can split strings $$$a$$$ and $$$b$$$ into two "zig-zags": $$$a_0, b_1, a_2, b_3, \dots$$$ and $$$b_0, a_1, b_2, a_3, \dots$$$.
For each of these zig-zags, you can achieve any sequence of 0s and 1s, provided that their quantities within the zig-zag are preserved. Therefore, the answer is Yes if and only if the number of 0 is at least $$$\lceil \frac{n}{2} \rceil$$$ in the first zig-zag and at least $$$\lfloor \frac{n}{2} \rfloor$$$ in the second one.
#include <iostream>
using namespace std;
void solve() {
int n; cin >> n;
string a, b; cin >> a >> b;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; ++i) {
if (i & 1) {
cnt2 += a[i] == '0';
cnt1 += b[i] == '0';
} else {
cnt1 += a[i] == '0';
cnt2 += b[i] == '0';
}
}
cout << (cnt1 >= (n + 1) / 2 && cnt2 >= n / 2 ? "Yes" : "No") << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t; cin >> t;
while (t--) solve();
}
t = int(input())
for test in range(t):
n = int(input())
a = input()
b = input()
cnt1, cnt2 = 0, 0
for i in range(n):
if i % 2:
cnt2 += (a[i] == '0')
cnt1 += (b[i] == '0')
else:
cnt1 += (a[i] == '0')
cnt2 += (b[i] == '0')
if cnt1 >= (n + 1) // 2 and cnt2 >= n // 2:
print("Yes")
else:
print("No")
2092C - Asuna and the Mosquitoes
Author: sergeev.PRO
Consider two cases: first, when all numbers have the same parity; and second, when there is at least one even and one odd number.
Let $$$S$$$ be the sum of all numbers, and $$$k$$$ be the number of odd numbers in the array. Then, in the second case, the answer is $$$S - k + 1$$$.
Case 1. All numbers in $$$a$$$ have the same parity. In this case, it is impossible to perform any operation, so the answer is $$$\max(a)$$$.
Case 2. There is at least one even and one odd number. Let $$$S$$$ be the sum of all numbers, and $$$k$$$ be the number of odd numbers in the array. We will now prove that the answer is $$$S - k + 1$$$.
First of all, the answer cannot exceed $$$S - k + 1$$$, since the number of odd elements in the array remains unchanged after performing the operation.
Secondly, we will show that $$$S - k + 1$$$ is always achievable. Consider an arbitrary odd number $$$A \in a$$$. We can then merge all even numbers into $$$A$$$ $$$\left( \text{i.e.} \ (A, 2k) \to (A + 2k, 0) \right)$$$, resulting in an array consisting of the number $$$A + 2m$$$, $$$k-1$$$ odd numbers, and zeros for the remaining elements (here $$$2m$$$ denotes the sum of all even numbers in the initial array).
After this step, for each remaining odd number, we can sacrifice one unit by transferring it to any zero element in the array and then merge the resulting even number into $$$A$$$. It's easy to see that there will always be at least one zero available in the array before merging each odd number. Eventually, we will obtain an array consisting of one element equal to $$$S - k + 1$$$, $$$k - 1$$$ elements equal to one, and zeros filling the remaining positions.
Therefore, the answer in this case will be $$$S-k+1$$$.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int n; cin >> n;
vector <int> a(n);
long long ans = 0, cnt = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
ans += a[i]; cnt += (a[i] & 1);
}
if (!cnt || cnt == n) {
cout << *max_element(a.begin(), a.end()) << '\n';
} else {
cout << ans - cnt + 1 << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t; cin >> t;
while (t--) solve();
}
t = int(input())
for test in range(t):
n = int(input())
a = [int(i) for i in input().split()]
ans, cnt = 0, 0
for i in a:
ans += i
cnt += i % 2
if not cnt or cnt == n:
print(max(a))
else:
print(ans - cnt + 1)
2092D - Mishkin Energizer
Author: k1sara
It is impossible to balance the string if and only if all its letters are the same.
It is clear that there is no solution if all the letters in $$$s$$$ are the same. Let us now prove that a solution always exists in all other cases.
While the string remains unbalanced, let us assume that $$$\text{cnt}(a) \le \text{cnt}(b) \le \text{cnt}(c)$$$, where $$$a, b, c \in \lbrace \tt{L}, \tt{I}, \tt{T} \rbrace$$$, and $$$\text{cnt}(l)$$$ denotes the number of occurrences of the letter $$$l$$$ in $$$s$$$. Consider two cases:
The string $$$s$$$ contains the substring
bcorcb. In this case, we can perform the operation on it to obtainbacorcab.Otherwise, the string $$$s$$$ must contain the substring
caorac. Without loss of generality, assume that $$$s$$$ containsca. Then we can perform the following sequence of operations:ca$$$\to$$$cba$$$\to$$$cbca$$$\to$$$cbaca$$$\to$$$cabaca.
It can be observed that after each operation, the value of $$$2 \cdot \text{cnt}(c) - \text{cnt}(b) - \text{cnt}(a)$$$ decreases by one. Here, $$$a$$$, $$$b$$$, and $$$c$$$ are always chosen such that $$$\text{cnt}(a) \le \text{cnt}(b) \le \text{cnt}(c)$$$. Therefore, the algorithm is guaranteed to terminate, and it does so when $$$2 \cdot \text{cnt}(c) - \text{cnt}(b) - \text{cnt}(a) = 0$$$, which implies $$$\text{cnt}(a) = \text{cnt}(b) = \text{cnt}(c)$$$.
It remains to prove that the number of operations performed by the proposed algorithm does not exceed $$$2n$$$.
Assume that initially $$$x = \text{cnt}(a), y = \text{cnt}(b), z = \text{cnt}(c)$$$, where $$$\text{cnt}(a) \le \text{cnt}(b) \le \text{cnt}(c)$$$. Then $$$x + y + z = n$$$.
While $$$\text{cnt}(a) \lt \text{cnt}(b)$$$, we increment $$$\text{cnt}(a)$$$ by one in each step, using no more than $$$4$$$ operations per step. Therefore, this phase requires at most $$$4(y - x)$$$ operations.
Afterwards, the algorithm alternately increments $$$\text{cnt}(a)$$$ and $$$\text{cnt}(b)$$$ by one until both become equal to $$$\text{cnt}(c)$$$. I claim that this phase will use the $$$4$$$-operations sequence (i.e., the second case in the algorithm) at most once. Indeed, let us consider the first time we perform the transformation ca $$$\to$$$ cabaca. It is easy to see that all subsequent steps will involve only single-operation transformations. Thus, this phase requires no more than $$$2(z - y) + 3$$$ operations.
Summing up, the total number of operations is bounded above by $$$4(y - x) + 2(z - y) + 3 = 2z + 2y - 4x + 3$$$. If $$$x \neq 0$$$, this number is less than $$$2n$$$ given that $$$n = x + y + z$$$. Otherwise, the first step of the algorithm will be either bc $$$\to$$$ bac or cb $$$\to$$$ cab, requiring only one operation. This improves the estimate for the number of operations in the first phase from $$$4(y-x)$$$ to $$$4(y - x) - 3$$$. As a result, in the case where $$$x = 0$$$, the total number of operations does not exceed $$$4(y - x) - 3 + 2(z-y) + 3 = 2z + 2y = 2n$$$.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
string base = "LIT";
int t; cin >> t;
while (t--) {
int n; string s;
cin >> n >> s;
if (count(s.begin(), s.end(), s[0]) == n) {
cout << -1 << '\n';
} else {
vector <int> ans;
while (true) {
vector <pair<int, char>> cnt;
for (auto i : base) cnt.push_back(make_pair(count(s.begin(), s.end(), i), i));
sort(cnt.begin(), cnt.end());
if (cnt[0].first == cnt[1].first && cnt[1].first == cnt[2].first) break;
auto op = [&] (int i) -> void {
string z = base; z.erase(find(z.begin(), z.end(), s[i])); z.erase(find(z.begin(), z.end(), s[i + 1]));
ans.push_back(i);
s = s.substr(0, i + 1) + z[0] + s.substr(i + 1);
};
bool done = false;
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] == s[i + 1]) continue;
if (s[i] != cnt[0].second && s[i + 1] != cnt[0].second) {
op(i);
done = true;
break;
}
}
if (done) continue;
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] == s[i + 1]) continue;
if (s[i] == cnt[2].second) {
op(i); op(i + 1); op(i); op(i + 2); break;
} else if (s[i + 1] == cnt[2].second) {
op(i); op(i); op(i + 1); op(i + 3); break;
}
}
}
cout << ans.size() << '\n';
for (auto i : ans) cout << i + 1 << '\n';
}
}
}
We assume that a solution exists (refer to the beginning of the first solution). Now, at each step, let's greedily find the first position where the rarest letter can be inserted and place it there. If no such position exists during a given iteration, we insert the most frequent letter instead. The algorithm terminates when all letters occur the same number of times. We claim that this greedy strategy performs no more than $$$2n$$$ operations.
Assume that initially $$$x = \text{cnt}(a), y = \text{cnt}(b), z = \text{cnt}(c)$$$, where $$$\text{cnt}(a) \le \text{cnt}(b) \le \text{cnt}(c)$$$. Then $$$x + y + z = n$$$. Here, $$$\text{cnt}(l)$$$ denotes the number of occurrences of the letter $$$l$$$ in $$$s$$$.
Claim 1. If the rarest letter cannot be inserted, there always exists a position where the most frequent letter can be inserted.
Suppose not. Then every pair of distinct adjacent letters must contain both a and c. This would imply that the number of bs is zero, which contradicts the condition $$$y \ge x$$$.
Claim 2. If $$$x = y$$$, the solution can always be obtained in exactly $$$(z - x) + (z - y) = 2z - x - y$$$ operations.
Let's find the first position $$$i$$$ such that $$$s_i \ne s_{i+1}$$$ and either $$$s_i$$$ or $$$s_{i+1}$$$ is c. Without loss of generality, assume the substring is of the form cb.... Then, the algorithm performs the following sequence of operations:
cb... $$$\to$$$ cab... $$$\to$$$ cbab... $$$\to$$$ cabab $$$\to$$$ $$$\dots$$$ $$$\to$$$ cbababab... Since every two iterations increase the counts of both a and b by one, we will reach a count of $$$z$$$ in exactly the required number of steps.
Now consider the case when $$$x \lt y$$$. Observe that in two operations, the algorithm increases the count of the rarest letter by one and increases the count of the most frequent letter by at most one. This is true because, after inserting the most frequent letter, we obtain a substring like bc. It is easy to see that a can be inserted into it, and the algorithm, being greedy, will do exactly that.
After some number of iterations, $$$x$$$ becomes equal to $$$y$$$ (note that $$$y$$$ remains unchanged throughout the algorithm). Then, by the reasoning above, the final value of $$$z' = \text{cnt}(c)$$$ satisfies:
Applying Claim 2, we get that the total number of operations does not exceed
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
using namespace std;
vector<char> a = {'L', 'I', 'T'};
int main() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
string s; cin >> s;
set<char> se;
for (auto u : s) se.insert(u);
if (se.size() == 1) {
cout << -1 << endl;
continue;
}
vector<int> ans;
map<char, int> mp;
for (auto u : s) {
mp[u]++;
}
auto func = [&](int i) {
for (auto u : a) {
if (s[i] != u && s[i + 1] != u) return u;
}
return '$';
};
while (max({mp['L'], mp['I'], mp['T']}) != min({mp['L'], mp['I'], mp['T']})) {
int mn = min({mp['L'], mp['I'], mp['T']});
int mx = max({mp['L'], mp['I'], mp['T']});
int ok = 0;
for (int i = 0; i < s.size() - 1; i++) {
char x = func(i);
if (s[i] != s[i + 1] && mp[x] == mn) {
mp[x]++;
ok = 1;
s.insert(s.begin() + i + 1, x);
ans.push_back(i);
break;
}
}
if (!ok) {
for (int i = 0; i < s.size() - 1; i++) {
char x = func(i);
if (s[i] != s[i + 1] && mp[x] == mx) {
mp[x]++;
s.insert(s.begin() + i + 1, x);
ans.push_back(i);
ok = 1;
break;
}
}
}
}
cout << ans.size() << endl;
for (auto u : ans) {
cout << u + 1 << endl;
}
}
}
2092E - She knows...
Author: k1sara
If a cell has an even number of neighbours, does its color matter?
Let $$$S$$$ be the set of cells that have an odd number of neighbouring cells. It is easy to observe that $$$S$$$ consists of border cells, excluding the corner ones. More precisely,
Idea 1. I claim that the number of adjacent border cell pairs with different colors is even. Indeed, if we walk along the border—which essentially forms a cycle—starting and ending at $$$(1, 1)$$$, we must change color an even number of times.
Idea 2. It can be observed that the parity of $$$|A|$$$ does not change when flipping the color of any cell not in $$$S$$$, since such a cell has an even number of neighbouring cells. Therefore, if we fix the coloring of the border cells, the parity of $$$|A|$$$ remains unchanged if we imagine recoloring all non-border cells white. This implies that the parity of $$$|A|$$$ depends solely on the parity of the number of black (or white) cells in $$$S$$$. Moreover, it can be seen that these parities are actually equal.
Idea 3. Now, we are left with counting the number of colorings such that $$$|S|$$$ contains an even number of black cells. Let's consider two cases:
All cells in $$$S$$$ are initially colored. In this case, the answer is $$$2^{n \cdot m - k}$$$ if the number of black cells in $$$S$$$ is even, and $$$0$$$ otherwise, since the colors of the remaining cells do not affect the parity.
At least one cell in $$$S$$$ is uncolored. In this case, the answer is $$$2^{n \cdot m - k - 1}$$$. This follows from the basic identity of the binomial coefficients:
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll mod = 1000000007;
ll binpow (ll a, ll n) {
ll res = 1;
while (n) {
if (n & 1) {
res *= a; res %= mod;
}
a *= a; a %= mod; n >>= 1;
}
return res;
}
void solve() {
ll n, m, k; cin >> n >> m >> k;
int good = 0, sum = 0;
for (int i = 0; i < k; ++i) {
int x, y, c; cin >> x >> y >> c;
if ((x == 1 && y == 1) || (x == 1 && y == m) || (x == n && y == 1) || (x == n && y == m)) continue;
if (x == 1 || y == 1 || x == n || y == m) {
++good; sum += c;
}
}
if (good == 2 * (n + m - 4)) {
cout << (sum % 2 ? 0 : binpow(2, n * m - k)) << '\n';
} else {
cout << binpow(2, n * m - k - 1) << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t; cin >> t;
while (t--) solve();
}
2092F - Andryusha and CCB
Author: k1sara
It is sufficient to consider only two types of blocks of consecutive 0s and 1s: those of size $$$1$$$ and those of size greater than $$$1$$$.
For a fixed beauty value $$$m$$$ of a substring, there are $$$O\left(\frac{sz}{m}\right)$$$ possible values of $$$k$$$, where $$$sz$$$ is the number of blocks of consecutive 0s and 1s.
For a fixed pair $$$(m, k)$$$, let $$$S$$$ be the set of indices $$$i$$$ such that it is possible to divide the prefix $$$[1, i]$$$ of the string into $$$k$$$ substrings of beauty $$$m$$$. It is claimed that $$$S$$$ forms a continuous segment $$$[l, r]$$$ for some $$$1 \le l \le r \le sz$$$.
First, note that it is sufficient to consider only two types of blocks of consecutive 0s and 1s: those of size $$$1$$$ and those of size greater than $$$1$$$. Based on this, we construct a new string of length $$$sz$$$, consisting of characters 1 and 2, representing the types of blocks. For example, for the original string 00101110, the new string is 21121.
Let’s fix a value of $$$m$$$—the desired beauty of the substrings we are dividing the string into. It is easy to see that there are $$$O\left(\frac{sz}{m}\right)$$$ possible values of $$$k$$$ (the number of substrings).
Now, for a fixed pair $$$(m, k)$$$, let $$$S_k$$$ be the set of indices $$$i$$$ such that it is possible to divide the prefix $$$[1, i]$$$ of the new string (representing block types) into $$$k$$$ substrings of beauty $$$m$$$. It is claimed that $$$S_k$$$ forms a continuous segment $$$[l, r]$$$ for some $$$1 \le l \le r \le sz$$$. Let’s prove this by induction.
Base case ($$$k = 1$$$). We set $$$l = m$$$, $$$r = m$$$ (0-based indexing).
Inductive step ($$$k \rightarrow k + 1$$$). Suppose that for every index $$$i \in [l, r]$$$, the first $$$i$$$ blocks can be divided into $$$k$$$ substrings of beauty $$$m$$$. Then we can construct a valid division into $$$k+1$$$ substrings by:
Simply appending a new substring of length $$$m+1$$$ starting at block $$$i+1$$$, giving us a new upper bound at $$$i + m + 1$$$.
Additionally, if block $$$i$$$ is of type $$$2$$$, we can split it between the $$$k$$$-th and $$$(k+1)$$$-th substrings, allowing us to finish the $$$k$$$-th substring earlier and start the $$$(k+1)$$$-th within the same block. This enables us to reach $$$i + m$$$ as well.
Thus, we conclude:
$$$S_{k+1} = [l + m, r + m + 1]$$$ if the $$$l$$$-th block is of type 2.
Otherwise, $$$S_{k+1} = [l + m + 1, r + m + 1]$$$.
To compute the full solution, we iterate over all values of $$$m$$$ from $$$1$$$ to $$$sz - 1$$$. For each $$$m$$$, we iterate over $$$k$$$ until the left bound $$$l$$$ of the corresponding segment exceeds $$$sz$$$. The special case $$$m=0$$$ should be handled separately.
For each $$$k$$$, we need to increment the answer for all positions from $$$l$$$ to $$$r$$$ by one. This can be efficiently done by recording the operations $$$(l, +1)$$$ and $$$(r + 1, -1)$$$ and then applying a scanline technique to process the accumulated changes.
Since there are $$$O\left(\frac{sz}{m}\right)$$$ possible values of $$$k$$$ for each $$$m$$$, the total complexity of this solution is $$$O(n \log n)$$$.
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
void solve() {
int n; cin >> n;
string s; cin >> s;
vector <int> a;
int curr = 0;
for (int i = 0; i < n; ++i) {
if (i && s[i] != s[i - 1]) {
if (curr) a.push_back(curr);
curr = 1;
}
else ++curr;
}
a.push_back(curr);
int sz = a.size();
vector <int> ans(n, 0), left(sz, 0), right(sz, 0);
for (int i = 0; i < sz; ++i) {
if (!i) {
left[i] = 0; right[i] = a[i] - 1;
} else {
left[i] = right[i - 1] + 1;
right[i] = left[i] + a[i] - 1;
}
}
for (int i = 0; i < sz; ++i) {
for (int j = left[i]; j <= right[i]; ++j) {
ans[j] += j - i + 1;
}
}
vector <int> add(sz, 0);
for (int m = 1; m < sz; ++m) {
ll l = m, r = m, k = 1;
while (l < sz) {
++add[l];
if (r + 1 < sz) --add[r + 1];
if (a[l] == 1) {
l += m + 1;
} else {
l += m;
}
r += m + 1;
++k;
}
}
int pref = 0;
for (int i = 0; i < sz; ++i) {
pref += add[i];
for (int j = left[i]; j <= right[i]; ++j) {
ans[j] += pref;
}
}
for (int i = 0; i < n; ++i) {
cout << ans[i];
if (i != n - 1) cout << ' ';
}
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t; cin >> t;
while (t--) {
solve();
}
}








why first question was harder than the second
was it ?
i guess it depends on what kind of stuff u hv seen before
true
It`s ez if u get the point, in fact the problem has nearly nothing to do with GCD
just a simple number theory ig
this is peak use of free will
i agree i agree
I didn’t like the round. I simply guess forced B, C and D. Particularly I think you are required to guess D. Also, this contest (up till E, as I didn’t solve F) was full adhoc: no DP, no binary search, no algorithmic analysis, no graphs (ok, this one is basically a myth in div. 2 at this point), no trees, no particular DS, simply full adhoc the entire contest :(
well you can prove no of operations you will use like mine was 2n-3, but then i also ran a guess solution which adds L I T as long as each of their count is less than n, and it passed too. im not really big fan of guess problems.
This seems to be a bit common on cf.
I thought i < j in C.
I thought i < j in C.
I thought i < j in C.
I thought i < j in C.
i spent 2 hours getting WAs after solving AB in 10 mins
someone kill me NOW
Why was constraint for n kept to be<=100?
Maybe the checker need to insert $$$2n$$$ times and it is $$$O(n^2)$$$.
Make brute force pass. $$$O(tn^2)$$$ is acceptable although there is an $$$O(tn)$$$ solution.
I believe step 1 of solution 1 for question D should say
bacinstead ofbca.Another easier way to E:
If a connected component is completely surrounded by another, the contribution must be even. So if we add a black circle to the outermost, the answer must be even too.
Consider what the black circle do: Contribute 2 for the white corner cells and 1 for the white border cells. So when the black circle removed, the number of white border cells also must be even.
We got a diddy collab before gta 6
how to come up with C? The solution gives a formula and tries to prove it. How do i come up with such formulas? just guess something and try to prove it??
First, we notice that odd and even numbers need to be combined. Then, we observe that the combination of an odd and an even number always results in an odd number. You can imagine the odd number 'devouring' the even number and turning into a larger odd number.
From a greedy perspective, we want the largest possible even numbers to be 'devoured' to maximize the resulting number. Thus, we can devour all the even numbers. However, we haven’t fully utilized the odd numbers. We can extract an even component from one of the odd numbers and add it to the original even numbers, making the even portion larger.
To optimize this, we leave just one odd number and merge all the remaining parts into the even numbers. Then, we let the odd number devour all the even numbers, achieving the desired result.
i use deepseek to translate my thoughts into English.
Revised explanation: We should reduce all odd numbers to 1 (each remaining as 1), instead of combining them into just one odd number.
Lets say u have all the numbers odd or even then the answer is the max this u can easily see as odd + odd = even & even + even = even. Now consider the maximum number to be odd lets say the array is 21 11 9 2 then when we consider 21 as the maximum value then when we add 2 to it becomes 22 and 2 becomes one thus no even number left but then even + odd = odd so 11 becomes 10 so it alternates so all the even numbers eventually become 0 and odd numbers become equal to 1. Then if the maximum number is even eventually you will see the number you have to minus is (count of odd-1) if you just use and example.... Sorry Didn't provide with a mathematical proof.
It's useful to think about invariants on the data, and about lower and upper bounds on the answer.
In this case, there are two invariants that are helpful:
Operations do not change the total sum of the array. (This one is pretty obvious.)
Operations do not change the total number of towers with even/odd height. (This is slightly less obvious, but since each operation involves 1 tower of odd height and 1 tower of even height, and increasing/decreasing a tower's height flips its parity, that means the two towers involved in an operation always swap their parity, keeping the total number of even and odd towers unchanged. This is true regardless of whether you move from the odd to the even tower, or vice versa.)
This allows you to prove an upper bound on the answer of (sum of heights) — (number of odd towers — 1), because given the above invariants, the very best case is where your "highest" tower has an odd height, all the other odd towers have height 1, and all the even towers have height 0.
Then you just need to prove that this is always achievable, which is fairly easy in this case.
I have solved quite a few problems, Until now i tried to find ans for this type of problems by deduction, I never thought of finding upper and lower bounds. From now on, I will try to do that. Thank you again
Yes!! Just guess bro, this is the current codeforces meta.
Felt D to be more easier today. But still can't solve it, due to miscalculation
If you think no one is cheated in this contest:
read this Blog
I can't understand the second question. Could someone explain it to me? (this is my first contest)
"You can observe that you can swap any two elements if they are in the positions of a₀, b₁, a₂... or b₀, a₁, b₂... Since swaps can be performed any number of times, all elements can be freely rearranged. For the first case where arbitrary swaps are possible, we need to make all elements at a₀, a₂... positions zero. This means the number of zeros in the a₀, b₁, a₂... sequence should be greater than or equal to the total number of elements at a₀, a₂... positions, which is the ceiling of n/2."
Why the number of zeros needs to be greater or equal n/2? And why in the c++ code, the conditional of cnt1, in the output, is (n+1)/2
The reason for using (n + 1) / 2 is to perform ceiling division, i.e., computing the smallest integer p such that p * k ≥ n (using the formula (n + k — 1) / k). This ensures that the number of 0s in Position 1 (i.e., cnt1) is at least the minimum required number of 0s.
About problem E: it might just be a me problem, but when writing a problem about a grid and cell coordinates on that grid, I think there needs to be an explanation on how the rows and columns are numbered (from left to right, top to bottom, …) to avoid confusion.
Get blocked by Cloudflare for nearly twenty minutes while solving C :(
Thanks for the contest! I was able to solve 3 problems. Nothing could have made me happier than waking up as a Pupil! Thanks again! ◝(ᵔᵕᵔ)◜
In D solution 1, why does 2 cnt(c) — cnt(b) — cnt(a) = 0 imply cnt(a) = cnt(b) = cnt(c)?
Because $$$cnt(a) \le cnt(b) \le cnt(c)$$$.
Is D solvable by just inserting the lowest frequency element(that we can insert) every time ?
No, because sometimes you can't insert it. For example in this case:
cccabbb— hereathe rarest element, but there is no way to insert itUPD. Oh, I misunderstood the question. Yes, it can be solved, but it is more difficult to prove the correctness.
Why is it hard to prove correctness. If freq(a) < freq(b) < freq(c), you need to insert 'a' freq(c) — freq(a) times, so it's not worse to insert a. You never lose any options when you do this, and every operation is independent from each other. If you don't have any options left, you are forced to insert c. It feels fairly straightforward to me. Am I missing something?
Because if you do this, the frequency of the
belement will not be constant, so it is difficult to use claim 2 to estimate the number of operations.But you don't have to prove that the number of operations is < 2n. You can just simulate and if at any point the number of operations is too much you can stop. It's still equally correct.
No. It has been proven that an algorithm with $$$\le 2n$$$ operations always exists if the string does not consist of identical characters. Therefore, if a solution is not optimal — let's say it requires $$$3n$$$ operations — and stops at $$$2n$$$ operations when a valid solution actually exists, it is incorrect.
What sergeev.PRO was trying to say is that the aforementioned solution does work; however, a formal proof that it performs no more than $$$2n$$$ operations is required to demonstrate its correctness.
I don't understand why you need a formal proof that it never performs more than 2n operation. Isn't enough to show that it's optimal, because then it would never incorrectly stop when a valid solution does exist.
My reasoning for why its optimal is:
1) It doesn't matter where you insert a letter in the string, all insertions of the same letter are equal.
2) The order in which you perform operations doesn't matter, in the sense that performing one operation doesn't stop you from performing any of the other operations that you could before.
3) Any optimal solution must insert at least freq(c) — freq(a) 'a's, and freq(c)-freq(b) 'b's, so its never worse to insert them first (all it does is open up more options).
Obviously this isn't a formal proof, but I feel like it's intuitive that its optimal (unless I'm missing something obvious which is definitely possible)
Yeah, I guess with this reasoning, the algorithm is indeed optimal, although there might still be some corner cases. Sorry for the inconvenience.
for problem F, despite writting a very efficient and low constant factor code, it still TLEs, was the TL set so tight to cut down any solutions ?
313191690
without push backs
313191925
The intended solution has a time complexity of $$$O(n \log n)$$$ and a memory complexity of $$$O(n)$$$, so ideally, we wanted to exclude solutions with higher complexities. My solution ran in under $$$300$$$ ms, so setting the time limit $$$6 \times$$$ of that seemed sufficient.
I tried visualizing all the problems in a single image — this is what I came up with. LOL!
https://youtu.be/2wZVP_cacGk?si=oRlJ_4Ct8hrCP-5b AI. generated just asked to solve the problem
https://youtu.be/2wZVP_cacGk?si=oRlJ_4Ct8hrCP-5b
Very good A problem that makes you think, thanks for the round
WHY (N+1)/2 IN LADYBUG PROBLEM IS TAKEN ?
Because $$$\lfloor \frac{n+1}{2} \rfloor = \lceil \frac{n}{2} \rceil$$$.
Who is this girl on your PFP? And what anime is she from? She looks cute.
I think its Kisara from engage kiss.
Problem E:
can someone explaine me, why the even neighbours cells will not have any effect on the answer ?
Think about putting some black cells in an infinitely large field filled with white cells. Take a connected segment of black cells. Then the number of pairs of adjacent cells with different colors is equal to the length of perimeter of the segment (if the segment has an inner hole, its perimeter also counts). And the length of the perimeter of the segment is always even (it's easy to prove this).
With this fact in mind, you can see the number of pairs of adjacent cells with different colors is always even, provided that the field is infinitely large. If the dimension of the field is finite, only the colors of the edge cells contribute to the parity of the number of pairs of adjacent cells with different colors -- because other cells' contribution cancels out. More precisely, the contribution of corner cells can be ignored, because they change the parity twice.
I got it.
Thank You
Another way to see it is:
Letting black cells be 1 and white cells be 0, we want $$$\sum\limits_{(u,v)\in E}^{} (u+v) = 0$$$ (mod 2). It's easy to see that this is equivalent to $$$\sum\limits_{u\in V}^{} deg(u)*u $$$ (mod 2). So cells with an even count of neighbors don't contribute to the answer.
I think reading the solution of F is like doing some blank filling tests.
Thanks for the fun round!
Just wanted to make a note that in problem F, the reason why we can update the answer the way the editorial says is because for a fixed pair $$$(k, i)$$$, there is at most one value of $$$m$$$ for which we can split the prefix of the first $$$i$$$ blocks into $$$k$$$ groups with $$$m$$$ changes in each group. Assume for the sake of contradiction that $$$m = x$$$ and $$$m = x + 1$$$ are both good. When $$$m = x$$$, there are at most $$$x * k + k - 1$$$ changes in the prefix (there are $$$x$$$ changes in each block, and at most $$$k - 1$$$ changes between blocks). On the other hand, when $$$m = x + 1$$$, there are at least $$$(x + 1) * k = x * k + k$$$ changes in the prefix (there are at least x + 1 changes in each block). Contradiction. The same argument can be applied for two values of $$$m$$$ which differ by more than $$$1$$$ as well, proving that for a fixed pair $$$(k, i)$$$ there is only a single good value of $$$m$$$.
Maybe it was obvious to other people, but I had to think a while before realizing.
In 2092B C++ solution, should there be an OR condition like this
along with this?
Because if n is odd then cnt2 could also be of odd length and cnt1 of even?
Nice problem
In Problem E, Why are we excluding the corners?
Because each corner cell has 2 neighbours, which is even.
Got it and also understood the hint statement completely now. Thanks.
Was having a hard time figuring out what the optimal value of $$$ d $$$ would be for A, and didn't quite get how they arrived at the result even after reading the editorial. So heres a more noob friendly explaination if anyone else is having the same doubt:
Assuming $$$ a \lt b $$$, we know $$$ gcd(a, b) = gcd(a, b - a) $$$. Applying this on the the condition we want to optimize in the problem, $$$ gcd(x + d, y + d) = gcd(x + d, y - x) $$$. The maximum possible $$$ gcd $$$ of two positive integers would be the lesser of the two numbers which is $$$ y - x $$$ here. Therefore, we arrive at the conclusion that $$$ gcd(x + d, y - x) \leq y - x $$$.
Now, addressing the part thats not explicitly explained in the editorial, to maximize the $$$ gcd $$$, or to make it equal to the maximum possible value we established earlier that is $$$ y - x $$$, we want $$$ gcd(x + d, y - x) = y - x $$$. We know that $$$ gcd(a, b) = b \Longleftrightarrow b \mid a $$$. So clearly we get $$$ y - x \mid x + d $$$. This means that $$$ x + d \equiv 0 \pmod{y - x} $$$. Therefore, on rearranging the terms, the smallest non-negative solution is $$$ d = (-x) \bmod{y - x} $$$, which is the proposed value of $$$ d $$$ that maximizes the condition.
In the Solution 2 of the problem D, the total operations should be $$$2z+2y-4x$$$
firstly we require $$$ 2x-2y$$$ operations to make $$$x=y$$$ as it takes atmost 2 operations to add one $$$a$$$ as we need to add both $$$a$$$ and $$$c$$$ in worst cases.
and now $$$x'=y$$$ , now we need exactly $$$2(z'-y)$$$ operations, now $$$z'$$$ may have become $$$z+(y-x)$$$
so, total operations = $$$2(z+(y-x) - y) + 2(y-x) $$$
finally = $$$2(z+y-2x) $$$
which is obviously < $$$2 *n$$$
Dam problem E is based on diddy party
For D problem the optimal solution would be always inserting the current rarest character. Let's say
cnt(a) <= cnt(b) <= cnt(c), ifbcorcbis present we can insertaand if it is not present then at leastacorcamust be present. Let's take theaccase.abcabacacbacacabacby this we have inserted
2a,1band1c.And eventually by doing this we will reach
cnt(a) == cnt(b) == cnt(c)Then why do we need to prove that operations will end within
2nsteps. Even though there is a proof, I think its unnecessary because this is the optimal way to insert.Does problem E have anything to do with Galois Field. I was trying to understand the editorial with the help of some tool and it said that the idea of cells with even neigbours not affecting the answer is something related algebraically to xor and mentioned something about GF(2). but I still do not understand how non-border cells will not affect the answer especially if they are neighbours of fixed cells. can someone explain
for second question we can directly find it like for any even indices of first string can be replaced by any odd indices of second string and vice a versa. so we want to replace all ones of first string with zeros from second string such that their indices have different parity. so we need to count ones at even indices in first string and it should be less than or equal to the zeros count at odd indices in the second string and it should be same for ones count at odd indices in first string should be less than or equal to the zeros count at even indices in second string.