Блог пользователя artew

Автор artew, 3 года назад, По-русски

1935A - Развлечение в ЦПМ

Идея: artew
Разработка: artew
Разбор: artew

Подсказки
Разбор
Решение на C++
Решение на Python

1935B - Информатика в ЦПМ

Идея: AndreyPavlov
Разработка: AndreyPavlov, artew
Разбор: AndreyPavlov

Подсказки
Разбор
Решение на C++
Решение на Python

1935C - Мессенджер в ЦПМ

Идея: artew
Разработка: artew
Разбор: artew

Подсказки
Разбор
Решение на C++
Решение на Python

1935D - Экзамен в ЦПМ

Идея: IzhitskiyTimofey
Разработка: IzhitskiyTimofey
Разбор: IzhitskiyTimofey

Подсказки
Разбор
Решение на C++
Решение на Python

1935E - Дистанционные курсы в ЦПМ

Идея: IzhitskiyTimofey
Разработка: AndreyPavlov
Разбор: AndreyPavlov

Подсказки
Разбор
Решение на C++

1935F - Дерево Андрея

Идея: AndreyPavlov
Разработка: IzhitskiyTimofey
Разбор: IzhitskiyTimofey

Подсказки
Разбор
Решение на C++
Разбор задач Codeforces Round 932 (Div. 2)
  • Проголосовать: нравится
  • +213
  • Проголосовать: не нравится

»
2 года назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Very fast editorial

»
2 года назад, скрыть # |
 
Проголосовать: нравится +91 Проголосовать: не нравится

I think D is easier than C lol. It takes me nearly 50 mins to come up with C, but only 25 mins on D.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +25 Проголосовать: не нравится

C is much harder than D,I even used a segment tree to solve it.

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится +34 Проголосовать: не нравится

Problem C can be solved in $$$O(n \log^2{n})$$$ if we use binary search to find the answer.

UPD: Sorry, I was wrong. Yesterday I had an idea of using binary search and I was sure that would work. I even saw some solutions using binary search, so I did not code it. But today I found out they had different time complexities. My bad.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

IMO C was too hard

»
2 года назад, скрыть # |
 
Проголосовать: нравится +25 Проголосовать: не нравится

Problem E is a fairly interesting and educational problem, I like it!!!

»
2 года назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

Only if I read D before C... xD

»
2 года назад, скрыть # |
Rev. 6  
Проголосовать: нравится +33 Проголосовать: не нравится

Really liked the problem B.

  • The key observation that hit me was "the mex of every subarray will actually be the mex of entire array if answer exists."
Why?
  • The 2nd observation was if answer exists, we only need 2 subarrays.
  • In O(N) we can find the mex of entire array.
  • Then pick first subarray starting from index 1 until you have read all numbers from [0, mx).
  • Then find mex for remaining array, and check if it equals to mex of entire array.
Pseudo Code
»
2 года назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Unbelievably fast editorial PLUS 2 language implementations?! Wow -- what an effort by the organizers, thank you!!!

»
2 года назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

C is quite easy for me but unfortunately forgets the case where x+y and y-x equal s in D :(

»
2 года назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

B was very nice.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

C has DP solution in $$$O(n^2)$$$. 249790851

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    not offending you but what do you expect you are contributing with these comments...most of the people don't understand the code because you know what logic you wrote...it doesn't have comments either. Some understand wrong solutions and concepts get unclear...please if u comment then atleast give a small explanation of ur code

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится +16 Проголосовать: не нравится

We can solve $$$C$$$ with easy $$$O(n^2)$$$ dp solution. First, sort all pairs by $$$b_i$$$ value.

Now, sum of $$$|b_i - b_{i-1}|$$$ is equal to $$$b_{last} - b_{first}$$$ Let's say $$$dp_{len}$$$ is minimum value of sum $$$a_i$$$. Answer is maximum $$$len$$$, if $$$d_{len - 1} + a_i + b_i \le L$$$.

Base is $$$dp_1 = min(a_1 - b_1)$$$

The transition is $$$dp_{len} = min(self, dp_{len - 1} + a_i)$$$

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится -13 Проголосовать: не нравится

    please explain your solution clearly by writing the states then the transitions so that it helps us...these types of comments create in us confusion related to the topics...please answer

    • »
      »
      »
      2 года назад, скрыть # ^ |
      Rev. 3  
      Проголосовать: нравится +13 Проголосовать: не нравится

      I think I explained it good, maybe some pseudo-code will help you. Ask if it is not

      We must choose maximum lenght subsequence such sum of $$$\sum a_i + max(b) - min(b)$$$ is minimum (we want to choose minimum sum, because we want this sum be not greater than $$$L$$$, if minimum sum is greater than $$$L$$$ it means every sum will be greater than $$$L$$$).

      So, let's iteratate in non-decreasing order of values $$$b_i$$$, for $$$len = 1$$$ we store base $$$d_1 = min(d_1, a_i - b_i)$$$ (because $$$b_i$$$ is minimal value, that is, $$$min(b)$$$. And in formula we get $$$\sum a_i + max(b) - min(b)$$$, so current value $$$b_i$$$ will be $$$min(b)$$$ and we must substract it).

      Now transition is simple: choose previous length and try to add new value in it, increasing lenght by $$$1$$$

      sort pairs by b[i] value 
      d[len] = INF
      
      d[1] = a[1] - b[1]
      for(int i = 2; i <= n; i++) {
         for(int len = 1; len <= n; len++ ){
              check if (len + 1) will be answer after we add a[i] b[i] pair in it
         }
         for(int len = n; len >= 2; len-- ){
              d[len] = min(d[len], d[len - 1] + a[i])
         } 
         d[1] = min(d[1], a[i] - b[i])
         
      }
      
      print ans
      

      Upd: my submission during the contest [here]

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Did someone manage to pass a solution with complexity $$$O(n \log A \log n)$$$, where $$$A$$$ is $$$\max(x_i, y_i)$$$, for problem E? My first two solutions with that complexity gave TLE on test 30, but with the TL and the constraints I believe it should be able to pass. Here is my last submission with that complexity, in case anyone is interested: 249817071. I ended up solving it in $$$O(n \log A)$$$ doing the same but with sweepline.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Was n^2log^2 not intended to pass for C? Was going to CM then FST lol.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone help me in problem C? I am trying to upsolve C and looking at the editorial would be my last option. Till now I have understood the problem and have thought of taking input for the message set as an array of pairs, and will try to sort it firstly on the basis of Api and then on the basis of Bpi and then form sets using dp to find the size of messgaes, Now should I move forward or should I change my approach ? Any hints?

»
2 года назад, скрыть # |
 
Проголосовать: нравится -26 Проголосовать: не нравится

I really liked problem E, but I misread C so I didn't have the time to implement it :(.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

My problem E solution(AC after contest) should be $$$O(nlog^3A)$$$ where $$$A$$$ is max grade value (maybe smaller than that, the constant should be really small though). I assumed that for interval that end at $$$r$$$, number of $$$l$$$ that will change the max value is at most $$$log_2(A)$$$, and for each interval $$$[l, r]$$$, the grade that we must consider is also at most $$$log_2(A)$$$ when transition to $$$[l -1, r]$$$.

For those who are interested: 249836964. Feel free to hack!

»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится +21 Проголосовать: не нравится

I have a quite different solution of C. First, sort the array in the non-increasing order of $$$b_i$$$ ($$$b_i \gt = b_{i+1}$$$).

If we can get a set of size $$$k$$$, we can get a size $$$k-1$$$, so we can binary search on the final answer.

Let the final answer be $$$k$$$ and the set is $$$p1 p_2 ... p_k$$$ => $$$a_{p1} + b_{p1} - b_{p2} + a_{p2} + b_{p2} -b_{p3} .... + a_{pk} + p_{bk}$$$.

The final set is $$$a_{p1} + a_{pk} + b_{p1} - b_{pk} + $$$ the sum of the minimum $$$k-2$$$ values of $$$a$$$ of the selected segement which gives $$$a_{p_2} a_{p_3} ... a_{p_{k-1}}$$$ and we can use a priority_queue or multiset to handle it.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why does 0 1 1 array in B return -1

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    Because if you select 0 and 1 1, the mex for first segment is 1, and for the second segment is 0, or if you select 0 1 and 1, the mex on the first segment is 2 and in the second is 0, so again mexs are different.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

Thanks for fast editorial

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem C, when I use a vector I get TLE, while when i use an array it gets accepted. Can anyone explain the reason?

https://codeforces.me/contest/1935/submission/249844469

https://codeforces.me/contest/1935/submission/249843979

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain what's wrong with this idea.

Code

Here dpi1 denotes the maximum number of messages that can be read if ith message is the last one read. dpi0 denotes the minimum time needed to read dpi1 number of messages if ith message is the last one read.

So the answer will be max(dpi1) over all i.

Please someone tell me what's wrong with the above idea/code.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

C has a simpler solution in O(nsq) where the solution before i can be maintiained in just a vector storing better answer upto i-1

Array a is sorted by b. best stores the min val of (ai+aj+ak...)-(bi) for cnt elements to make ans with a new term az+bz need to be added

        vector<ll>best(n+1,INT_MAX);
        best[1]=(a[0].a-a[0].b);
        for(int i=0;i<n;i++){
            for(int cnt=i+1;cnt>=2;cnt--){
                ll prevmin=best[cnt-1];
                ll cans=prevmin+a[i].a;
                best[cnt]=min(best[cnt],cans);
                cans+=a[i].b;
                if(cans<=l){
                    ans=max(ans,cnt);
                }
            }
            best[1]=min(best[1],(a[i].a-a[i].b));
        }
        cout<<ans<<endl;

»
2 года назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

for C, why the check v[r][0] - v[l][0] + cur > L works.

For the case when we pop a message from the heap that belongs to l or r index, shouldn't we change v[r][0] - v[l][0] to v[r - 1][0] - v[l][0] or v[r][0] - v[l + 1][0]

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I messed up C, made it $$$O(n^3)$$$. I didn't realise that for a given start value of $$$b$$$. If an element has to be discarded, it just has to be discarded!
I initially thought of a 2 pointer like thing, but discarded it later, and kind of brute forced without considering this fact.


Aside from that: Seems like the demographics have changed!
I remember getting severe negative deltas at such performances (such as mine today, in terms of rank).

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

time travel editorial

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится +2 Проголосовать: не нравится

Can someone help with Problem C ?
The tutorial's O(N^3logN) solution seems correct to me, but I have some hiccups in accepting the O(N^2logN) solution.

My question is in transitioning for (l, r) to (l, r+1)

  1. Answer for (l, r) would be picking as many smaller a's as possible with sum <= L-(a[l]+a[r]+(b[r]-b[l])), lets say you maintain the set S of best a's that fit this equation.
  2. Now, in the transition into (l, r+1) pair, using a[r] is optional. What if a[r] was too high that caused us to remove some good a's in previous iteration from S that could have been used for the solution of (l, r+1).

I know the solution lies where if a[r] was too high, the multiset would have already removed that first, but then, let's be honest, we are not really calculating the exact answer for (l, r) pair in the first place.

Could someone help with a precise explanation here?

  • »
    »
    2 года назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +3 Проголосовать: не нравится

    In the solution, for the [l,r] interval, we are not maintaining number of smaller a's with sum <= L - (a[l] + a[r] + (b[r] - b[l])).

    Rather, for every interval [l, r], our priority_queue / multiset maintains the list of a's which lie in [l, r], with sum <= L - (b[r] - b[l]). only (which means we do not fix a[l] and a[r] to always be included in the sum).

    Now, 4 cases arise for interval [l, r]:

    1. Both a[l] and a[r] are included in the set of the smallest sum.
    2. a[l] is included but a[r] is not included
    3. a[r] is included but a[l] is not included
    4. both a[l] and a[r] are not included.

    For case 1, we know that this is the best set of a's maintained for [l, r], so we can do mx = max(mx, count) here. For every other case, we have some other interval [l_inner, r_inner] where l_inner and r_inner are indices of the leftmost and the rightmost included a's.

    For every [l, r] interval where case 1 is not applicable, we say that the current count is less than or equal to the count of [l_inner, r_inner] (This is because b[r_inner] - b[l_inner] <= b[r] - b[l] increasing the possible number of items we can pick for the sum within limit).

    So, for any [l, r] where cases 2, 3 and 4 exist, the answer will always be <= answer of [l_inner, r_inner].

    Also, we can see that for the interval [l_inner, r_inner], elements at l_inner and r_inner will be always be chosen in the sum. Since we are looping through all the possible values of l and r, it is ensured that we will calculate [l_inner, r_inner].

    So, this is why we do not need to calculate the exact sum for [l, r], but rather b[r] - b[l] + (sum of a's) <= L.

    Hope this helps and is not too confusing.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone tell me what am I missing in C problem ? I sorted in increasing order of b and found the required time for n messages , then I removed the messages one by one , checking which would reduce the time by maximum .. as soon as this sum was getting less than l , I was printing the current number of messages in the set...

249827661

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For problem F, it can be proved that for any node $$$u$$$, there always exists an optimal set of edges such that there is at most one edge of the form $$$(mx_v, mx_v + 1)$$$, and all other edges are of the form $$$(mn_v - 1, mn_v)$$$ (and possibly one $$$(u - 1, u + 1))$$$.

In fact, we only require an edge of the form $$$(mx_v, mx_v + 1)$$$ when there is some neighbor $$$v$$$ of $$$u$$$ with $$$mn_v = u + 1$$$. In all other cases, it is sufficient to add all the valid $$$(mn_v - 1, mn_v)$$$ edges (and possibly one edge of the form $$$(u - 1, u + 1)$$$) to unite the entire tree.

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

The phase of the coding journey where you are done with the first div 2 question in maximum 5-10 mins and then spend the next two hours coming up with O(n^5) solutions

»
2 года назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится
  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится +5 Проголосовать: не нравится

    Same Solution

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Can you explain what you did?

    • »
      »
      »
      2 года назад, скрыть # ^ |
      Rev. 4  
      Проголосовать: нравится 0 Проголосовать: не нравится

      first of all we will store values in a vector<pair<int,int>> v and sort according to bi. (if you don't know why, ask)

      now, my dp[i][j] has following arguments i and j.

      i: chosen subset will end at i. j: chosen subset will have j values

      and dp[i][j] is the minimum cost of choosing optimal subset such that it ends at i and have j values in it.

      answer will be maximum value of all js such that dp[i][j]<=l.

      now transition: well I lied above dp[i][j] is not the minimum cost of choosing the optimal subset but rather it is dp[i][j] = minimum cost — v[x].second. where x is the index of the first value in the subset.

      and it's not only ending at i its minimum cost for all indexes <=i with values count j —

      transition works as follows dp[i][j] = dp[i-1][j-1] + v[i].first + v[i].second.

      a lot of things happened here dp[i-1][j-1] = minimum cost — v[x].second(explained above) is the amongst all the ending indexes < i, such that we get minimum cost — v[x].second so dp[i][j] = minimum cost + v[i].first + (v[i].first-v[x]) now since the b values are sorted only contribution we will get from b is (v[i].second-v[x].second)

      now we will do dp[i][j]= min(dp[i-1][j], dp[i][j]-v[i].second) (because we want to make it (minimum cost — v[x].second)).

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
        Rev. 3  
        Проголосовать: нравится 0 Проголосовать: не нравится

        what made you choose to sort in accordance with bi

        got it :<)

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Wow...your solution was great. I mean thinking of such states and transitions are great. please share some more prblms like this if you have where the state definition is not trivial and transitions require critical thinking...i Specially liked the part where you are using ur dp array to check if it is <= l...and at the next step making it such that it helps in recalculation of next dp states.

        please share more problems like this

        • »
          »
          »
          »
          »
          2 года назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          this is a common technique where we have to separate f(i) and f(j) if we are dealing with pairs. (here essentially we are dealing with every thing before index j and j) let me give you a trivial example.

          suppose we have been given a array "arr" with some values and we have to calculate no of pairs such that arr[i]*arr[j] = 1 (mod 1e9+7)

          we can just loop in the array store 1/arr[i] (mod 1e9+7) in a hashmap and add and+=mp[arr[i]]; code:

          map<int,int> mp; int ans = 0;

          int inverseMod(int val) for(int i=0;i<n;i++){ ans+=mp[arr[i]]; mp[inverseMod(arr[i])]++; }

          the main idea being used in that dp solution is that only.

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится +3 Проголосовать: не нравится

        upsolving three months later , your explanation is helpful thank you brother

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      well i did not explain it very clearly if you have any doubt just ask me.

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        now we will do dp[i][j]= min(dp[i-1][j], dp[i][j]-v[i].second) (because we want to make it (minimum cost — v[x].second)).

        i don't get it when are you choosing to have v[x].second in dp[i][j] because if choosing ith as optimal then only we should add v[i].second to dp[i][j] so then if it's no optimal it shouldn't be added and shouldn't be subtracted in future

»
2 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Another solution for C using DP and binary search on the solution. https://codeforces.me/contest/1935/submission/249784754

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain why in the editorial of problem C when extracting elements from the multiset the value of v[r].first - v[l].first doesn't get updated? According to the logic, the value of v[r].first - v[l].first will decrease as we remove the greatest element from the multiset.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I solved B using two pointers: 249886727. It works because mex is monotone by inclusion. Imo your editorial code is quite confusing for beginners. I would scan the array forward and backward instead of inventing how to update mex while removing a number.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For the solution of problem 1935C, when iterating over all $$$(l, r)$$$, the messages $$$l$$$ and $$$r$$$ are certainly read. However, when extracting elements from set s (which contains the $$$a$$$ values of the messages), no care is taken that messages $$$l$$$ and $$$r$$$ are not extracted. Can someone explain this?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    If messages $$$l$$$ or $$$r$$$ are removed from the set, we're now calculating the cost incorrectly: the cost we calculate is larger that the true cost, and this might mean that our answer is too small.

    But that is actually never a problem. If $$$l$$$ or $$$r$$$ gets removed when considering the range $$$[l, r]$$$, the range $$$[l, r]$$$ is definitely not optimal ($$$[l, r-1]$$$ or $$$[l+1, r]$$$, depending on which element got removed, would be at least as good as $$$[l, r]$$$). Since we comsider all ranges, including $$$[l, r-1]$$$ and $$$[l+1, r]$$$, we won't miss the optimal solution.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why in c we have taken b max value — b min value? why not first minimum and second minimum?

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

why not in c we have taken 1st and 2nd minimum value of b . why we have taken max and min value?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

B was a very cute problem! Keep it up!

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I am still confused about the formula , $$$\sum_{i = 1} ^ {i = n - 1} |B_i - B_{i+1} |$$$. The formula always gets minimum , as the sequence is ordered. How to proof the formula clearly ???

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    What proof?

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      When the sequence of B is odered, why the formula always get minimum ? I want to the reason.Please teach me ,i am willing to know!

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        I think you misunderstood this. Let's observe formula:

        $$$\sum_{i=1}^{k} a_{p_i} + \sum_{i=1}^{k-1} (b_{p_i} - b_{p_{i-1}})$$$

        Sum of values $$$a_i$$$ does not require any order, we can just sum them.

        What about sum of differences of $$$b_i$$$, of course, we want to minimize this sum (so that sum is not greater than $$$L$$$)

        Now, we can forget about values $$$a_i$$$ (their sum does not depend on order we choose values). But if we got values $$$b_i$$$, it's always optimal to sort them. For example, $$$b = [1,6,1,5]$$$ gives sum $$$6-1 + 6 - 1 + 5 - 1 = 14$$$, but $$$b=[1,1,5,6]$$$ gives sum $$$1-1 + 5 - 1 + 6 - 5 = 5$$$. Why? You can see abs function $$$|x-y|$$$ as distance beetween two points $$$x,y$$$ on straight line. It's easy to see, that sorting gets the minimum sum.

        Now, we can sort values $$$(a_i, b_i)$$$ by $$$b_i$$$ and choose sum values $$$b$$$ to our answer. Let's closer look at $$$b_j - b_{j-1}$$$, it's equal to $$$b_2 - b_1 + b_3 - b_2 + b_4 - b_3 + ... + b_{k} - b_{k-1}$$$, there are pairs $$$b_2 - b_2 + b_3 - b_3$$$ etc for all $$$i$$$ from $$$2$$$ to $$$k-1$$$, and now this sum equal to $$$max(b) - min(b)$$$

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

You can solve A without n at all. You just need to find the minimum lexicographic string between the regular string, the inverted string + the usual string, the usual string + the inverted string

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in second how to prove this that if we can not separate array into two segments then it is also not possible for us to do it if the number of segments are increased ?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    If there is answer in such $$$k$$$ segments we can always reduce it into $$$2$$$ segments. So, it is not possible to find answer in $$$2$$$ segments, we won't find it in any bigger segments (if there is answer in bigger number of segments, we would reduce it into $$$2$$$ segments and found it there).

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится +5 Проголосовать: не нравится

    Let us assume for array $$$a$$$, we have a valid partition into $$$k$$$ subsegments $$$S_1, S_2, ... S_k$$$ each having $$$mex(S_j) = m$$$. If we consider merging subsegments $$$S_i$$$ and $$$S_{i+1}$$$ ($$$i \lt k$$$), the $$$mex$$$ of the new subsegment would also be $$$m$$$ (as it too would contain all elements from $$$0$$$ to $$$m-1$$$ but would not feature $$$m$$$). Continuing this process will lead us to partition array $$$a$$$ into any number of subsegments less than $$$k$$$.

    We can conclude that if there is a valid partition of $$$a$$$ into $$$k$$$ subsegments, a valid partition into $$$x ( \lt k)$$$ subsegments must exist. As a corollary, if $$$a$$$ cannot be partitioned into $$$x$$$ segments, it cannot be partitioned into $$$k ( \gt x)$$$ segments. This would prove your argument for $$$x = 2$$$.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I solved C using DP and lazy propagation (I know overkill). Basically $$$dp[i][j]$$$ is the minimum cost to get $$$j$$$ elements in the range $$$[i,n]$$$. Let's sort the elements by $$$b[i]]$$$. In that case $$$dp[i][j] = \min_{k=i+1,k=i+2,...k=n+1} b[k] -b[i] + a[i]$$$. This so far is $$$O(n^3)$$$. We can optimize it by making a segment tree that supports lazy propagation for each $$$j$$$, however that gets MLE. If we iterate over $$$j$$$ we notice that we only care about $$$j-1$$$ so we can use only 2 segment trees. Why do we need lazy? When go from $$$i+1$$$ to $$$i$$$ , the difference between $$$b[i]$$$ and all $$$b[j]$$$ such that $$$j \gt i$$$ will increase by $$$b[i+1]-b[i]$$$ so we can update them using lazy propagation. The answer is the maximum $$$j$$$ such that there is some $$$dp[i][j] \le l$$$, thus the solution is $$$O(n^2logn)$$$. Code: 249828732

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help me on why I'm getting munmap_chunk() RTE on pretest 1 for Problem C? If I run each test case on the 1st pretest one by one, then there is no RTE! Submission: 249970089

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone help me with B i have the same idea as editorial but i dont know why iam getting WA here is my code: 249976418

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in problem C, when i remove the max element multiset, if i use erase, i WA but i use extract, it AC. Why Ex: mst.erase(mx); (WA) mst.extract(mx); (AC) but when i use extract, my complier ERROR

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In the author's solution to problem C, can anyone please explain how it is ensured that the end points v[l].second and v[r].second are covered in the multiset and also in the sum $$$cur$$$ (as we are simply removing values greedily)?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

There is a nice dp way to solve the problem C. After sorting the pairs according to b[i]. lets dp[pos][len][2] be the state. Here pos means any prefix of array dp[pos][len][0] refers minimum possible total cost of any subsequence of length len of the prefix pos and we have stopped taking new value here. and dp[pos][len][1]refers minimum possible total cost of any subsequence of length len of the prefix pos and we will take one or more element to the choosen subsequence. here is my solution link : https://codeforces.me/contest/1935/submission/249842011

»
2 года назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

You don't have to use DSU or binary search machinery in problem F. You can simply join components as follows:

  1. For each component $$$u$$$ with $$$v \lt mx_u \lt n$$$, join $$$mx_u$$$ to $$$mx_u+1$$$. This joins all components extending above $$$v$$$.
  2. For each component $$$u$$$ with $$$1 \lt mn_u \lt v$$$ that doesn't extend above $$$v$$$, join $$$mn_u$$$ to $$$mn_u-1$$$. Also, if there are any components $$$w$$$ with $$$mn_w \lt v \lt mx_w$$$, join $$$mn_w$$$ to $$$mn_w-1$$$ for such a component $$$w$$$ which has the smallest value of $$$mn_w$$$, unless this value is $$$1$$$. You can reason inductively to see that this joins all components which extend below $$$v$$$, either via the edges added in this step or via the edges added in step 1.
  3. This joins everything unless you need to add the edge $$$(v-1,v+1)$$$, in which case, add it.
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For Problem C, why are we sorting the values based on the value of b? Why don't we sort the values according to the value of A?

»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

For problem E, if n = 2 and y1 = 5 and y2 = 9 and let's consider cases when x1 = 0 and x2 = 0. If we look at the highest bit = 3, then this bit appears twice in numbers <= y2 [number 8 (1000) and 9 (1001)]. I don't think we can set all bits less than 3 in the answer. You can only pick one number out of the two. Here we choose 9. And in order to set all bits to 1 we will have to choose 6 (110) from other number which is not possible. Can somebody rephrase what the editorial is trying to say?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    "Suppose we are iterating over bit $$$i$$$, then if such a bit occurs $$$c$$$ times in $$$y$$$ numbers there are several cases:"

    This $$$c$$$ means the number of times that a bit occur in $$$y$$$, not in $$$[0, y]$$$. In your example, when you have $$$y_1 = 5$$$ and $$$y_2 = 9$$$, when we are looking for the highest bit = $$$3$$$, then $$$c = 1$$$, so you put the bit $$$3$$$ in the answer. After that, the next bit is $$$2$$$, that appears once in $$$5$$$, so you put the bit $$$2$$$ in res, then, bit $$$1$$$ doesn't appears in any of them, and bit $$$0$$$ appears both in $$$5$$$ and $$$9$$$, so, the answer is $$$(1101)_2$$$.

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Ok, I got confused in the language.

      Another thing is that after removing the values of $$$w_i$$$ we are ignoring the values of $$$x^{'}_i$$$ and only considering $$$y^{'}_i$$$. What allows us to do that? I believe that editorial does not explain that clearly.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Hey, in case any of you are looking for a slightly detailed editorial on Problems B to E, here's my attempt: https://www.youtube.com/watch?v=TrnshSV0qy0&t=5061s&ab_channel=DecipherAlgorithmswithSaptarshi

Btw I got a great comment seeking solution of C in O(n^2) (that I didn't care thinking myself because my O(n^2 * log(n)) worked during contest. But then, putting a thought, I could see that the a very common DP approach — direct Knapsack — would work in O(n^2). I'll consider uploading one video on that, as we all know how much DP knowledge is valuable in these contests.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

e done using 2d dp :- https://codeforces.me/contest/1935/submission/250851075

clean implementation

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Sample solution for 1935C - Messenger in MAC should get WA due to integer overflow, but passes tests...

Hack data:

1
2 1000000000
1000000000 1
1000000000 1000000000

Answer is 1 but prints 2.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

sorry for necroposting but i want to share my solution for F. it is actually linear. i do everything very similar to what the editorial does but at the end for a fixed vertex $$$v$$$ i solve the problem in $$$O(\deg_v)$$$ time rather than $$$O(\deg_v \log n)$$$. For every neighbour $$$u$$$ of $$$v$$$, if $$$1 \lt mn_u \lt v$$$, i will add an edge $$$(mn_u - 1, mn_u)$$$. for every $$$u$$$ with $$$v \lt mn_u \le mx_u \lt n$$$, i will add an edge $$$(mx_u, mx_u + 1)$$$. if after this process i already have $$$\deg_v - 1$$$ edges, i am done. otherwise, let's find $$$u$$$ such that $$$mn_u \lt v$$$ and $$$mx_u$$$ is maximized. i add the edge $$$(mx_u, \max(v + 1, mx_u + 1))$$$. i will leave it as an exercise for the reader to see why this algorithm is valid. here is my code: 347589533