Idea: soullless
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int x;
cin >> x;
int mn = 9;
while(x > 0) {
mn = min(mn, x % 10);
x/=10;
}
cout << mn << endl;
}
}
2126B - No Casino in the Mountains
Idea: soullless
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n,k;
cin >> n >> k;
int a[n + 1];
for(int i = 1;i <= n;i++) cin >> a[i];
int ans = 0;
int cnt = 0;
for(int i = 1;i <= n;i++) {
if(a[i] == 1) {
ans+=(cnt + 1)/(k + 1);
cnt = 0;
continue;
}
else {
cnt++;
}
}
ans+=(cnt + 1)/(k + 1);
cout << ans << endl;
}
}
2126C - I Will Definitely Make It
Idea: soullless
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n,p;
cin >> n >> p;
int a[n + 1];
for(int i = 1;i <= n;i++) cin >> a[i];
int cur = a[p];
int dist = a[p];
sort(a + 1,a + n + 1);
bool ans = true;
for(int i = 1;i <= n;i++) {
if(a[i] < cur) continue;
if(a[i] — cur > dist) {
ans = false;
}
cur = a[i];
}
if(ans) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
Idea: soullless
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n,k;
cin >> n >> k;
pair <int,pair <int,int> > p[n + 1];
for(int i = 1;i <= n;i++) cin >> p[i].first >> p[ыi].second.first >> p[i].second.second;
sort(p + 1,p + n + 1);
int cur = k;
for(int i = 1;i <= n;i++) {
if(p[i].first > cur) break;
cur = max(cur,p[i].second.second);
}
cout << cur << endl;
}
}
Idea: Away_in_the_heavens
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long a[n + 1];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long b[n + 1];
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
long long ans[n + 1];
for (int i = n; i >= 1; i--) {
ans[i] = lcm(a[i], b[i]);
}
bool ch = 1;
if(ans[1] != a[1]) ch = 0;
if(ans[n] != b[n]) ch = 0;
for (int i = 2; i <= n; i++) {
if (__gcd(a[i — 1], ans[i]) != a[i]) {
ch = 0;
}
}
for (int i = n — 1; i >= 1; i--) {
if (__gcd(b[i + 1], ans[i]) != b[i]) {
ch = 0;
}
}
if (ch) {
cout << "YES" << "\n";
} else {
cout << "NO" << "\n";
}
}
}
Idea: soullless
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int B = 2e5 + 100;
int n;
int col[B];
long long costt = 0;
vector <pair <int,int> > reb[B];
bool was[B];
map <int,long long> cnt[B];
int pred[B];
long long zn[B];
void dfs(int v) {
was[v] = true;
for(auto u:reb[v]) {
if(was[u.first]) {
pred[v] = u.first;
continue;
}
if(col[v] != col[u.first]) costt+=u.second;
dfs(u.first);
zn[u.first] = u.second;
cnt[v][col[u.first]]+=zn[u.first];
}
}
void update(int v,int x) {
if(v != 1) {
cnt[pred[v]][col[v]]-=zn[v];
if(col[pred[v]] == col[v]) costt+=zn[v];
cnt[pred[v]][x]+=zn[v];
if(col[pred[v]] == x) costt-=zn[v];
}
else {
}
costt+=cnt[v][col[v]];
costt-=cnt[v][x];
col[v] = x;
}
void clr() {
for(int i = 1;i <= n;i++) {
reb[i].clear();
was[i] = false;
cnt[i].clear();
zn[i] = 0;
}
costt = 0;
}
int main() {
int t;
cin >> t;
while(t--) {
int q;
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> col[i];
for(int i = 1; i < n; i++) {
int u, v, c;
cin >> u >> v >> c;
reb[v].push_back({u,c});
reb[u].push_back({v,c});
}
dfs(1);
while(q--) {
int pos,x;
cin >> pos >> x;
update(pos,x);
cout << costt << "\n";
}
clr();
}
}
2126G1 - Big Wins! (easy version)
Idea: soullless
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int B = 2e5 + 100;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
int a[n + 1];
for(int i = 1;i <= n;i++) cin >> a[i];
int ans = 0;
for(int i = 1;i <= 100;i++) {
int b[n + 1] = {};
for(int j = 1;j <= n;j++) {
if(a[j] >= i) b[j] = 1;
else b[j] = -1;
}
int pref[n + 1] = {};
for(int j = 1; j <= n; j++) {
pref[j] = pref[j - 1] + b[j];
}
int prefmn[n + 2] = {},suffmx[n + 1] = {};
prefmn[0] = 0,suffmx[n] = pref[n];
for(int j = 1; j <= n; j++) {
prefmn[j] = min(prefmn[j - 1],pref[j]);
}
for(int j = n - 1; j >= 1; j--) {
suffmx[j] = max(suffmx[j + 1],pref[j]);
}
for(int j = 1; j <= n; j++) {
if(prefmn[j - 1] <= pref[j] || pref[j - 1] <= suffmx[j]) {
ans = max(ans,i - a[j]);
}
}
}
cout << ans << endl;
}
}
2126G2 - Big Wins! (hard version)
Idea: soullless
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
int n;
int a[200005 + 2];
array<int, 4> t[800005];
array<int, 4> merge(array<int, 4>a, array<int, 4>b) {
int ans = max(a[0], b[0]);
int mxpref = max(a[1], a[3] + b[1]);
int mxsuff = max(b[2], a[2] + b[3]);
ans = max({ans, mxpref, mxsuff});
int sum = a[3] + b[3];
return {ans, mxpref, mxsuff, sum};
}
void update(int v, int tl, int tr, int pos, int val) {
if (tl == tr) {
t[v] = {max(val, 0), max(val, 0), max(val, 0), val};
} else {
int tm = (tl + tr) >> 1;
if (pos <= tm) {
update(v * 2, tl, tm, pos, val);
} else {
update(v * 2 + 1, tm + 1, tr, pos, val);
}
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
}
array<int, 4> get(int v, int tl, int tr, int l, int r) {
if (tl == l && tr == r) {
return t[v];
} else if (l > r) {
return {0, 0, 0, 0};
} else {
int tm = (tl + tr) >> 1;
return merge(get(v * 2, tl, tm, l, min(r, tm)), get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
}
int solve(int n, vector<int>A) {
int m = 0;
for (int i = 1; i <= n; i++) {
a[i] = A[i — 1];
m = max(m, a[i]);
}
vector<int>ind[m + 1];
for (int i = 1; i <= n; i++) {
ind[a[i]].push_back(i);
}
stack<int>s;
s.push(0);
a[0] = -INT_MAX;
int l[n + 1];
for (int i = 1; i <= n; i++) {
while (a[s.top()] >= a[i]) {
s.pop();
}
l[i] = s.top() + 1;
s.push(i);
}
a[n + 1] = -INT_MAX;
s.push(n + 1);
int r[n + 1];
for (int i = n; i >= 1; i--) {
while (a[s.top()] >= a[i]) {
s.pop();
}
r[i] = s.top() — 1;
s.push(i);
}
int med = 1;
for (int i = 1; i <= n; i++) {
update(1, 1, n, i, 1);
}
for (auto u : ind[1]) {
update(1, 1, n, u, -1);
}
int ans = 0;
for (int mn = 1; mn <= m; mn++) {
for (auto u : ind[mn]) {
int lg = l[u], rg = r[u];
while (med < m && get(1, 1, n, lg, u — 1)[2] + get(1, 1, n, u + 1, rg)[1] + (a[u] < (med) ? -1 : 1) >= 0) {
med++;
for (auto u : ind[med]) {
update(1, 1, n, u, -1);
}
}
}
// cout << med << ' ' << mn << endl;
ans = max(ans, med — mn);
}
return ans;
}
int solveslow(int n, vector<int>A) {
for (int i = 1; i <= n; i++) {
a[i] = A[i — 1];
}
int mx = 0;
for (int i = 1; i <= n; i++) {
vector<int>v;
for (int j = i; j <= n; j++) {
v.push_back(a[j]);
sort(v.begin(), v.end());
mx = max(mx, v[v.size() / 2] — v[0]);
}
}
return mx;
}
int rnd() {
return (rand() + rand() * RAND_MAX);
}
main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while(tt--) {
int n;
cin >> n;
vector<int>v;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v.push_back(x);
}
cout << solve(n, v) << endl;
// cout << solveslow(n, v) << endl;
for(int i = 0;i <= n*4;i++) {
t[i][0] = 0;
t[i][1] = 0;
t[i][2] = 0;
t[i][3] = 0;
}
}
}








solution section for problem C, E and G2 seems to be not formatted well
thanks? edited
F also
I think so jiangly had some different solution for G which is using DSU. If anyone could explain it that would be great.
For G1, I had the same solution in mind but why are we not ensuring the existence of $$$m$$$ in $$$(l, r]$$$? I got confused because of that and left
because lets say for some
m, you found a valid segment(l, r]It may be possible that m doesn't lie in that segment or some value greater thanmmight become the median. In the above mentioned cases, when you eventually move to larger values ofm, the range sum for the same subarray will still be non-negative! (edit: In that case, the answer is BOUND to be greater than the currently computed value)see this case:
lets only operate on index
i=0arr[] = {1, 3, 3, 4}b = [1, 1, 1, 1]Since a valid range exists, my answer will be updated toans = m - a[0] = 1 - 1 = 0b = [-1, 1, 1, 1]Valid range exists for i = 0, soans = m - a[0] = 2 - 1 = 1b = [-1, 1, 1, 1]Valid range exists for i = 0, soans = m - a[0] = 3 - 1 = 2b = [-1, -1, -1, 1]It's pretty clear that no valid subarray exists for i = 0so my final answer become 2, hope this helps..
Fast editorial!!!
Hi, I am new to this platform, I am unable to view others submissions for some reason. In the source section it shows N/A. Not sure what is the reason for this, can anybody help me out with this?
How to solve problem D when the value of "real" is not necessarily between l and r? I spent hours on this version of the problem. One approach is dp but states are too big. Is there any other way?
Here's a testcase:
Output: 100
Oh I thought that was the version initially and spent around 15 minutes thinking.
What I thought was we do lower_bound and upper_bound for finding the boundaries which can be reached and then store the maximum value of real and then go on exploring other values. My solution was initially coded for that, but then I just made a minor adjustment to pass this.
what if we create a graph using the real values, and each edge is based on if we can use one casino's real value to visit the other. Then we can recognize the casino's we can initially start from and perform a BFS for finding the largest node
Creating the graph would be the tedious part which will affect time complexity imo.
I believe this is the right approach. Perhaps with an interval tree that supports removal of intervals, it would be possible to run the BFS without actually creating a graph?
We can use a segment tree instead. Let's sort intervals by their left ends. Node responsible for an interval [l,r] contains a pair {maxR,ind}, where maxR is a maximum right end of intervals in range [l,r], ind is an index where this maximum was reached.
Alternative solution for $$$F$$$ (which I've seen quite a few times and are probably the reason for all the hacks):
Let's fix some $$$X$$$. For all vertices $$$u$$$ with $$$\mathrm{deg}(u) \le X$$$, we can just simulate the change and recalculate the answer in $$$\mathcal O(X)$$$. If $$$\mathrm{deg}(u) \gt X$$$, for all adjacent colors, we will store the sum of edges with that specific color that end in $$$u$$$. A color-update of such vertices can be performed in $$$\mathcal O(1)$$$ (add the contribution of the old color, remove the contribution of the new color).
It remains to update this "color-map". Since each vertex $$$v$$$ can be adjacent to at most $$$\frac{n}{X}$$$ such vertices, these updates can also be performed in $$$\mathcal O(n / X)$$$ time.
The total time complexity is $$$\mathcal O(n + q \cdot \max (X, n / X))$$$. The memory complexity is $$$\mathcal O \left(n + \frac{n}{X} \cdot n \right)$$$ if we remove the unused colors from the map.
If we choose $$$X = \sqrt{n}$$$, the total time complexity is $$$\mathcal O((n + q) \sqrt{n})$$$. Unfortunately, my solution with that choice of $$$X$$$ was hacked (MLE), but setting $$$X$$$ to $$$3500$$$ ($$$~5\sqrt{n}$$$) works.
same :( I had $$$ X = 400 $$$ as a constant and it TLE-ed. Later, I saw Dominater having $$$ X = \frac{\sqrt{n}}{4} $$$ and that worked. a good learning experience but it turned out to be the difference between my current performance and a master performance which would've shot me into expert :)
BTW, I tried $$$ X = \frac{\sqrt{n}}{k} $$$ for all all $$$ 2 \lt = k \lt = 11 $$$ and it works. But I couldn't get a single $$$ X = \sqrt{n} \pm k $$$ to work for some reason.
$$$X = \sqrt n \pm k$$$ is basically $$$X$$$ (especially for such small $$$k$$$ (or do you mean multiplication instead)). And is it possible that your definition of $$$X'$$$ is the opposite of mine, i.e. $$$X' = \frac{n}{X}$$$ 😅
Larger k too. I tried so many values but all tled
Maybe try $$$k = \frac {\sqrt n} 2$$$ xd
Hi , Can you please tell why is this submission giving MLE MySolution. I am not using any extra structure as compared to final solution given in solutions.
As mentioned in my comment, setting $$$X = \sqrt n$$$ gives mle, try using
lmt = 3500.My initial idea was similar; I would compare the degree of the vertex with the number of queries until the next time the vertex was painted. If the degree was smaller, I would update all neighbors, else I would update everything between the current query and the next query of v. I believe this comes out to the same time complexity, but it was practically too slow.
Looks like there were many solutions for $$$G2$$$, here's mine using Persistent Segment Trees and maximum subarray sum queries (somewhat similar to the editorial but worse time complexity xd). (This segtree extension was also part of the solution of a recent div 3H).
The general idea is to fix the index $$$i \in [1, n]$$$ of the minimum element and find the largest possible median containing $$$i$$$. We can binary search the median with the same construction of the array $$$b(m)$$$ as in the editorial of $$$G1$$$, i.e. for some fixed $$$m$$$, $$$b_j(m) = a_j \ge m\ ?\ 1 : -1$$$. Any non-negative subarray sum would correspond to a median $$$\ge m$$$. To ensure that $$$i$$$ is included in that subarray, we add a large number to $$$b_i(m)$$$, i.e. $$$b_i(m) = X + (a_i \ge m\ ?\ 1 : -1)$$$. One option would be $$$X = 2n$$$.
We need all the possible arrays of $$$b(m)$$$, which can be stored in a persistent segment tree. Start with $$$b_j = -1$$$ and for all $$$v \in [1, n]$$$ in descending order, do the following:
After doing that for all indices with value $$$v$$$ (and values $$$ \gt v$$$), our persistent segment tree will have the values of $$$b(v)$$$.
While iterating over the $$$i$$$ and doing the binary search, we need to add $$$2n$$$ to $$$b_i$$$. This is easy, since we can simply update $$$b_i$$$ and "forget" about the update.
The total time complexity is $$$\mathcal O(n \cdot \log^2(n))$$$, where the $$$\log$$$ factors come from binary search and persistent segment tree update. The memory complexity is around $$$\mathcal O(n \cdot \log n)$$$, but this has a high constant hidden, since we need to store 4
ints in each vertex of our segment tree for the queries. It is important that we remove the persistent segment tree vertices we create while querying — otherwise, we will need quite a lot of memory ...Submission
Nice.. My first idea was using two different persistent segment trees (one for the normal array, one for reversed) to store max prefix sums but kept getting TLE or MLE.
Later I updated it to a single persistent lazy tree storing a pair of {min, max} prefix sums. That was enough to pass. submission
Memory Limit was quite low (and the intended memory limit is $$$\mathcal O(n)$$$, so makes sense that $$$\mathcal O(n \log n)$$$ will be tight), so even constant factors matter :/
I am sorry if I missed something, but in the proof for the solution of problem E, I think there are times when $$$P_i=\frac{p_i}{g}$$$ and $$$x=\frac{p_{i-1}}{p_i}$$$ are not coprime: For example, if the underlying array is [49, 7, 1], then the prefix array would be [49, 7, 1] and the suffix array is [1, 1, 1]. If we let $$$i=2$$$, then $$$P_i=\frac{7}{1}=7$$$ and $$$x=\frac{49}{7}=7$$$, and they are not coprime.
Is there something I’m missing here? I’d appreciate any clarification.
I came up with another proof. Hope this might help.
Theorem. If the prefix and suffix gcd sequences $$$p_{1:n}$$$ and $$$s_{1:n}$$$ are valid (i.e., there exists an original sequence whose prefix and suffix gcd sequences are $$$p_{1:n}$$$ and $$$s_{1:n}$$$), then sequence $$$a_{1:n}$$$, defined by $$$a_k=lcm(p_k,s_k)=\frac{p_ks_k}{gcd(p_k,s_k)}$$$, has a prefix gcd sequence of $$$p_{1:n}$$$ and a suffix gcd sequence of $$$s_{1:n}$$$.
Proof: First, we can prove $$$gcd(a_1)=a_1=p_1$$$ easily using the fact that $$$gcd(p_1,s_1)=s_1$$$.
Then, we can prove the rest with mathematical induction. Assume that $$$gcd(a_{1:{k-1}})=p_{k-1}$$$, we want to prove $$$gcd(a_{1:k})=p_k$$$.
Therefore, proving $$$gcd(a_{1:k})=p_k$$$ is equivalent to proving $$$\frac{p_{k-1}}{p_k}$$$ and $$$\frac{s_k}{gcd(p_k,s_k)}$$$ are coprime.
Given the validity of $$$p_{1:n}$$$ and $$$s_{1:n}$$$, there exists an original sequence $$$b_{1:n}$$$ such that they are its prefix and suffix gcd sequences. Then, we have:
Because $$$gcd(gcd(b_{1:k-1}),gcd(b_{k:n}))=gcd(b_{1:n})$$$, $$$\frac{gcd(b_{1:k-1})}{gcd(b_{1:n})}$$$ and $$$\frac{gcd(b_{k:n})}{gcd(b_{1:n})}$$$ are coprime, and therefore $$$\frac{gcd(b_{1:k-1})}{gcd(b_{1:k})}$$$ and $$$\frac{gcd(b_{k:n})}{gcd(b_{1:n})}$$$ are coprime, due to the fact that $$$gcd(b_{1:n})|gcd(b_{1:k})$$$.
Now we have proven that the prefix gcd sequence of $$$a_{1:n}$$$ is $$$p_{1:n}$$$, and the proof for the suffix gcd sequence is symmetric.
Corollary. The prefix and suffix gcd sequences $$$p_{1:n}$$$ and $$$s_{1:n}$$$ are valid, if and only if sequence $$$a_{1:n}$$$, defined by $$$a_k=lcm(p_k,s_k)=\frac{p_ks_k}{gcd(p_k,s_k)}$$$, has a prefix gcd sequence of $$$p_{1:n}$$$ and a suffix gcd sequence of $$$s_{1:n}$$$.
Proof: Trivial. Therefore, we can construct $$$a_{1:n}$$$ and see if its prefix and suffix gcd sequence fit with $$$p_{1:n}$$$ and $$$s_{1:n}$$$ to judge if they are valid.
I didn't quite get how you concluded if and only if, because I don't see you proving the uniqueness of the sequence of b. But otherwise, well rounded proof to show why it works.
Sorry for skipping the if and only if part, felt too tired and just slacked off lol.
From the theorem, it is proven that if the prefix and suffix gcd sequences are valid, then our construction is a valid underlying sequence. But conversely, if the construction is a valid underlying sequence for the prefix and suffix gcd sequences, then it means that there exists an underlying sequence to have the given sequences as its prefix and suffix gcd sequences, which means that the prefix and suffix gcd sequences are valid, so it is an iff.
The theorem is proving something like: if there exists a solution to the problem, then X is a solution to the problem. But conversely if X is a solution to the problem, then there exists a solution to the problem.
As for the uniqueness of sequence b, it is just a device to show the properties of the sequences p and s, so I don't need it to be unique, I just need it to exist, which is assured by the definition of validity in the problem.
Yeah lol the 3rd paragraph was what I was wondering about, it felt obvious XD. Yeah but nice proof
Yes, I believe there is a mistake, as do you. I'm also trying to understand their proof, but I couldn't get it from yesterday lol. Isn't there an error in step 3? soullless
my g2 sol uses a two pointer approach while using segtree to handle query and updates. you can find nlogn solution here
Your solution is very interesting. At first impression, the merge function seemed to be non-commutative. But when I tried with a bottom-up implementation of segment tree, it worked without any handling of segment order, even when $$$n$$$ was not a power of 2. Would you care to explain why it works in this case?
because when you dont use a build function and build through a "update function", the range a node covers is calculated dynamically. they are calculated at "runtime" and are always the same for same n.
After much pondering over what you said, I decided to investigate what was really happening. I found that, in most implementations, the update function merges nodes $$$2i$$$ with $$$2i+1$$$ into node $$$i$$$, which may not be the correct order for all $$$i$$$ when $$$n$$$ is not a power of 2. However, the query function follows a different pattern, so that problematic nodes will be skipped and correct order is preserved (even if the right pointer falls in a subtree to the left of that of the left pointer). Anyway, thanks for the reply!
catch up to me on discord #hippie4640
For E: Should it be "Key arithmetic facts: $$$S_i$$$ and $$$x$$$ are always coprime, because $$$x = \frac{P_{i-1}}{P_i}$$$ and no prime can divide both a number and its own quotient. Symmetrically, $$$P_i$$$ and $$$y$$$ are coprime." ?
doubt solved
Code above is correct solution, 72 is not divisible by 48 so the answer "NO" is correct for given test case.
soullless Why didn't you decide to accept $$$O(n\sqrt{n}\cdot logn)$$$ solutions in F?
By the plan, people had to solve problem by using brain, not just by transfering the problem into data structure that they know
Why is there a surge of people doing heavy-light partitioning in Div 3? I mean, was it that hard to do a simple tree-based solution? Look, it's not a 2400-rated D2E.
For real
Is there an issue with the solution for problem D?
For example, take this test case:
3 5
1 2 100
4 6 9
4 6 9
The reference program outputs 100, but the correct answer should be 9.
"In the solution to problem G1, I have a different opinion. I believe the correct case should be when the suffix maximum is greater than the prefix minimum."
When I thought carefully about it, I also arrived at this conclusion. Initially, I didn't understand the editorial and even thought it might be incorrect. But after thinking it through, I believe the editorial can be interpreted this way:
We only need to check whether a subarray ending (or starting) at position $$$j$$$ exists because — suppose both intervals $$$[x, j]$$$ and $$$[j, y]$$$ are invalid. What does that imply? It means that whether we extend to the left or to the right, in any such extended subarray, the number of elements greater than or equal to $$$m$$$ is less than half of the total number of elements. In this case, if we try to enlarge the interval from one end, the other end becomes a burden — it won’t help us make the number of elements $$$\ge m$$$ reach or exceed half.
This is why the condition in the editorial — that the suffix maximum is greater than or equal to the prefix minimum — is equivalent to the requirement. That is, either the suffix maximum is at position $$$j$$$, or the prefix minimum is at position $$$j$$$; at least one of these must hold.
From another perspective, $$$a[j]$$$ is at worst $$$-1$$$. If extending in either direction from $$$j$$$ fails to make the sum greater than or equal to $$$0$$$, then the best possible result we can achieve is just $$$-1$$$.
I thought about it more carefully and realized you were right. I forgot to consider that the maximum value of the suffix would also include the maximum value of the prefix, so my approach might not include 'j' in the result.
Problem D: Why does my DSU approach gives TLE on test 28.. it must be O(n.logn) Submission: 330766212
For E, you can also just compute $$$a_i := lcm(p_i,s_i)$$$, and then verify that $$$a$$$ produces $$$p$$$ and $$$s$$$.
I did the same thing
Can you please tell why does that work, i can't think about it intuitively
Because both $$$ p_i $$$ and $$$ s_i $$$ divide $$$ a_i $$$. It is sufficient because the constructed array of lcm will always satisfy the given $$$ p $$$ and $$$s$$$, if they are valid, again as the calculated $$$ p_i$$$ and $$$s_i $$$ from lcm array are the same as given.
2126A — Only One Digit
solution- https://youtu.be/T2g3bK6eNrM?si=d49_BqqFjlu1jTkh
Problem F: why use map instead of unordered_map? The elements don't need to be sorted and map offers worse time complexity for access
For Problem D:
1 9 20, 1 9 8, 8 8 100
the editorial solution gives answer 20 but the actual answer is 100 right?
In problem F, different heavy-light partition ways will get different verdict(TLE or AC) in spite of all of them is $$$O(\sqrt{n} \cdot logn)$$$ per query. I found a way that can AC(640ms): https://codeforces.me/contest/2126/submission/350566213
Had an alternative solution to E : https://codeforces.me/contest/2126/submission/352395496
Tried to create the original array and verify if the pref and sum gcd arrays match for the created array. An element i in the original array at a bare minimum has to have both p[i] and s[i] as factors. So generate an array v where v[i] = (p[i] * s[i]) / gcd(p[i], s[i]). This ensures that v[i] does not have any additional factors and only the bare minimum to be valid for both p[i] and s[i] values simultaneously.
Then to just verify p and s using this array. Can someone help with how to prove that this would work for all cases?
How is F rated 2000. The logic is simple. May be critical edge cases??
I am stucked in a doubt in Problem D. In editorial, it is checking only lower bound. It is not checking if curr value is greater than ri? And, It is clearly given in the question, that we can only enter in the casino, if our coin's value is between li and ri.
can someone explain this to me.
the solution of the problem E is not formatted correctly really difficult to even understand the solution
Problem B has already a neat solution, but other way to think of it in terms of sliding window is as follows.
The idea is to maintain a sliding window of size
kand check two things:0(good weather),To check if a window contains only zeros, maintain a variable
rainwhich stores the number of1s in the current window. Using a sliding window:At any index
i ≥ k-1, the window[i-k+1 ... i]is valid ifrain == 0.Next, we need to enforce the break condition. For this, maintain a variable
nextwhich represents the earliest index where the next hike can start.When we find a valid window:
start = i - k + 1start >= nextSo the condition becomes:
After taking a hike:
[start ... i]i+1is a mandatory breaki+2So we update:
Overall, we iterate once over the array, maintaining the sliding window and greedily taking the earliest possible valid hike.
This greedy works because taking a hike as early as possible always leaves more room for future hikes, and never reduces the total count.
Complete implementation:
Don't get how test case 1 for C is 3->2->1->4->5
shouldn't it be 1->2->3->4->5? as we need to climb in a non-decreasing manner.
Is this a valid test case for C??
3 5 4 10 9 5 6 5 6 7 6if yes , then the number of coins won't be monotonic as said in the editorial!!!!
I did not understand the solution of problem E after reading the editorial, especially due to the "key arithmetic fact" part, which just sent me. (Maybe I am just stupid)
So I tried to derive a valid proof on my own again (first time I did it incorrectly hence I got a WA). It's as follows:
The first 3 necessary conditions are quite obvious from the editorial, so I won't go deep into that.
But for the sufficient condition..
We know the overall GCD of the hidden array a must be just one value. The overall GCD (lets call it g) can also be calculated by finding the GCD of "the prefix GCD at index i, the suffix GCD at index i + 1" for i in range [1, n — 1]. This works as the prefix of i and the suffix of (i + 1) covers the entire array.
So essentially we must check if (g == gcd(p[i], s[i + 1])) holds true in the domain of i.
We can easily prove this is a sufficient condition through contradiction.
So to check if the given arrays p and s are valid for a hidden array a, we need to check the three necessary conditions and then this last sufficient condition.
If all checks out, we print YES, else NO.