Thank you for participating in our round ! We hope you enjoyed our problems. Feel free to discuss them in the comment section :)
/!\ The solution codes will be available in a few minutes
UPD: The solution codes have been added ! UPD2: The solution codes links are fixed ! Sorry for the inconvenience.
Let's say a bag is expensive if $$$a_i \gt c$$$ and free if $$$a_i \leq c$$$.
First, observe that as long as there is at least one free bag remaining, we should destroy a free bag. We will perform all expensive destructions at the end. The goal is to perform as much free destructions as possible.
When there are multiple free bags, which one to choose? The bigger a bag is, the less time you have to destroy it before it becomes expensive. Hence, at each second, you should destroy the biggest remaining free bag.
This process can be naively simulated in $$$\mathcal{O}(n^2)$$$, which was sufficient under given constraints. If you sort the array beforehand and keep the current multiplier in a separate variable (instead of multiplying all weights), it can be done in $$$\mathcal{O}(n \log n)$$$ time.
Instead of taking either the leftmost or rightmost element, think about taking either the minimum or maximum element.
It's possible to ensure that $$$q_1 \lt q_2 \gt q_3 \lt q_4 \gt q_5 \ldots$$$
Let's number turns from $$$1$$$ to $$$n$$$. On odd turns, we take the minimum element and on even turns, we take the maximum element. We claim that $$$q_1 \lt q_2 \gt q_3 \lt q_4 \gt q_5 \ldots$$$ (which is actually stronger than what the statement asked).
Proof: consider taking the minimum on an odd turn : $$$q_i = \min(p_l, p_r)$$$ where $$$l, r$$$ were the endpoints of the remaining array at turn $$$i$$$. Suppose without loss of generality the minimum was on the left, i.e. $$$q_i = p_l \lt p_r$$$. Then at the next turn, $$$p_r$$$ is still available:
For example, if the remaining array at an odd turn is $$$[5, ?, \ldots, 10]$$$, we take the minimum $$$5$$$, and then no matter what the $$$?$$$ is, we take at least $$$10$$$ which is greater than $$$5$$$. This proves $$$q_i \lt q_{i+1}$$$ when $$$i$$$ is odd. Similarly, we can prove $$$q_i \gt q_{i+1}$$$ when $$$i$$$ is even.
Try to come up with a sufficient condition first, then prove it's necessary. Doing some examples with $$$n = 3$$$ might be useful.
Let $$$m_i = \min(b_1, \ldots, b_{i-1})$$$ for every $$$2 \leq i \leq n$$$, which can be computed in linear time. We claim we can achieve $$$a = b$$$ if and only if $$$b_i - m_i \lt m_i$$$ for every $$$2 \leq i \leq n$$$.
Proof that it's sufficient: we can build the array from left to right. For every $$$i$$$, if $$$b_i \lt m_i$$$ then we directly add $$$b_i$$$. Otherwise, we first add $$$b_i - m_i$$$, then $$$m_i$$$.
Proof that it's necessary: suppose we managed to reach $$$b$$$. At every step, $$$a_i \leq b_i$$$ for every $$$i$$$. Consider the last operation $$$x$$$ that incremented $$$a_i$$$. Then, by the definition of $$$i$$$, we must have
Hence, $$$b_i = a_i + x \leq 2x-1 \leq 2m_i - 1$$$.
Let $$$b_i = \max(a_i, a_{i+1})$$$, show that $$$b_1 \geq b_2 \geq \ldots \geq b_{n-1}$$$.
If $$$a_i \lt a_{i+1}$$$, show that $$$a_{i+1} \gt \max(a_{i+2}, a_{i+3}, \ldots a_{n})$$$.
Let $$${i_1, \ldots, i_m}$$$ be the set of $$$i \in [1, n]$$$ such that $$$a_i \gt a_{i+1}$$$. Let us show that the sequence $$$(i_1, \ldots, i_m)$$$ is an LDS:
- It is a decreasing subsequence: let $$$k \in [1, m -1]$$$. If $$$i_{k+1} = i_{k}+1$$$, then $$$a_{i_k} \gt a_{(i_k) + 1} = a_{i_{k+1}}$$$. Else, we have $$$a_{(i_k) + 2} \lt \max(a_{i_k}, a_{(i_k)+1})$$$, but $$$i_k + 1$$$ is such that $$$a_{i_{k}+2} \geq a_{i_{k+1}}$$$, so $$$a_{(i_k) + 2} \lt a_{i_k}$$$. We have also $$$a_{i_k + 1} \lt a_{i_k}$$$ by definition so we can easily prove by induction on $$$j \gt 0$$$ that $$$a_{i_k + j} \lt a_{i_k}$$$ where it makes sense.
- It's optimal : let $$$E$$$ be the set of indexes taken by a LDS. For each i such that $$$a_i \leq a_{i+1}$$$, at least one of the elements of $$${i, i+1}$$$ does not belong to $$$E$$$. Now, these sets are disjoint : if $$$a_i \lt a_{i+1}$$$ we cannot have $$$a_{i+2} \lt a_{i+1}$$$ (otherwise the condition $$$\max(a_i, a_{i+1}) \gt a_{i+2}$$$ is not met. So $$$|E| \leq m$$$.
To calculate the sum of the LDS of the sub-arrays, it is therefore sufficient to count for each $$$i$$$ such that $$$a_i \lt a_{i+1}$$$ the number of sub-arrays $$$a[l,r]$$$ which contain $$$i$$$ and $$$i + 1$$$. It's $$$(i+1) \cdot (n - i - 1)$$$ for $$$0$$$-indexation : a necessary and sufficient condition is that $$$l \leq i$$$ and $$$r \geq i + 1$$$.
Note that there also exists a dp approach, see this comment.
2128E1 - Submedians (Easy Version)
You can use binary search on the answer. Why ?
Let's start with a, perhaps, more natural task: finding whether or not $$$v$$$ is a submedian. To do so, we create an array $$$\text{big}$$$ such that $$$\text{big}[i] = 1 \text{ if } \text{a}[i] \geq v \text{ else } -1$$$
We also create an array $$$\text{small}$$$ such that $$$\text{small}[i] = 1 \text{ if } \text{a}[i] \leq v \text{ else } -1$$$
Knowing whether v is a submedian or not requires using both $$$\mathrm{big}$$$ and $$$\mathrm{small}$$$ ; when you want to know if the largest submedian is larger than or equal to v, do you actually need both arrays?
Solve the relaxed problem where we only consider the first three constraints (and neglect the constraint on $$$\mathrm{small}$$$) i.e finding the largest $$$v$$$ such that:
- $$$1 \leq l \leq r \leq n$$$,
- $$$r - l + 1 \geq k$$$,
- $$$\text{big}[l] + \dots + \text{big}[r] \geq 0$$$
Use binary search and prefix sums to rewrite the constraint on $$$\text{big}$$$
Is the resulting value a submedian ?
First, note that the predicate ``there exists a submedian $$$\geq v$$$'' is monotonic. That is, if there exists a submedian $$$\geq v$$$, then for all $$$v' \lt v$$$, there exists a submedian $$$\geq v$$$. Thus, we can binary search to find the largest $$$v$$$ such that there exists a submedian $$$\geq v$$$. Such a $$$v$$$ will be the largest submedian.
Now, what is left is to check is, for a fixed $$$v$$$, whether or not there exists a submedian $$$\geq v$$$.
Let's start with a, perhaps, more natural task: finding whether or not $$$v$$$ is a submedian. To do so, we create an array $$$\text{big}$$$ such that $$$\text{big}[i] = 1 \text{ if } \text{a}[i] \geq v \text{ else } -1$$$
We also create an array $$$\text{small}$$$ such that $$$\text{small}[i] = 1 \text{ if } \text{a}[i] \leq v \text{ else } -1$$$
Using these notations, $$$v$$$ is a submedian iff there exists $$$(l, r)$$$ such that:
- $$$1 \leq l \leq r \leq n$$$,
- $$$r - l + 1 \geq k$$$,
- $$$\text{big}[l] + \dots + \text{big}[r] \geq 0$$$
- $$$\text{small}[l] + \dots + \text{small}[r] \geq 0$$$
Let's try to solve a relaxed problem where we only consider the first three constraints (and neglect the constraint on $$$\text{small}$$$ i.e finding the largest $$$v$$$ such that:
- $$$1 \leq l \leq r \leq n$$$,
- $$$r - l + 1 \geq k$$$,
- $$$\text{big}[l] + \dots + \text{big}[r] \geq 0$$$
Such a problem is standard and can be easily solved in $$$\mathcal{O}(n \log n)$$$ by binary search and prefix sums. Indeed, for a fixed $$$v$$$, we can iterate over the value of $$$r$$$. Now: $$$\text{big}[l] + \dots + \text{big}[r] \geq 0$$$ iff $$$\text{pref}[r] - \text{pref}[l-1] \geq 0$$$ i.e $$$\text{pref}[r] \geq \text{pref}[l-1]$$$ so taking $$$l$$$ that minimizes $$$\text{pref}[l-1]$$$ is optimal. Thus, it is enough to maintain such an $$$l$$$ in constant time (when you increase $$$r$$$, there is only one new candidate for $$$l$$$).
Now, I claim that the value of $$$v$$$ we get happens to be the largest submedian. Indeed, if it doesn't satisfy $$$\text{small}[l] + \dots + \text{small}[r] \geq 0$$$, then $$$v + 1$$$ would still satisfy $$$\text{big}[l] + \dots + \text{big}[r] \geq 0$$$ which contradicts the maximality of $$$v$$$.
2128E2 - Submedians (Hard Version)
The set of submedians is a range. Try to prove it.
For $$$l_1 \leq r_1$$$ and $$$l_2, r_2$$$ such that $$$|l_1 - l_2| + |r_1 - r_2| \leq 1$$$, both subarrays share at least one median. How to prove it ? Knowing this fact, how to prove Hint 1 ?
Find a discretely ``continuous'' path from the subarray of the minimum submedian, to the subarray of the maximum submedian
Read the editorial of E1 first.
In such problems, it can be useful to try an easier version, for example, finding maximal/minimal values (which is why the subtask is helpful).
Now, one can observe that if $$$x \leq y$$$ are submedians, then any $$$z$$$ such that $$$x \leq z \leq y$$$ is also a submedian ! One can prove this using the intermediate value theorem. Let's fix $$$x \leq z \leq y$$$ and $$$[l_1, r_1]$$$ (resp. $$$[l_2, r_2]$$$) be a range where $$$x$$$ (resp. $$$y$$$) is a submedian. wlog, assume $$$l_1 \lt l_2$$$. Start by sweeping $$$r_1$$$ to $$$l_2$$$. Then $$$r_1$$$ to $$$r_2$$$. And finally, $$$l_1$$$ to $$$l_2$$$. Then one of the ranges that we covered has submedian $$$z$$$. Intuitively, when incrementing one of the ends of a range, the +1/-1 in the arrays big and small are not changing that much (the norm 1 distance is bounded by 2) so we can think of using the intermediate value theorem (see proof below). Also, note that at any time, the covered range is of length at least $$$k$$$.
Thus, it is enough to find the smallest and largest submedians (using the solution of E1) and maintain the set of submedians while sweeping. One way to do that is to use a fenwick tree to simulate an ordered set (or you can also use two range sum point update queries to simulate the \text{big} and \text{small} arrays). Alternatively, you can use two sets (one with the lower half of the values of the current range, and one with the upper half).
For each $$$[l, r]$$$ that our algorithm covers, we consider the point $$$(|{i \in [l,r] \mid a[i] \geq z}| - |{i \in [l,r] \mid a[i] \lt z}|, |{i \in [l,r] \mid a[i] \leq z}| - |{i \in [l,r] \mid a[i] \gt z}|)$$$ where $$$|A|$$$ is the cardinal of set $$$A$$$. Essentially, you can think of this point as an encoding of both conditions on small and big for $$$z$$$ to be a submedian.
Note that, when moving one end of the range, each coordinate of the point changes by at most 1 (in absolute value). Also note that, by a simple computation, the sum of both coordinates is positive (it is equal to twice the number of $$$i$$$ such that $$$a[i] = z$$$).
I claim that one of these points will have positive coordinates (thus, $$$z$$$ will be a submedian). Indeed: our path starts at a point with (negative first coordinate, positive second coordinate) and ends at a point with (positive first coordinate, negative second coordinate). Each move allows moving to a point at distance at most 2. However, you can see on the drawing below that one must pass through points with positive coordinates to achieve this (otherwise, you would be stuck in (negative first coordinate, positive second coordinate)).

In red, the area with points such that $$$x+y \geq 0$$$, below the green line are forbidden points. The blue points are our starting and ending points. Finally, the arrows are the possible moves after incrementing/decrementing one end of the range.
We shorten $$$\text{dist}_w$$$ by $$$d$$$. We denote $$$d_L$$$ as the distance when every edge has weight $$$l_i$$$, and $$$d_R$$$ as the distance when every edge has weight $$$r_i$$$.
Say an edge is green if its weight is $$$l_i$$$ and red if its weight is $$$r_i$$$. If the answer is YES, prove there exists a solution that colors a path from $$$1$$$ to $$$n$$$ in green and the rest of the edges in red.
Consider an arbitrary solution $$$w$$$ and let $$$P$$$ be the shortest path from $$$1$$$ to $$$n$$$ with respect to $$$w$$$. Decreasing the weight of an edge in $$$P$$$ will decrease $$$d(1, n)$$$ by exactly one and will decrease $$$d(1, k) + d(k, n)$$$ by at most one. Increasing the weight of an edge outside $$$P$$$ will not increase $$$d(1, n)$$$ and will not decrease $$$d(1, k) + d(k, n)$$$. By repeating these operations, we can make $$$P$$$ green and the rest red.
Let $$$P$$$ be the chosen green path. Find a necessary condition for the corresponding weight assignment to be correct. Then, prove if it's sufficient.
The condition is $$$d_L(u, v) \lt d_R(u, k) + d_R(k, v)$$$ for all $$$u, v \in P$$$.
It's obviously necessary; otherwise, $$$1 \rightarrow_L u \rightarrow_R k \rightarrow_R v \rightarrow_L n$$$ would have length $$$d(1, n)$$$. Conversely, if there exists a $$$1-n$$$ shortest path going through $$$k$$$, if you consider the last node in $$$P$$$ you visited before reaching $$$k$$$ and the first node in $$$P$$$ you visited after reaching $$$k$$$, this pair of nodes will violate the condition.
Try to build a correct path using a Dijkstra-like algorithm. It might be helpful to have an intuitive understanding of the condition found in the previous hint in order to quantify with a single integer how good the current path is. Imagine you're a robber going from $$$1$$$ to $$$n$$$ and you don't want to be caught by cops.
Imagine nodes are cities, $$$k$$$ is a police station, and you're some robber. Traversing the $$$i$$$-th edge takes $$$l_i$$$ seconds for the robber, and $$$r_i$$$ seconds for the citizens and the cops.
Each time you go through a city, the citizens of this city will run to the police station to warn the cops. Once the cops are warned, they start dispatching in all directions as fast as they can to catch up to you.
The current danger can be represented by an integer timer $$$x$$$. If $$$x \lt 0$$$, it means the cops will be warned in $$$-x$$$ seconds. If $$$x \geq 0$$$, it means the cops have been warned since $$$0$$$ seconds. It's the only information you need to remember.
Try to find for each node $$$u$$$ what is the corresponding minimum timer $$$t_u$$$. As the robber runs through the graph, the timer always increases, so you can do this with Dijkstra.
Please read all the hints first.
We first make a classical Dijkstra from $$$k$$$ with $$$r$$$-weighted edges. Let $$$d_u$$$ be the distance between $$$u$$$ and $$$k$$$ for citizens/cops.
We then make a modified Dijkstra, starting from the situation $$$(1, t_1)$$$ with $$$t_1 = -d_1$$$ (meaning the cops will be warned in $$$d_1$$$ seconds).
When you traverse the $$$i$$$-th edge to go from $$$(u, t)$$$ to $$$v$$$, the new timer will be
We have $$$t' \geq t+l_i$$$ because the previous timer continued to advance while the robber was traversing the edge, and $$$t' \geq -d_v$$$ because a new citizen started running from $$$v$$$ to the police station.
Each time, we take the situation with the minimum timer from the priority queue. If the node has already been processed, or if $$$d_u \leq t_u$$$ (meaning the cops can catch up to the robber in $$$v$$$), the situation is skipped. The answer is YES if and only if we processed the node $$$n$$$.
The time complexity is $$$\mathcal{O}((n + m) \log (n+m))$$$.








Auto comment: topic has been updated by bestial-42-centroids (previous revision, new revision, compare).
wow instant tutorial!
Thanks for the quick tutorial! Very good contest, I found problems level very well distributed. There wasn't a big gap in difficulty between consecutive problems, at least until E1. Well done!!!
C and E2 are cool, but neither D nor E1 should be in a div2
I humbly disagree with your opinion. In my opinion, D was an excellent problem.
C was way too easy IMO.
Agreed..
the problem with E is, it is similar to recent div3 G1/G2
if C was way too easy IMO, under which topic in IMO could it be appeared.
IMO is not some topic. sorry for confusion. here I used IMO in context of "In My Opinion".
If you are looking for topic to solve C, I would say try to see pattern. You can observe a pattern that If minimum of the prefix part from 0 to i-1 is 'm' , then in the index 'i' you can not add more than 2 * m — 1
How ? ( first you would add m-1 to index 'i', and then you would add 'm', so total 2 * m — 1 ).
I got stuck at ABC, ironically. Nevertheless, D and E1 were fun!
You can now rate the problems in the editorial !
Auto comment: topic has been updated by bestial-42-centroids (previous revision, new revision, compare).
1486D - Max Median
problem E1 is similar to this
isnt it the exact same
Indeed it's the same similar problem though in this div2 you check
pref[r] — pref[l — 1] >= 0
while that problem passes with pref[r] — pref[l — 1] > 0
E1 was pretty easy comparing to div2 E. I have solved at least 3 problems which uses this similar idea
Whats the proof for submedian being monotonic for E1?
did you get the proof
This is my understanding, but it might not be correct. for a sub list and a V, you need to satisfy 2 conditions 1. big condition, the number of elements in the list greater or equal to V must bigger than (|list|+1)/2, which wishes V to be small 2. small condition
submid want to fit the two conditions, which is not monotonic,for example [6,7,8] k=3, V=7 fit the two conditions, but V'=6 does not
however for one condition, such as big condition is monotonic the smaller V is, the more likely the big condition to be fitted
so we can binary search the biggest V to fit the big condition, and we also know the bigger V is, the more likely the small condition to be fitted
let v be the largest V fits big condition, [ans_l:ans_r] be the sub list. [v, ans_l, ans_r] is the answer. how to prove it?
we can infer v must be in the sub list if ith<v<i+1th, e[v]+g[v]-l[v]>=0, e[v]=0 (e stands for equal, g stands for greater, l stands for less, "ith" and "i+1th" respectively refer to the i-th and (i+1)-th largest numbers.) let v'=i+1th,l[v']=l[v],e[v']+g[v']=g[v], big[v']>=0 but v'>v The proof by contradiction holds.
so the small condition is also fitted
Instead you can just binary search on the condition that a median $$$\ge x$$$ exists iff in an array with $$$\text{b}[i] = 1$$$ if $$$\text{a}[i] \ge x$$$ else $$$-1$$$ you have some $$$\text{pref}[r] - \text{pref}[l] \ge 0$$$ with $$$r-l \ge k$$$ and there is nothing to prove. Also a classic trick.
This is what I meant by the predicate being monotonic. Essentially: if you know there exists a submedian $$$\geq x$$$, then the largest submedian is $$$\geq x$$$.
You might ask: what does it have to do with monotonicity ? Well, binary search on the answer can be explained like this:
You define a function $$$f: \mathbb{N} \rightarrow {0,1}$$$ (note that $$$\mathbb{N}$$$ could be replaced with something else), Assume that function is increasing (so essentially, either $$$f$$$ is constant, either there exists $$$k$$$ such that $$$f(0) = f(1) = ... = f(k) = 0$$$ and $$$f(x) = 1$$$ for $$$x \gt k$$$. Binary search allows you to find that $$$k$$$ i.e finding the last $$$0$$$ (or equivalently, the first $$$1$$$).
Now, in practice, it means that if $$$f$$$ is an "increasing", "question", you can find the smallest thing such that the answer to the question is YES.
PS: of course, a similar thing holds for decreasing
Sorry if it wasn't clear from the editorial
Yes, I did understand that part. That line did indeed make it clear (editorial is great) that you could binary search on median $$$\ge x$$$. However, after that you talk about something else that is interesting (but I suppose isn't the easiest way to solve the problem) so I'm assuming that's what confused people. In my comment I just wanted to point out that you can ignore all that (although I know you probably included it to provide intuition) and just do the binary search with the sole fact that you have a monotonic predicate.
Interesting E2.
Thanks!
"and will increase $$$d(1,k) + d(k,n)$$$"
I think there's a typo here, we're decreasing the weight of the edge, that can't happen
fixed, thanks!
Auto comment: topic has been updated by hugopm (previous revision, new revision, compare).
thanks for superfast editorial
D can be solved with dp too. Because of the constraint $$$ max(p_i, p_{i+1}) \gt p_{i+2} $$$, the $$$ LDS $$$ ending at index $$$ i+2 $$$ will always have one of $$$ p_i $$$ or $$$ p_{i+1} $$$ as the second last element. Hence, we get
This basically means that for jth index, all LDS ending at jth index will increase by 1 hence we get j increments and for all the elements after j, we will get a new LDS of length 1
My solution was also based on dp, though a bit cleaner. One can observe that either $$$p_i$$$ or $$$p_{i+ 1}$$$ is the greatest element on suffix from $$$i$$$. Let $$$dp_i$$$ be the sum of lengths of LDSs for subranges that start at $$$i$$$.
If $$$p_i \gt p_{i+1}$$$, $$$p_i$$$ is the suffix maximum, so we can always take it as the start of LDS and then find LDS from $$$i+1$$$ independently. Thus $$$dp_i = dp_{i+1} + n - i$$$ (in 0-indexation). $$$n - i$$$ is the number of times $$$p_i$$$ is used in LDS.
If $$$p_i \lt p_{i+1}$$$, we always take $$$p_{i+1}$$$ in LDS, but not $$$p_i$$$, unless the range is $$$[i, i]$$$, then we only take $$$p_i$$$. So $$$dp_i = dp_{i+2} + n - i$$$, because the total contribution of $$$p_i$$$ and $$$p_{i+1}$$$ remains $$$n - i$$$.
The answer is the sum of all $$$dp_i$$$
Neat solution! I included a link to it in the editorial :)
Here dp[i] means subranges that start exactly at i or subranges where l >= i?
Start exactly at $$$i$$$
If you iterate $$$i$$$ and define and update the dp from the other way around, it would be cleaner too:
or
submission
Yeah, that's what I did in contest too submission
i did also same
can you explain why dp[i]=dp[i-1]+1 ???
This happens when (arr[i+1] > arr[i])
So in this case we know that max(arr[i-1],arr[i]) > arr[i+1] and since arr[i] < arr[i+1] then arr[i-1] > arr[i+1].
And the other two observations would be — 1. arr[i-1] > arr[i] as arr[i+1] > arr[i]. 2. For any index i, the best possible LDS till index i will have arr[i] as the last element. You can prove it easily.
Hence basically all the LDS for i+1, will have all the elements of all LDS till i except the last element, the last element will get shifted to index i+1 for every LDS
Hence, dp[i] = dp[i+1] + 1
deleted
If p[i] < p[i+1] then just dp[i]= dp[i+1]+1. This could be cleaner.
genius
I can't understand why the total contribution of pi and pi+1 remains n — i ,could other friends help me?Thanks.
if a[i] is greater than a[i+1], for all subsequences with i+1 as the start , a[i] will be the new addition to all those subseqs in different subarrays made with i as the start , so to all those subseqs , 1 will be added , therefore we add n-i to dp[i+1] ,,but when a[i+1] is greater than a[i] , a[i] adds only one extra subsequence which is itself (because its smaller) ,, so dp[i]=dp[i+1]+1 in that case ,, which can also be written as dp[i]=dp[i+2]+n-i , which is basically the same thing as we use the contribution made by a[i+1] this time ,, but former is much easier to understand I think
I have understand it.Thank you very much!It's based on the relationship between now and future. I have some thoughts:This method is beautiful! But i think it's not better than the tourial because it use O(n) dp vector while the tourial not use.
yes tutorial one is better but I felt this was easier to understand
wait, we can space optimize dp one to use one variable only which we can reuse in the loop , so there you go , O(1)
I got it.You are so smart.
Noo , only reason I was trying to optimize this is I was not getting the original tutorial solution lol
Hey! I had a different idea and wanted to ask if it's close to the correct approach?
I thought of defining $$$dp[i]$$$ as the total number of decreasing subsequences that end in prefix $$$[1..i]$$$
Then I figured: if we already know $$$dp[i - 1]$$$, we can add all valid subsequences that include $$$a[i]$$$ and that gives us $$$dp[i]$$$
So the final answer will be $$$dp[n]$$$
$$$n = 3$$$, $$$a = [3, 2, 1]$$$
Base: $$$dp[0] = 0$$$
-- $$$dp[1] = dp[1 - 1] + 1 = 0 + 1 = 1$$$;
-- $$$dp[2] = dp[2 - 1] + 3 = 1 + 3 = 4$$$;
-- $$$dp[3] = dp[3 - 1] + 6 = 4 + 6 = 10$$$;
Your answer is very clear, I like it very much.
Such a cool solution, i think if we observe carefully, this solution works with just the condition that elements of the array $$$p$$$ are distinct. [This solution doesn't use the condition that $$$p$$$ is a permutation, it also doesn't use the fact $$$max(p_i, p_{i + 1}) \gt p_{i + 2}$$$, so this solution is more general.
No It uses — Take the permutation 1 2 3 4 5 6 The condition would fail
in condition pi<pi+1 what you are doing is dp[i] = dp[i+1] +n-(i+1)+1 because in all the sub arrays start with arr[i] thier LDS will start with arr[i+1]? am i correct ?
wow
really clean
Another solution is thinking it as a tree — https://mirror.codeforces.com/contest/2128/submission/347202769.
This lets you calculate for each element what is the contribution of it in the answer.
is this solution for all types of permutation? is this condition max(pi, pi+1) > pi+2 essential for the dp solution to work?
Yes, a nice observation does a lot of work here
so i dont understand, this condition max(pi, pi+1) > pi+2 doesnt matter? and it works for all permutation?
The condition does matter
can you give me short explanation why
how to prove lds at i+2 must take one of pi pr pi+1 as second last element ? wlog assume pi>pi+2 and pi+1<pi+2 then what if pi-1 has value < pi but also pi-1 >pi+2 will it not be more better to consider second last element be pi-1 instead of pi ?
In fact, it's the same. Because in your situation, there must be $$$p_{i-2} \gt p_i$$$. So whether it's for $$$p_i$$$ or $$$p_{i-1}$$$, they are both the second to last element. The impact on the subsequent part is the same.
Hi, just wanted to ask how do we arrive at this condition: dpi=max(dpi−2+(i−2)+(i−(i−2)),dpi−1+(i−1)+(i−(i−1))), what does the (i-2) and (i-(i-2)) signify? Thanks
$$$ dp_i $$$ represents the $$$ LDS $$$ ending at the $$$ i-th $$$ index. Now we've two options, either the LDS will have $$$ p_{i-2} $$$ as second last element or $$$ p_{i-1} $$$ as second last element. Let us consider them more generally like this.
$$$ LDS $$$ has $$$ p_j $$$ as second last element. All the $$$LDS$$$ having $$$ p_j $$$ as last element will have an element $$$ p_i \lt p_j $$$ added at the end, hence their length increases. There are $$$ j $$$ such sequences and all get one increment so we get $$$ j $$$ increments in total. Then, all the elements in the range $$$ (j, i] $$$ will have only themselves as $$$LDS$$$ i.e their length would be 1 so a further $$$ i - j $$$ needs to be added. So more generally, we get:
which can be translated to both $$$ i - 2 $$$ and $$$ i - 1 $$$. Note that this is just equivalent to
Ahh okayy got it. Thankss!!
awesome
WOW NICE TUTORIAL, I AM WAITING FOR RATING CHANEGS
The jump between E1 and E2 was huge. But great contest.
I had no clue about B till the very end but got C pretty quicklu. I dont know how so many people got it lol
I found C to be very tricky problem.
Same C was not trivial for me
very bad contest performance for me. Got failed systest on A as missed '='. B I just did some shitty logic and C so much time to do it. and D i just couldn't think the logic calmly.
I also got a panic when I read in the comments about
=case in A, thankfully I had handled it.. will be big shock for everyone who missed that.Amazing E2. Walking from range of min to range of max is quite a cool trick, never thought I can use this trick from MO in CP :P
btw E1 is too easy and standard for Div.2 E, but it's a good hint for E2 :D
Can anybody explain the proof for C in simpler terms.
for array -> [10,5,6,0,0,0,0.......]
To be able to construct this array, it must satisfy the condition: Ai≤min(A1,…,Ai−1)×2−1 for all 1<i<=n
Why is that? Let me explain with the example of the third element (A2=6). The largest value we can achieve for A3 depends on the previous elements. We can add at most 5 in one turn, because adding more than that would affect A1. But we can do better than that by first adding 4, then adding 5 in subsequent turns, which ends up giving us 9 for A3. i think you can understand that now we can make it any elements in range 0 to 2mx-1
Sorry this is a really informal explanation, I'm a newbie too.
Try an arbitrary number like
40, the max you can build to the right of it is+39 then +40 =79. Try5, the largest you can build is4+5=9. For anynto the largest you can go is(n-1) + n. In other wordsnext value < 2*nIts optimal to build left to right, you cant skip values (aka leave it at 0) if you want to build values to its right.
You have to keep track of the minimum allowed as you go left to right. Say
b[0] = 10the limit is now19. Ifb[1] =15it's fine, because its within the limit. But the limit is still tied to10*2 -1 =19, the lower value not15*2 -1 = 29.If we were to try to build anything greater than
19anywhere to the right ofb[0],b[0]would have to increase beyond10first, as we have proved in point 1 the max it can go is2*n -1.very happy to see very less low ranked people in higher ranks.. do we have some new AI protection
Good contest, however I found C to be on easier side this time
is it possible to solve D without the max(pi,p_(i+1))>p_(i+2) condtion?
the dp mentioned here https://mirror.codeforces.com/blog/entry/145042?#comment-1297811
does it fail when the given condition is not met. my solution https://mirror.codeforces.com/contest/2128/submission/331153885
also does something similar and I didn't feel I used that condition.. but not sure.
The state transition uses the condition.
The (i+1) term in the transition state dp[i] = dp[i — 1] + (i + 1) comes from the fact that the new element $$$p_i$$$ is small enough that it can be appended to the LDS of every single subarray p[l...i-1] (for all 0 <= l < i).
I think it's possible using this, but this is way more advanced.
someone might've mentioned, but A is solvable in O(n).
for B, solvable by taking first two, then sorting left and right based on the previous 2 values
the answer for C is a bit confusing, but that might just be me. the solution i found simplest was to keep track of min value and ensure that all after it are strictly less than 2*(min value), which is probably exactly the same as the solution text, but a bit simpler worded for anyone that might be confused like me
How is A solvable in O(n), i don't see it
i solved it in nlogn dont know about o(n) tho
simple, for each number a[1] to a[n] we go from 1 to 31 (k) and find the first power of two where (1 << k) * a[i] > c. we form a separate array where we keep track of how many numbers there are for each k. once we have this, we can simply go through the array and find out how many of the integers can be used for free. for this, a slight complication is the fact that if there are k's where p[k] = 0, which is why we keep track of how many zeroes there are, because they allow us to use more of the next values. in my code, this is accomplished via a variable called leeway. the total time complexity is O(t * n * 31), or O(n) for each test case, since the 31 is a constant. the code isn't bad so you might be able to clear up anything i've explained badly with it.
The time taken for the code to run should be no less than a code with O(n^2) because n <= 30
yeah, for n <= 30. if n went up to 10^9 my code would still run in linear time as opposed to nlogn or n^2 tho
There's a reason n wasn't bounded at 10^9 , the last element that's getting multiplied would be 2^10^9 , that's some crazy size. 2^30 is ~ 10^9 , that's why it's bounded at 30.
did you even read my solution? n is the length of the array, there's no reason to use 1 << n.
WTH!!!! How is A solvable in O(n)???
Auto comment: topic has been updated by bestial-42-centroids (previous revision, new revision, compare).
I can't access the solution codes, pls fix this
A was diabolical in every terms. whoever created it needs to step down . I left the contest cuz of A thinking of an optimised way
You need to chill out. Why would you need an optimised way when n <= 30
well, it exists lol
looking for faster than required is usually bad practice for competitions but in this case you were onto something. keep it up! that mentality is very good
Contest number 999999 to solve a problem using -1, +1 median trick and binary search on answer.
I think E1 is there to hint for the solution towards E2. Certainly without E1 I would've found E2 to be harder.
I felt the same but could not solve E2, LOL.
Yes, the idea of E1 was to make the problem easier by giving a hint. Also, it was there to bridge the gap beween D and E2. Turns out the problem was apparently too standard
my solution for D is takes somewhat a different approach, i solved D with dp with a couple of key observations:
we first handle the lds problem individually for one singular array
a[n], and definedp[i]as the longest lds ending with positioni.observation 1: in standard lds problems,
dp[i]is usually computed from multiple previous statesdp[j]'s wherea[j] > a[i], j < i. however, due to D's unusual constraints, we can prove through contradiction that the only j's that are relevant isi-1andi-2.this observation decreases dp's computing time complexity to
O(n).observation 2: we can also prove (again by contradiction) that
dp[i] >= dp[j]for allj < i. this brings to an important idea: sincelds(1, i)equals to the maximum of dp values from 1 to i,lds(1, i) = dp[i].now, define
f(i)aslds(i, i) + lds(i, i + 1) + ... + lds(i, n). using the observations above, we can calculatef(1)in o(n) time. how can we transitionf(i)tof(i + 1)quickly?we can break it down into two cases:
if
a[i] > a[i + 1], then a[i] is the maximum of the arraya[i...n], which can lead to the fact that alllds(i, k), i <= k <= nstarts witha[i]. so, all lds's are shortened by one, which gives:f(i + 1) = f(i) - (n - i + 1).on the other hand, if
a[i] < a[i + 1]then it can be proven that alllds(i, k), i < k <= n(notice the how its<not<=) does not start witha[i], since withk > i + 2it it always better to choosea[i + 1]as the starting point rather toa[i]. so in this case,f(i + 1) = f(i) - 1.now
f(n)can be calculated inO(n), summing allf(i)gives the answer.total time complexity: O(n).
code: link
The contest was fun! Enjoyed during the contest!
Auto comment: topic has been updated by bestial-42-centroids (previous revision, new revision, compare).
why my code failed for submission (problem a), what's wrong in my code ?
code: ~~~~~
include <bits/stdc++.h>
define ll long long
define all(v) v.begin(), v.end()
using namespace std;
bool check_bool(string str){ int i=0,j=str.size()-1 ; while(i<j){ if(str[i]!=str[j]){ return false ; } i++ ; j-- ; } return true ; } bool check_frq(string str){ map<char,int>mp ; for(int i=0;i<str.size();i++){ mp[str[i]]++ ; } bool ptr= true ; for(auto it=mp.begin();it!=mp.end();it++){ if(it->second%2==0){ continue ; } else { if(ptr==true){ { ptr=false ; continue ;}
}else{ return false ; } }} return true ; } long long gcd(long long a,long long b) { while(b) { long long t=a%b; a=b; b=t; } return a; } ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); } int main() { int t=1; cin>>t ;
while(t--){ int n ; ll c; cin>>n>>c ; vector<ll>vi(n) ; for(int i=0;i<n;i++){ cin>>vi[i] ; } sort(vi.begin(),vi.end()) ; int ans=0 ; int j=-1 ; for(int i=n-1;i>=0;i--){ if(vi[i]<c) { j=i ; break ; } } int ptr=0 ; for(int i=j;i>=0;i--){ ll x=ptr*vi[i]*(ll)2 ; if(x<c){ ptr++ ; ans++ ; } } cout<<n-ans<<endl ; } return 0;} ~~~~~
for each iteration ptr should be ptr=*2 instead of ptr++.
but i'm increasing ptr only when im selecting a value and how late that vales is selected is shown by ptr and at at that second what will be the value of that trash bag is ptr*vi[i]*2 ;
check after selecting three values, the value of the leftover trash bags should be 8 times but in your case it will be 6 times.
nice , thanks for help me out , but according to my code ,i'm doing exponentially. that's why.
Guys, how to notice facts like hint1 in D? Let bi=max(ai,ai+1), show that b1≥b2≥…≥bn−1.
I mean to say, how do I know what to look for?
I have no idea. I feel like editorials do a bad job of capturing thought process. They focus more on proving a solution to be correct than showing people HOW to come up with a solution.
Sorry, I don't really recall how I solved this problem but I can try an explanation.
Perhaps a first remark you could do is that if the condition was that if a[i] >= a[i+1] for every i, then the array would be decreasing and LDS would be the whole sequence.
So in a sense, max(a[i], a[i+1]) >= a[i+2] feels like that the array is "almost decreasing". Now to leverage that, you should get a feel of what "almost decreasing" means, in particular how much it prevents you from increasing. For that it can be helpful to try to generate arrays that respect the condition, and try to "feel the ceiling".
Suppose you start with [50, 30, 40], now after the 40 you see that you must put something less than 40. Let's say you put 39, then after that you still must put something less than 40. You start to feel the ceiling. You wonder if you can increase back to something above 40, but you will quickly realize that's impossible, and that's what will make you realize that a[i] < a[i+1] implies a[i+1] > max(a[i+2], ..., a[n]), which is the hint2 (the hint1 is only useful to prove hint2).
Great thought process.
Nice contest :D
Alternate solution for D:
Let dp[i] be the sum of all the subarrays that ends with the index i.
Set dp[0] = 1. Then, for 1 <= i < n, we have dp[i] = dp[i-1] + (i+1) if p[i] < p[i-1], and dp[i] = dp[i-1] + 1 otherwise. The answer is the sum of all the dp[i]s.
Can you explain why this works?
Why this solution isn't working for problem E1??
What's the reason?
https://mirror.codeforces.com/contest/2128/submission/331201015
You cannot assume that the largest median can only be found in an interval of size k (like your solution calculates). In other words, it may be the case that the largest submedian can be found in an interval size >k.
I calculate for interval of size k and k+1 (One even and odd length interval). I thought all other interval of size >= k+2 can be convert to these two cases.
Edit: Now, I got it. One of case where my solution fails is:
9 61 6 6 1 3 3 4 8 8
Expected Output: 6
My Output: 4
Great contest, thanks :)
ai<x≤min(a1,…,ai−1)≤min(b1,…,bi−1).
Hence, bi=ai+x≤2x−1≤2mi−1.
Can any one help me with how final equation came from it previous step. That is how min(a1,...,ai-1)==2x-1?
Problem E1 is identical to this problem (https://mirror.codeforces.com/problemset/problem/1486/D?locale=ru)
Author is leecher
F was literally useless in this contest. Nice contest overall !
These problems are such that you're just better off guessing something rather than trying to prove a solution. Don't make such problems please.
I really enjoyed E2 and it's a very cool problem. But I think making E1 worth 1750 (same as E2) was too much. It is really demotivating to see my score on E2 being less than my score on B.
Any idea about the rating of B & C?
I can solve E1 very fast because it was same as this problem
https://mirror.codeforces.com/problemset/problem/1486/D
Can someone please explain to me why my code for problem C isnt right?
The code in contest:
The code after the contest (after i change a little bit to make it like the tutorial):
But both are wrong, however.
Can someone please explain to me?
(I put both in void solve() btw)
I know the reason
You return the function solve() before reading all the array a because of this problem is multi test case
if the array have 10 numbers and when you read the 5th number, you found it is incorrect and you return the function. It means that there is still 5 numbers you don't read
Thanks!
Check my solution on D 331161296
I also have a dp solution for D.
Let $$$dp_i$$$ be the sum of LDSs of the segment $$$[i, j]$$$ with $$$i \leq j \lt n$$$ and $$$dp_i = 0$$$ with all $$$i \gt n$$$ and $$$LDS(i, j)$$$ be the LDS for the subarray $$$a_i, a_{i + 1}, ... a_j$$$
Due to the fact that $$$max(p_i, p_{i + 1}) \gt p_{i + 2}$$$, I claim (with no solid proof) that $$$LDS(i, j) = LDS(i + 2, j) + LDS(i, i + 2) - 1$$$ with $$$j \geq i + 2$$$
First, initialize $$$dp_n = 1$$$
Then consider $$$i \lt n$$$
We have 3 cases:
Consider the segment $$$[i, i]$$$. Clearly the LDS is 1. So first $$$dp_i = 1$$$
Then consider the segment $$$[i, i + 1]$$$. The LDS is 2 if $$$a_i \gt a_{i + 1}$$$ and 1 otherwise. So the contribution to $$$dp_i$$$ is either 2 or 1.
Next consider the remaining segments. According to the claim above, $$$LDS(i, j) = LDS(i + 2, j) + LDS(i, i + 2) - 1$$$ with $$$j \geq i + 2$$$. Since $$$LDS(i, i + 2)$$$ is a constant, and we have $$$n - i - 1$$$ subarrays starting from $$$i + 2$$$, we can deduce that the sum of the LDS in this case is $$$(n - i - 1)*[LDS(i, i + 2) - 1] + dp_{i + 2}$$$.
Therefore, my dp formula is as follows:
$$$dp_n = 1$$$
$$$dp_{n - 1} = 1 + LDS(i, i + 1)$$$
$$$dp_i = 1 + LDS(i, i + 1) + [LDS(i, i + 2) - 1]*(n - i - 1) + dp_{i + 2}$$$
Then sum all of the dp values to get the answer.
This runs in $$$O(n)$$$ time
Submission: 331179927
I do not understand how problems like E1 are allowed in Div2 style contests. E2 alone should have been enough.
I agree, because there was a problem same as E1 was in contest.
Because I done it so I can clear the E1 very fast :D
not my dumb ahhh failed system test on A
submission link
Can someone please explain why my O(n^2) solution for C passes?
might be the early breaks or good optimization elsewhere
it gives TLE on T=1
N=2e5
999800001 999800002 999800003 999800004 999800005 999800006 999800007 999800008 999800009 999800010 ........... ......... 1e9
and input is valid i guess
you were lucky
I don't think B is even a real problem.Actually,no matter how you write your program,it'll can be accepted.
I have a quite stupid solution for $$$C$$$
Iterate from $$$2$$$ to $$$n$$$, let the current number be $$$x_i$$$ and $$$mn$$$ be the minimum of all elements before it.
Also, let $$$l$$$ be $$$floor(\frac{x_i - 1}{2})$$$ and $$$r$$$ be $$$x_i - l$$$
Iff $$$max(l, r) \gt mn$$$ then the answer is definitely "NO" and otherwise.
Even I did the same when I solved it from the problem-set. Felt dumb after reading the tutorial :).
After carefully studying your solution section, I feel that there might be a typo in the "It's optimal" part. If ai < a{i+1}, then a{i+1} must be greater than a{i+2}; otherwise, max(ai, a{i+1}) = a{i+1} cannot be greater than a{i+2}.
Can someone suggest some problem that involves considering all possible subarrays, like problem D?
I can't understand problem C at all.... can anyone help?
In prb D edi, shouldn't it be "if ai < ai+1 we cannot have ai+2 > ai+1" ?
I really appreciate problem E2, but I think this test should really have been considered. It's one of the easiest mistakes one can make while sweeping that they set answers for the whole $$$[min, max]$$$ range every time, instead of updating only the answers that are yet to be found.
The hack test $$$n=300\,000$$$, $$$k=2$$$, $$$[1,n-1,2,n-1,2,n-1,...,2,n-1,2,n]$$$ forces the minimum and maximum median to be at the very ends, and every range of even length (except for the very ends) to have $$$[2,n-1]$$$ as the median range. Thus, sweeping is performed for $$$\mathcal{O}(n)$$$ iterations and every second iteration it updates $$$\mathcal{O}(n)$$$ answers, leading to $$$\mathcal{O}(n^2)$$$ time in total.
I tried this test on the 16 accepted submissions from trusted participants, and 2 of them are successfully uphacked.
Thanks, we added your test.
D was an excellent problem and C was easy compare to other div-2 contest
E1 is such a good problem if it was provided in a div3 round, or an edu round.
i have a idea for problem D. We just need to notice that the sequence is a decreasing one. However, there may be some positions where it increases once. It is absolutely impossible for three numbers to be in increasing order. Therefore, it is easy to figure out the subsequent operations by drawing a diagram.
i spent long on A~E1, i didn't solve E2 in the test. But that's really a good problem :)
What a tricky E1, but I like it.
There is a solution to problem D that does not need the statement
max(p_i, p_{i+1}) > p_{i+2}to be true, and it does not have to a be permutation either meaning it will work for any array.Ideas is to calculate for each index i where 0<=i<n, if r = i, what would the answer be and sum of them is the final answer. To calculate it we use idea of Longest Increasing Subsequence in O(n log n), but it is Longest Decreasing Subsequence now.
Overall time complexity is
O(n log n).Link to the submission: https://mirror.codeforces.com/contest/2128/submission/331402684
It appears that the dp approach for problem D is more intuitive to implement, when you try to figure out the relation with a paper.
A was very hard
E1 was very easy
Never mess up with M_Abu_Bakr_Shahid He is my brother
bro you both are the same person, you are acting as if I don't know how to read names, LOL! The stupidity of this decision that you made is so funny!
So why did you hurt me by writing an Afghanistan-Pakistan message
Bro I didn't know that you were such a cry baby, my bad darling!
I am currently sitting next to him watching him doing all this stuff
Some hacker posted bad blogs and I recieved this message Hello, ss250403222_M_Abu_Bakr! Your recent comments/blog posts/other activities have violated rules of our community. It seems this content is nonsensical, troll-like, dirty, coarse, offensive, aggressive, meaningless, violating other rules or ethical norms. Probably, it wasn’t written in English (or Russian if you specify this language for blog post or comment). Please, be polite, reasoned, do not publish junk content, do not violate conventional ethical standards. You should not publish content that does not correspond to the topic of discussion or does not correspond to the topic of the website. Use common sense when analyzing to write or not write any content. You should not publish comic content, especially if it is not interesting to a wide part of the audience, repeats the existing one, or has no connection with competitive programming. Your account has been blocked to write any social content for 48 hours and your recent content has been deleted. Hope you follow the relevant conclusions and this situation will not happen again. In case of repeated violations, more severe penalties may be applied to your account, up to and including blocking.
What a beautiful excuse!
We should not fight if you are a Muslim then you should assume the best from your brothers.
I guess mabubakrshahid2 is also your brother.
This is my solution to problem D. First, consider the sum of the LDS of sub-segments starting with 1. Let f_i be the contribution of sub-segment [1,i].
Since $$$\max(p_i, p_{i+1}) \gt p_{i+2}$$$, transferring from $$$i$$$ and $$$i+1$$$ to $$$i+2$$$ must be non-degenerate.
Next, consider how to transfer from the sum of $$$\text{LDS}$$$ starting with $$$i-1$$$ to the sum of $$$\text{LDS}$$$ starting with $$$i$$$. Let $$$g_i$$$ be the sum of $$$\text{LDS}$$$ starting with $$$i$$$, and initialize $$$g_1=\sum \limits_{i=1}^n f_i$$$.
If $$$a_{i-1} \gt a_i$$$, then for all sub-segments, the transition from $$$i-1$$$ to $$$i$$$ is lost, so $$$g_i=g_{i-1}-(n-i+1)-1$$$. The final subtraction of 1 represents the contribution from the segment $$$[i-1,i-1]$$$, and the same applies below.
Otherwise, there is no loss, and $$$g_i = g_{i-1} + 1$$$.
The answer is $$$\sum \limits_{i=1}^n g_i$$$.
I am unable to view code. Kindly help me out
Dear Codeforces Team,
I am Ankit.Kushwaha, and I recently received a plagiarism warning for problem 2128C from Codeforces Round 1039 (Div. 2), associated with solution ID: 331146482. I respectfully wish to clarify that the submitted solution was written entirely by me, independently, during the contest using Visual Studio Code on my personal machine, without any external assistance or reference.
The approach I implemented involves maintaining the current minimum of the array while verifying that each subsequent element satisfies the condition
arr[i] ≤ 2 * currentMin - 1, with a special check in case the minimum is zero. Given the simplicity and deterministic nature of the logic, it is highly likely that multiple participants may have arrived at similar implementations independently.At no point did I upload or share my solution on any public platforms, nor did I utilize online IDEs such as Ideone or any other tools that could inadvertently expose my code.
I sincerely request a re-evaluation of my case. If any further information or clarification is required, I would be glad to provide it. Thank you for your time and consideration.
Sincerely,
Ankit.Kushwaha
How do you have enough brain cells to cheat but not have enough brain cells to read rules and blog 8790?
E2 was nothing short of brilliant.
Nevertheless, I wouldn't have been able to solve it without the hint from E1
I personally find the editorial for D rather difficult to follow, and I want to suggest some changes.
There should be an assertion that $$$a_i = 0$$$ for all $$$i \gt n$$$.
The sequence $$$(i_1, ..., i_m)$$$ is not an LDS. $$$(a_{i_1}, ..., a_{i_m})$$$ is.
The optimality proof of the LDS is kind of hard to follow, and there's also a typo.
The statement "if $$$a_i \lt a_{i+1}$$$ we cannot have $$$a_{i+2} \lt a_{i+1}$$$" is wrong. It should be corrected to say: "if $$$a_i \lt a_{i+1}$$$ we must have $$$a_{i+2} \lt a_{i+1}$$$".
Here's my interpretation of the optimality proof. Please correct me if I'm mistaken or it can be improved.
Let $$$I$$$ be the index sequence $$$(i_1, ..., i_m)$$$ from earlier, and $$$E$$$ be the index sequence of any LDS of $$$a$$$. Let $$$i$$$ be an arbitrary integer in $$$[1, n]$$$, and consider the following.
In both cases, $$$I$$$ always takes at least the number of elements from $$$E$$$, so $$$|I| \geq |E|$$$.
I know these are subtle changes, but I think they can help clarify some points of the editorial.
I think the editorial for F is incomplete (and I arrived at the solution with this exact incomplete reasoning lol). You don't really show that the condition $$$d_L(u, v) \lt d_R(u, k) + d_R(k, v)$$$ holding for all $$$u, v \in P$$$ suffices for any path $$$P$$$ (you show this for some shortest path $$$P$$$, and make use of properties of shortest paths), which is required as our Djikstra's-like algorithm later isn't really being forced to only consider shortest paths.
The necessary part incorrectly assumed that $$$P$$$ was shortest in $$$w_P$$$, my bad. But we can make things work as follows.
TLDR: instead of looking at $$$d_L(u, v)$$$, look at the length of the subpath of $$$P$$$ going from $$$u$$$ to $$$v$$$.
Let $$$P$$$ a path from $$$1$$$ to $$$n$$$. For every $$$u \in P$$$, let $$$\delta_u$$$ the sum of $$$l$$$-weights of the edges of $$$P$$$ that leads from $$$1$$$ to $$$u$$$. In particular $$$\delta_1 = 0$$$ and $$$\delta_n$$$ is the length of $$$P$$$. As you pointed out, $$$\delta_u$$$ might be larger than $$$d_L(1, u)$$$.
Let's say that $$$P$$$ avoids $$$k$$$ if $$$|\delta_u - \delta_v| \lt d_R(u, k) + d_R(k, v)$$$ for all $$$u, v \in P$$$. This is different from what I said in hint 2.
Let $$$w_P(e) = l(e)$$$ if $$$e \in P$$$ and $$$r(e)$$$ otherwise, and $$$G_P$$$ the corresponding weighted graph.
Claim 1: if there is a valid assignment $$$w$$$ of weights, then there exists a path $$$P$$$ that avoids $$$k$$$.
Just take the shortest path in $$$w$$$
Claim 2: if a path $$$P$$$ avoids $$$k$$$, then $$$w_P$$$ is valid (even if $$$P$$$ is not a shortest path in $$$G_P$$$).
Suppose by contradiction that in $$$G_P$$$, there exists a $$$1-n$$$ shortest path $$$B$$$ (B like Bad) that goes through $$$k$$$. Write
When you write this sequence of nodes, color in black the nodes in P. Let $u_i$ the rightmost black node before $$$k$$$, and $$$v_j$$$ the leftmost black node after $$$k$$$.
Now consider the subarray $$$(u_i, u_{i+1}, \ldots, u_{|u|-1}, k, v_0, v_1, \ldots, v_j)$$$
Since the part of $$$P$$$ that goes from $$$u_i$$$ to $$$v_j$$$ has length $$$|\delta_{u_i} - \delta_{v_j}|$$$, we must have
So $P$ does not avoid $$$k$$$.
Dijkstra algorithm either
In d solution you mean "if ai<ai+1 we cannot have ai+1<ai+2" Instead of "if ai<ai+1 we cannot have ai+2<ai+1" right?