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

Автор vovuh, история, 6 лет назад, перевод, По-русски

Все идеи принадлежат MikeMirzayanov.

1385A - Три попарных максимума

Разбор
Решение

1385B - Восстановление перестановки после слияния

Разбор
Решение

1385C - Сделай хорошо

Разбор
Решение

1385D - а-хорошая строка

Разбор
Решение

1385E - Ориентация ребер

Разбор
Решение

1385F - Удаление листьев

Разбор
Решение

1385G - Перевороты столбцов

Разбор
Решение
Разбор задач Codeforces Round 656 (Div. 3)
  • Проголосовать: нравится
  • +157
  • Проголосовать: не нравится

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

vovuh There's a typo in idea of E "Otherwise, the answer is always "YES".... Its not always YES.

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

Great contest with amazing set of questions, just a bit inclined towards DIV 2 rather than DIV 3.

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

Is it first time that ratings have been given to problems before the main testing ??

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

Great contest indeed and great editorial . . . learnt new things in this contest. . . Make more of these. Thanks

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

Was this test rated, if it was how much time will it take to update the ratings ?

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

https://mirror.codeforces.com/contest/1385/submission/87162089 can someone explain what is the error in this code for problem E ? I can't understand the diagnostics message

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

Anyone else used binary search for C?87114284

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

    i did it

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

      I still didn't get the Binary Search approach, can you explain? Thanks

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

        let's say $$$P(x)$$$ tells us whether by erasing the prefix of length $$$x$$$ the array becomes good (this is the subarray $$$[x+1\dots n]$$$). The key is that if $$$P(x) = true$$$, then $$$P(x+1) = true$$$. So, apply binary search to find the lowest $$$x$$$ such that $$$P(x) = true$$$. Now for a fixed $$$x$$$, to know if the array becomes good we do following:

        Let's say $$$L[i]$$$ tells us whether we can put all elements from left side of $$$i$$$ before $$$i$$$ in the array $$$C[]$$$ or not.

        $$$L[i] = \begin{cases}true & \ \text{ if } i = x+1\\L[i-1] \ \& \ A[i-1] \lt A[i] & \text{ if } i \gt x+1 \end{cases}$$$

        Let's say $R[i]$ tells us whether we can put all elements from right side of $$$i$$$ before $$$i$$$ in the array $$$C[]$$$ or not.

        $$$R[i] = \begin{cases}true & \ \text{ if } i = n\\R[i+1] \ \& \ A[i+1] \lt A[i] & \text{ if } i \lt n \end{cases}$$$

        Now, if there exists some $i \in [x+1\dots n]: L[i] = R[i] = true$, then $$$P(x) = true$$$; otherwise $$$P(x) = false$$$.

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

    Me too

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

    I also did it

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

How to solve D in bottom up manner?? And/or Using iterative only??

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

    Look at this submission. Here $$$dp[i][j]$$$ is the minimum cost of make good the range starting at position $$$i$$$ having length $$$2^j$$$. The transitions are the same as recursive approach.

    $$$dp[i][j] = \min(dp[i][j-1] + \text{cost}(i + 2^{j-1}, i + 2^j-1), dp[i+2^{j-1}][j-1] + \text{cost}(i, i + 2^{j-1}-1))$$$

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

      Thanks for the reply. But I can't understand fully. Can you please explain how you are finding which character should come in the range I to 2^j-1 and what dp[0][I] means? And what does the j-i+1 in the cost function mean?

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

        Let's say $$$k=\log_2 n$$$. Then, if the range is of length $$$2^p$$$, the character here must be $$$\text{'a'}+k-p-1$$$, cause each time we half the length of range, the character augments in 1. Now, the cost at some range is the amount of characters on the range that are different of the right character.

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

For 1385E - Directing Edges, an alternative solution can be as described below with DFS, DSU.

Strategy: Creating edge from SMALLER value node to BIGGER value node

Observation: If the directed edges make any cycle, then there is no answer. Else, there is always an answer.

Solution Construction:

  1. Make dsu components from directed edge list
  2. If u and v both didn't participate in any directed edge, then make an edge from min(u,v) to max(u,v)
  3. If u is a normal node and v is a node that participated in a direct edge, then always make edge from u to v

  4. If u and v both participated in directed edges and both belong to separate dsu component, then make an edge u->v if root(u) < root(v), else v->u

  5. If u and v both participated in directed edges and both belong to same dsu component, then make an edge u->v or v->u in such a way that no cycle occurs.

Solution: 87185374

(This strategy essentially behaves like the topsort solution from editorial. The editorial's cpp code's equivalent strategy is such that it tries to make edge from bigger value node to smaller value node)

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

I think problem D can add a modify operation — let $$$s_i\gets c$$$ ($$$c$$$ is a character).

Then we can use a segment tree to solve it.

Actually, the calc function in problem D's code is just like a pushup function in a segment tree.

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

i am still having difficulty for understanding complexity of prob D depth of recursion is log n

there are n possibilities if i am not wrong

e.g. aaaabbcd aaaabbdc aaaacdbb aaaadcbb the rest four with aaaa at the end... like we try all n possiblities but doesn the editorial do an additional step of counting at each step?

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

vovuh For problem F, the editorial states: We can notice that all leaves are indistinguishable for us. I think this is correct conceptually but do not know how to prove it. Can you share how you come up with this fact? When I first read this problem, I was thinking about the order of choosing different leaves to remove is going to have an impact on the final answer. Obviously this is not true.

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

I cant understand why problem E's solution needs to --x; --y;? Hoping for reply:)

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

https://mirror.codeforces.com/contest/1385/submission/87187974

anyone ? this python code is getting runtime error on 27th test (last one). i am new to python and have no idea why is this happening.

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

Do we need to memoize in problem D i passed it using simple recursion.

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

Can anyone help me for Problem D. I am getting TLE for Test Case 3. I have followed the approach similar to that given in editorial 87191656

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

Shouldn't the editorial be the same as the tag given, for example, Problem C has the tag Binary Search whereas the explanation is pure implementation. I mean explain the approach that requires algorithms rather than intuitive ones.

Edit — Same goes for D no explanation for the DP approach

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

Editorial Answer is very good for C. But i implemented it using binary search.

If we think this question as a binary search question then according to me it is a very good binary search question.

Thats why i thought to share my idea.

If we consider f(x) as a boolean function that tells whether the array is good after xth index,then obviously f(x) will become a monotonic function by intution. As by intution we can see that if we take x1>x then for every x1 the array is good. So just find minimal value for x so that the array is good after index x and finally your answer will be n-(total elements in good array).

Hope this solution helps someone.

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

What problems should I practice to solve D easily ? Any specific concepts ?

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

In problem E, is this a standard way of checking if there's a cycle in the directed graph? I did a quick google search and couldn't find such implementation.

I usually go about coloring the vertices and check if I revisit a vertex in current traversal.

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

I think D can be further optimised to O(N) by making a prefix array that stores count of a,b,c....z in any prefix. In this way the time of count function will drastically reduce to O(1). And final complexity will become exponential(logn) i.e 2^(logn) which is almost equivalent to O(n).

This way count(l,r,c) can be written as pre[r][c]-pre[l-1][c]

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

Did anyone solve G by 2-SAT ??

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

IN ques 1385C — Make It Good
eg .
1
4
1 3 2 4
soln:
step 1 : 3 2 4 c=[1]
step 2 : 2 4 c=[1 , 3]
step 3 : 2 c =[1 , 3 , 4]

the ans is 1 but A/C to this ques ans is 2 how?? please any one hep

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

Why am I getting a TLE in D when I have used the same logic, and the code has same time complexity as mentioned in the tutorial?

It would be really kind of any one of you if you could help me out

My Submission : https://mirror.codeforces.com/contest/1385/submission/87198924

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

How to solve problem F if we can remove any k leaves at a time(i.e not only the leaves from a particular nodes but we can pick leaves from any node)?

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

why E's solution code will get MLE at test1 using Clang++17 while AC on g++17

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

Need help. I am getting TLE on testcase 3 for the problem D. I have tried similar approach but i am not sure why i am getting TLE. MySolution. Any help would be appreciated.

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

https://mirror.codeforces.com/contest/1385/submission/87155412

Can anyone Please explain why i am getting tle on test case 3 in question D? I have used recursion.

https://mirror.codeforces.com/contest/1385/submission/87198479

in the above submission, I just write both the parts separately which were included in minimum in the very first submission and this time I did not get the tle but I am unable to understand what was the problem

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

Problem D — Not able to understand what is causing TLE https://mirror.codeforces.com/contest/1385/submission/87200081

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

Anyone else solved E using analysis of DFS coloring states? (0 for not seen, 1 for discovered, 2 for visited)

My Solution:

Maintain a global vector of pairs for all the edges in the final solution

For every node, sort its edges in the following manner (directed edges comes before undirected)

Do a DFS starting from any node.

Color current node 1

While considering its neighbors:

A)If directed: (Standard methods used for detecting a cycle)

1) Check if the next node is colored 1; That implies a back edge is present, thus cycle formed

2) If the next node is colored 0, then recursively call DFS on it, push {current node, next node} as pair in edges

3) If the next node is colored 2, push {current node, next node} as pair in edges

B)If undirected:

1) Check if the next node is colored 1; implies a back edge would exist if {current node, next node} is considered an edge, thus convert it into a forward edge and push back {next node, current node} as an edge

2) Check if the next node is colored 0 (I might get a little sloppy with my explanation for this, so please bear with me :D)

Now I choose to always select {next node, current node} as my edge in this case due to the following:

Lemma: If I visit all the directed edges before undirected edges for a node, then conversion of all undirected edges to incoming directed edges for the current node {next node, current node}, will never result in the formation of a cycle

Proof:

If all undirected edges are converted into incoming edges. A cycle consisting of any pair these edges and the current node wouldn't be formed (as both edges are in the same direction). The same applies to all incoming directed edges.

Now for all outgoing directed edges, as per our sorting before we would have already visited these directed edges before encountering any undirected edge for our current node. Let's assume that along the path of the above outgoing directed edge, we reach a node 'u' and encounter an undirected edge linked to node 'v', which we convert into an incoming directed edge ('v' -> 'u'). Now if a cycle were to be formed using 'v', 'u', 'current node' and 'next node', then it wouldn't be possible as both of them are incoming edges ('v' -> 'u') & ('next node' -> 'current node').

Thus for all next nodes colored 0, we will choose the {next node, current node} as an edge.

3) If the next node is colored 2, we do nothing as we established above, it would already would have been treated as an edge {current node, next node} while we observed DFS for the next node (As its already been visited (colored 2)).

After visiting its neighbors, color the current node as 2 (visited and explored).

Finish DFS. Finally print edges.

My Solution: (https://mirror.codeforces.com/contest/1385/submission/87158004)

(Here I created a map of the managed undirected edges so as to only treat the edge for only one node, that implies in case of undirected edges, we wouldn't need to check if the next node is colored 2 as if it would, we would have already skipped past it, thus without checking for anything we can treat the undirected edge as an incoming directed edge)

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

You can do F using Tree DP in 2 DFSes in $$$\mathcal O (n)$$$ time.

Arbitrarily root the tree.

Let $$$p(u)$$$ be $$$u$$$'s parent and $$$C(u)$$$ be the set of $$$u$$$'s children.
Let $$$\operatorname{in}(u)$$$ be the max number of moves we can do if we only consider the subtree of $$$u$$$.
Let $$$\operatorname{out}(u)$$$ be the max number of moves we can do if we remove the subtree of $$$u$$$.
Let $$$\operatorname{can}_i(u)$$$ be $$$1$$$ if we can remove all the nodes from the subtree of $$$u$$$, $$$0$$$ otherwise.
Let $$$\operatorname{can}_o(u)$$$ be $$$1$$$ if after deleting the subtree of $$$u$$$, we can remove all nodes except $$$u$$$'s parent, $$$0$$$ otherwise.
Let $$$\operatorname{cnt}_i(u)$$$ be the number of children $$$v$$$ of $$$u$$$ which we can turn into a leaf if we restrict ourselves to subtree of $$$v$$$.
Let $$$\operatorname{cnt}_o(u)$$$ be the number of nodes connected to $$$p(u)$$$ which we can turn into leaf after deleting the subtree of $$$u$$$.

Now, $$$\operatorname{in}(u) = \sum_{v \in C(u)} \operatorname{in}(v) + \left\lfloor \frac{\operatorname{cnt}_i(u)}k\right\rfloor$$$; $$$\operatorname{can}_i(u) = 1$$$ iff $$$\operatorname{cnt}_i(u) = \left\lvert C(u)\right\rvert$$$ and $$$k \mid \operatorname{cnt}_i(u)$$$; $$$\operatorname{cnt}_i(u) = \sum_{v \in C(u)} \operatorname{can}_i(u)$$$.
We can compute this in the first DFS.

$$$\operatorname{out}(u) = \operatorname{out}(p(u)) + \operatorname{in}(p(u)) - \operatorname{in}(u) - \left\lfloor \frac{\operatorname{cnt}_i(p(u))}k\right\rfloor + \left\lfloor \frac{\operatorname{cnt}_o(u)}{k}\right\rfloor$$$; $$$\operatorname{cnt}_o(u) = \operatorname{cnt}_i(p(u)) - \operatorname{can}_i(u) + \operatorname{can}_o(p(u))$$$; $$$\operatorname{can}_o(u)$$$ is $$$1$$$ iff $$$\operatorname{cnt}_o(u) + 1 = \deg(p(u))$$$ and $$$k\mid \operatorname{cnt}_o(u)$$$ which we can compute in the second DFS.

Now the answer will be $$$\max_{u}\left(\operatorname{in}(u) + \operatorname{out}(u) - \left\lfloor\frac{\operatorname{cnt}_i(u)}{k}\right\rfloor + \left\lfloor\frac{\operatorname{cnt}_i(u) + \operatorname{can}_o(u)}{k}\right\rfloor\right)$$$.

Code

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

Can you someone tell me why am I getting TLE for the 4th question (a-good string)? My solution is very similar to the editorial. Here's my submission Your text to link here...

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

My video editorial for C and E.

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

for Q.3- Make it good, can someone tell why this is an incorrect logic? Looking for a counterexample.

for(int i=1;i<=n-2;i++) { if(a[i]<a[i-1] && a[i]<a[i+1]) { c=i; }

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

https://mirror.codeforces.com/contest/1385/submission/87201950 can anyone explain why my code giving tle for problem D. it is similar to editorial solution.

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

Can anyone tell what is this code wrong for solution of Problem B? I iterate from left to right and take all integers in permutation1 until the first integer of permutation1 appears again. Then I do the same for second interger. But it throws wrong ans.

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

I am WA on test case 2 for E. I have used the same topo sort approach. Where am I getting wrong? 87207905

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

Detailed editorials with appropriate proofs and implementations on my YT channel

Editorial for problem C: https://youtu.be/vjRHaZb16bU
Editorial for problem D: https://youtu.be/9gVpnosPKec
Editorial for problem E: https://youtu.be/yn6ZPaqwlso

Enjoy watching!!

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

If anyone need, Detail Explanation(not a video tutorial) for D Here

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

If anyone is interested in rerooting and DP on trees method to solve Problem F: here is my submission :) 87210583

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

    Please explain it as well.

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

      Sure ^_^

      TL;DR: if you have calculated the maximum number of moves for the tree rooted at a vertex, find a formula to calculate it for its children.

      Let's notice, first of all, that if we choose a vertex $$$v$$$ and consider it as a root, then we can calculate the answer for that vertex: that is, we can find the maximum number of moves if at each move we remove $$$k$$$ leaves such that each leaf was a child of it's adjacent vertex for the graph rooted at vertex $$$v$$$. A point to note: in particular, this means that we never remove vertex $$$v$$$, as it never becomes a child of another vertex, as it is the root itself.

      Now another lemma: for a fixed rooted tree, the order of operations is not important. As long as there are leaves in the rooted tree (note again, that we don't count the root as a leaf), we can keep removing them, and any sequence of moves leads to the same number of moves.

      So for a given vertex, we can calculate this by DFS. Let's calculate it for vertex $$$1$$$.

      But we also maintain a map: $$$map \lt pair \lt int, int \gt , bool \gt ok$$$, which I define as $$$map[{v, par}]=true$$$ if in the tree, if $$$v$$$ is child of $$$par$$$, can we remove all children of $$$v$$$, hence making it possible for $$$v$$$ to be removed from $$$par$$$.

      We also store $$$count[v]$$$: number of direct children of $$$v$$$, each denoted by $$$u_i$$$, such that $$$ok[{u_i, v}]=1$$$

      Now let's say we have calculated the maximum number of moves $$$moves[v]$$$ in rooted tree $$$v$$$. How to do calculate $$$moves[u]$$$ for each child $$$u$$$ of $$$v$$$?

      Here's where re-rooting comes in. We have the recurrence relation

      $$$moves[v]=moves[par]-count[par]/k+(count[par]-ok[{v, par}])/k-c/k+(c+ok[{par, v}])/k$$$ where $$$c$$$ is the number of vertices adjoint to $$$v$$$, except $$$par$$$, such that $$$ok[{c, v}]=1$$$.

      The formula may look a bit involved but it is really intuitive so I recommend you to come up and prove it yourself. Lemme know if you have any doubts :)

      The answer is, of-course, the maximum over all $$$moves[v]$$$

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

Can anyone help me out with this solution for C — Make it good? Link to solution. It is giving correct solution on test 2 on my local machine, but wrong answer on the site. I changed Language from GNU C++14 to GNU C++17(64), it passed a few more test cases but again it gave Wrong answer. Can anyone tell me why this is happening? Thank you.

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

de

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

Can someone explain why this wont pass third test case for problem B? 87097426

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

somebody please explain me the tc 3 of Problem D :: 8 bbaaddcc

how the ans is 5.

Thanks in adv

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

В задаче Е опечатка в разборе. В Третьем слове вместо «если» написали «ели»

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

Can i get a author's testcase, which doesn't fit completely in CodeForces tasks set of testcases? (for example, the 2nd testcase from task F)

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

    I can give you the generator's code. Note that you need to have testlib library locally to compile it.

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

      Thank you I just have some trouble with this line in solution:

      if (leaves[a].size() == leaves[b].size()) return a < b;

      I don't understand, why code without this line gives WA and with this line gives AC

      Can you explain?

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

        If two vertices have the same size of $$$leaves_v$$$ and you don't compare them by some other metric, it's undefined behavior. Standard set can't handle it (because can't compare which one is less than other) and you don't know what it will return, so it can lead to RE or WA easily. This is a pretty standard bug.

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

Is rating updated ??
I participated the contest but then I had to go somewhere else, so i leaved the contest without single submission.
So is this some kind of feature of codeforces to not involve registered but not participated contestant in rating changes?

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

Can someone explain how the complexity of the 4 th solution is (nlog(n)) ?

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

For 1385F - Removing Leaves, there exists a O(n) solution based on rerooting of trees concept.

Check this out : 87161106

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

i am kinda having hard time understanding B . can anyone plz explain the problem and tutorial to me . thanx in adv.!!!!!

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

    In B you are given a permutation of length n. Now the question is saying that they are merging the permutation of n with itself. So now you just have to find the original order of the permutation. Merging is done in such a way that the relative order is maintained in the merged permutation.

    Like for e.g- original permutation is 5 3 2 4 1 and the merged permutation is 5 3 5 2 4 3 2 1 4 1

    the answer to the question is simply 5 3 2 4 1 and you can see that the relative order is not distorted 3 if always coming after 5, 2 after 3 and soon.

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

Is there any iterative way to solve problem D?

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

using unordered_set in Problem B. gives WA. why?

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

Hi Guys, I couldn't solve question E during the contest.So I thought to give it a try after reading the editorial. I have implemented recursive method of topological sorting before , so i tried to do it by iterative method, but somehow i couldn't get the logic right .

This is the case on which my code fails 3 3 1 1 2 1 1 3 1 3 2

Here is the my code : https://mirror.codeforces.com/contest/1385/submission/87232399

Can somebody help.

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

IN Codeforces Round #656 (Div. 3) Question C If I use the logic: grp=2; if(n>2) for(int i=n-3;i>=0;i--) { if(a[i]>a[i-1]&&a[i-1]<a[i-2]) break; grp+=1; } cout<<grp<<endl;

How is the logic wrong?? Can anyone give me a corner case?

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

For G, after the contest, I found a different, possibly simpler, greedy algorithm.

  1. Start with an empty set of swaps (the result)
  2. Work out how many instances there are of each value in each row, and where they are.
  3. For each value in [1, n]:
    1. If there aren't a total of precisely two instances fail
    2. If there is one instance in each row then we are done
    3. If there are two instances in one then we can fix this by swapping either of the columns it occurs in. Swapping a column, however, will either cause a new value to be duplicated, or fix a duplication. If a new value is duplicated then we need to do a further swap to fix this. For each initial column work out the chain of swaps that will result.
    4. Work out the cost of each chain. The cost isn't simply the length, since some swaps will reverse swaps done in fixing previous duplications, in which case they reduce rather than increasing the cost.
    5. Take the cheaper chain, and apply all swaps in it to the rows. For each swap in the chain, if it wasn't previously in the result then add it. If it was then remove it.

I haven't proved that this always gives the best result, or worked out its theoretical performance, but my Python implementation of this passed all the tests well within the time limit.

See https://mirror.codeforces.com/contest/1385/submission/87241176

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

87210508

I seem to be getting an error for my solution of Problem F on some test of testcase 19 and can't seem to figure out where the error is. Can someone here help me out ? Thanks in advance

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

https://mirror.codeforces.com/contest/1385/submission/87250962

Please anyone explain me why my code of question 4 is resulting in TLE on testcase 3.

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

vovuh Both solutions are the same while one is giving the wrong answer for problem F. Wrong Answer Accepted

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

    Those solutions are not exactly the same:

    In your WA solution, you call leaves[v].erase(leaf) and then s.erase(v)

    In your AC solution, you call s.erase(v) before you call leaves[v].erase(leaf), and since v points to s.begin(), the operation leaves[v].erase(leaf) will use a different value of v in your AC solution.

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

      I don't get what difference does it make changing the order of execution of leaves[v].erase(leaf)

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

        v is a pointer to the first value of s (s.begin()). If v was defined as v=s.begin(), rather than v=*s.begin(), then there would be no difference between the two solutions. But since v points to s.begin(), when you remove v from s, then call leaves[v].erase(leaf), the value of v here is different from the value of v in the previous line

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

Hello! For problem C, I saw 'binary search' as a problem tag. If anyone here is able to produce a solution as to how one uses binary search to solve the problem, I will greatly appreciate it.

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

Problem E was easily searchable, I think. A quick search on how to convert undirected graph to directed acyclic graph got me this result.

https://stackoverflow.com/q/45752331/7121746

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

Can somebody elaborate on this part in F please:

				int leaf = *leaves[v].begin();
				g[leaf].erase(v);
				g[v].erase(leaf);
				st.erase(v);
				st.erase(leaf);
				leaves[v].erase(leaf);
				if (leaves[leaf].count(v)) leaves[leaf].erase(v);
				if (g[v].size() == 1) {
					int to = *g[v].begin();
					st.erase(to);
					leaves[to].insert(v);
					st.insert(to);
				}
				st.insert(v);
				st.insert(leaf)

Shouldn't we insert v when we are done removing all the k leaves? Also what is the part if(leaves[leaf].count(v)) for?

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

https://mirror.codeforces.com/contest/1385/submission/87268081 can anyone explain what this code get Memory limit exceeded on test 7 !!

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

Problem F: http://mirror.codeforces.com/contest/1385/submission/87375086 wrong answer test case 20. I can't find my error.what's wrong in this. I used connect[] and leaf[] array for counting number of node connected with j node and number of leaf node connected with j node...help me..thank you

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

Here is my solution for problem D https://mirror.codeforces.com/contest/1385/submission/87450593 and its showing time limit error. Can anyone please tell me the mistake (I have used recursion as explained in tutorial).

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

Why is the time complexity of Problem D not exponential but O(N * log N)?

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

Can someone help me in problem F. Why does the solution in the tutorial add the leaf again into the set st? I fell it to ensure that st doesn't become empty. Any other reasons for it?

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

    Because the set with the custom comparator doesn't update the order of elements itself, you need to update it "manually". But during one move very few elements change so we can just remove them and then insert back with updated values.

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

I have tried to make editorial for questions A-E . please have a look. Language :- Hindi

https://www.youtube.com/playlist?list=PLrT256hfFv5UWlctr-HughML5i0U-zFYy

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

For Problem G, can someone explain how did you think of modeling the problem as a graph?

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

shouldn't the complexity of A-good string be O(n)? As we are doing constant amount of calculations for each node and there are a total of 2*n nodes in segment tree.

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

In the problem F, if two nodes have same no of leaves then the order of them can be anything right, why do we need the if statement in the comparator function of the problem F. Please anyone clarify it.

``` struct comp {

bool operator() (int a, int b) const {
    if (leaves[a].size() == leaves[b].size()) return a < b; // why do we need this
    return leaves[a].size() > leaves[b].size();
}

};

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

code for Problem C, please explain what is wrong, same logic as of editorial

//this is code
signed main(){
    int t=1;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<int>arr(n);
        for(int i=0;i<n;i++)cin>>arr[i];
        if(n==1){cout<<0<<"\n";continue;}
        int is_increase=0;
        if(arr[n-1]>=arr[n-2])is_increase=1;
        if(is_increase){
            int i=n-1;
            for(;i>=1;i--){
                if(arr[i]<arr[i-1])break;
            }
            cout<<i<<"\n";
            continue;
        }
        int idx=0,cnt=0;
        for(int i=n-1;i>=1;i--){
            if(cnt==0 && arr[i-1]<arr[i])cnt++;
            if(cnt==1 && arr[i-1]>arr[i])cnt++;
            if(cnt==2){
                idx=i;
                break;
            }
        }
        cout<<idx<<"\n";
    }
}

can anyone explain what i am doing wrong for Problem C, its getting failed at 888th test-case,

i used the same logic as in tutorial, but the code is bit different but i didn't get it, please help