10

Codeforces Round 914 (Div. 2) Editorial

 9 months ago
source link: https://codeforces.com/blog/entry/123160
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Sorry for the weak pretests on A :(. We hope you enjoyed the round nonetheless.

1904A - Forked!

Idea: Apple_Method
Preparation: Apple_Method/oursaco
Analysis: lunchbox

Solution
Code

1904B - Collecting Game

Idea: oursaco
Preparation: oursaco
Analysis: lunchbox

Solution
Code

1904C - Array Game

Idea: lunchbox
Preparation: lunchbox
Analysis: lunchbox

Solution
Code

1904D1 - Set To Max (Easy Version) / 1904D2 - Set To Max (Hard Version)

Idea: oursaco
Preparation: oursaco
Analysis: oursaco

Hint 1
Hint 2
Solution
Code

1904E - Tree Queries

Idea: Apple_Method
Preparation: oursaco

Editorial 1:
Analysis: oursaco

Thanks to errorgorn for the solution!

Hint 1
Hint 2
Solution
Code

Editorial 2:
Analysis: willy108

Hint 1
Hint 2
Hint 3
Solution
Code

1904F - Beautiful Tree

Idea: Apple_Method
Preparation: oursaco
Analysis: Apple_Method

Hint
Solution
Code
  • 3 days ago, # ^ |

    Great Idea to express that in the username xd

4 days ago, # |

after this contest i realised, that chess tasks are not for me)) i solved B easily, mb im stupid for A idk

  • 4 days ago, # ^ |

    Lol, I could say the same about math tasks. My suggestion would be for you to go onto chess.com and practice in your free time :)

    • 4 days ago, # ^ |

      i don't think the problem is that you are not good at chess enough, this is really just a brain teaser, i might suggest instead to get familiar with those type of problems, a good book about this is "The art and craft of mathematical problem solving", an interesting one full of problems, although it barely talks about math, just skip the math ones since the book structure is designed for so :3

  • 4 days ago, # ^ |

    and who can explain why this 236632847 doesn`t work ?

    • 3 days ago, # ^ |

      Try this sample:

      Correct answer would be 1 instead of 2 (from your solution)

      The reason is that when you use upper_bound, it give you the first element that is considered greater than your number. You should get the prev of that element for calculating difference. In the case above, 1006 - 1000 give you 6 and upper_bound give you 8, but you should look for 5

      • 3 days ago, # ^ |

        thanks you man !

4 days ago, # |

Got TLE in B on system testing. Did two wrong submissions also on B. I was hoping to be blue today. But missed the chance.

  • 2 days ago, # ^ |

    Downvotes shows hate or what?

4 days ago, # |

What's wrong with this solution for A? 236554300

  • 4 days ago, # ^ |

    236585544 is my solution with same approach

  • 4 days ago, # ^ |

    Don't do that, if you check all cases by it makes your code so long and sometimes missing cases lead to WA. Instead of using you can make 2 vectors Move_x, Move_y then For from 0->7 (a knight can move 8 cases) from the Queen and King, If a cell can be moved by King and Queen your Ans++. You can refer to my code: 236529329

    • 4 days ago, # ^ |

      Yes, solved it like that later but thought casework would work as well. I missed a few cases in brute force case solution which I have to check

4 days ago, # |

I liked D2 on how simple its implementation really was. Even though I implemented sparse table and range update segment trees, realizing it only required monotonic stack was pretty cool.

4 days ago, # |

can anyone tell me why did I get TLE here ? https://codeforces.com/contest/1904/submission/236554358.

I think the complexity here is O(N*N*log(N)). Am I wrong?

  • 4 days ago, # ^ |

    You used set and map there. So on the worst case time complexity is O(N*N*log(N)*log(N))

  • 4 days ago, # ^ |

    Rev. 2  

    0

    Quite strange that commenting out the map gets it accepted because the map itself should run in O(N*logN)

    • 4 days ago, # ^ |

      thats why I'm asking. I really didnt understand the issue in that part of the code.

      • 4 days ago, # ^ |

        • Your set h contains at most N ^ 2 elements. Then u lower_bound in that set for every i in array a, and run the pointer back to the beginning of the set, so the time complexity is O(N ^ 3 * logN)
        • 4 days ago, # ^ |

          Should the time complexity not be O(N^2 logN). As logN is required to find the upper/lower bound in the set for an element. Why there is an additional N factor in TC. Could you please explain?

          • 3 days ago, # ^ |

            oh my bad, i thought he runs the pointer back to the starting point after using lower_bound. Anyway, his code is correct, just need a few minor optimizations. Here the fixed code: 236638928

4 days ago, # |

Thanks for contest

4 days ago, # |

Can someone try hacking this solution to D2?

  • 4 days ago, # ^ |

    Rev. 2  

    0

    This test will TLE, courtesy of porker2008

    print(1)
    
    print(200000)
    
    ans = [200000 - i for i in range(200000)]
    print(" ".join(map(str, ans)))
    
    ans = [min(200000, 300000 - i)  for i in range(200000)]
    print(" ".join(map(str, ans)))

4 days ago, # |

Rev. 2  

-10

nice problems

4 days ago, # |

Loved this competition! My solution to problem C first failed on system testing, but then suddenly the verdict changed) Also, my friend’s solution failed on the same test, but it changed the verdict after the final results! Can someone please explain how this could be, because I just don't understand how system testing works on Codeforces. Sorry for my poor language...)

  • 4 days ago, # ^ |

    Something like that happened to me as well. Not sure what happened there

4 days ago, # |

can somebody help me with the reason of tle on d2(hard version) on test case 25 .here's my submission — https://codeforces.com/contest/1904/submission/236593367

  • 4 days ago, # ^ |

    that is n^2

4 days ago, # |

Cool Contest!

4 days ago, # |

For D1/D2, I had a solution I'd like to share.

Firstly, if any position exists such that a[i] > b[i], then obviously there's no solution.

Then, I iterated over the numbers from 1 to n, and for each i, I tried to find all the intervals centered around a position j such that a[j] = i. I then tried to extend it as much to the left and right as possible without messing any conditions up. I found that it you did this for all numbers from 1 to n in increasing order, then you'll either construct a valid solution or find that it's impossible to do so. You can optimize this a bit for D2 by binary searching how far to the left and right you'll extend your current position to and using range assignment and range query operations to check if the intervals you're using are okay (You can do that with a lazy segment tree).

In essence, it's similar to the official solution except you actually construct the solution in this case instead of determining if it's possible. Here's my submission: 236596139

It barely passes in time lol

4 days ago, # |

It will be better if F offers stronger samples.

I finished coding and passed samples in 15 min but got WA on #3, and failed to debug in remaining 30 min.

Anyway, A~D are enjoyable.

4 days ago, # |

Rev. 2  

0

Nice problems. D2 systests were pretty weak though.

4 days ago, # |

In problem E, how are we handling the case when path is outside of current subtree?

  • 4 days ago, # ^ |

    If you reroot the tree to correctly, then each leaf in the segment tree will store the distance of a node to and this isn't limited to just 's subtree. Doing a max query on all the non blocked intervals will then find the maximum distance to all reachable nodes.

4 days ago, # |

Was the TL and ML for problem F a little tight in this contest? I see a lot of submissions here which have taken > 3 seconds.

I have roughly the same solution as the editorial, except that my graph is built with edges denoting >= relationships (instead of > relationships), then I find the SCCs of the graph, and the remainder of the construction is roughly the same. Or is there something else extremely slow about my code? I could get it to pass very narrowly with pragmas though.

  • 3 days ago, # ^ |

    Maybe to solve the memmory problem,you should use tree chain dissection.

  • 3 days ago, # ^ |

    But the time complexity is O(log^2n) However

4 days ago, # |

E is a great problem, thank you for such a masterpiece!

4 days ago, # |

236621743 can someone please give me any idea to improve this code as iam getting time limit exceeded for test3 of B.

4 days ago, # |

Can someone help me understand the solution for D.Why does there always exist a valid sequence of operations if we can satisfy the conditions for each element independently

3 days ago, # |

How to solve problem E if the queries given online?

  • 3 days ago, # ^ |

    The solution described in "Editorial 2" is online.

3 days ago, # |

Nice Solutions.

3 days ago, # |

Can someone help me in C? The first test gives me MLE and WA but in my computer gives me a correct answer 236711122

  • 3 days ago, # ^ |

    Maybe you can try resubmit.

    I tested your solution and it is working fine (there is a bug in your solution though)

3 days ago, # |

Rev. 4  

0

In Problem F, can anyone explain how they are achieving this exactly: "Given a path and a node, add a directed edge from the node to every node in that path"? I can't understand how they can add edges to (or from) all the nodes in the path, won't that take linear time?

Also, in this sentence "we can repeatedly add an edge from that node to the largest node we can" what does "largest" node mean?

  • 28 hours ago, # ^ |

    Imagine creating a binary lifting on the tree. You have , , etc. Here, is an auxiliary node that has edges to and . is an auxiliary node that has edges to and . Note that this is equivalent to having edges to , , and . Similar to how you always jump up the largest power of 2 that fits when you perform binary lifting, you will always add an edge to the auxiliary node with the largest power of 2 that fits. This will always require at most edge additions.

3 days ago, # |

There is also a online sol for E (although with high constant).

Take the euler tour of the tree (the version where you insert again every time you go to a new node, exactly like this image taken from https://cp-algorithms.com/graph/lca.html ):

Lemma: the length of the diameter of a tree is for , being the height of the node in the euler tour. In that case, if is the node in the euler tour, the endpoints of the diameter will be and and their lca will be . Proof is an exercise to the reader ;)

Using that lemma, we can ban segments like the solution 1 and if is the source node of our query, the answer will be for or for , which can be done with a segtree. You just need to remember to rollback your updates after every query. Code: https://codeforces.com/contest/1904/submission/236721097

3 days ago, # |

Hi can someone help me in figuring out why my two pointer approach for problem C is failing?

https://codeforces.com/contest/1904/submission/236751570

  • 3 days ago, # ^ |

    Ive sorted the array and finding the sum which is closest to the ith element and then min of all their difference is the answer.. can anyone pointout the issue in my logic?

    • 2 days ago, # ^ |

      You can try this case.

      Spoiler
      • 40 hours ago, # ^ |

        Thanks!! i missed this case

2 days ago, # |

In problem C should I pick different (i,j) each time?

  • 2 days ago, # ^ |

    No there is no any condition on taking different (i,j) each time.

2 days ago, # |

Or can someone share their two pointer approach for problem c?

  • 2 days ago, # ^ |

    Your two pointer approach is mostly correct except two bugs.

    Check out 236828774 for references.

46 hours ago, # |

Direction of the edges in the explanation for the solution for F are backwards

40 hours ago, # |

can someone please explain what does |a| notation mean in problem C? is it the length of array?

if so, why sorting array in this problem doesn't violate the condition of must pick some (i,j) such that 1≤i<j≤|a|?

  • 37 hours ago, # ^ |

    You are correct. |a| means the length of the array a.

    Essentially, in each operation, you can pick two different elements (can be equal) from the array and compute the absolute differences and put the result back to the array.

    So the order of the elements in the array does not matter and you are free to reorder the array (including sorting it).

32 hours ago, # |

Can anyone explain me how edges are created in "problem F" in simple way and why are they created in that way(thought process)?

32 hours ago, # |

why do we need to find smallest ai satisfying ai≥v and greatest ai satisfying ai≤v in C why can't we go over all combinations of dif[i]-a[j] (where dif is the difference array of a of length n-1)

failed submission : https://codeforces.com/contest/1904/submission/236901057

9 hours ago, # |

Hi thanks for this contest and I hope this is not necro-posting but for , don't you think it is enough to check if and then set the answer of equal to the answer of ? I have an implementation which you can check out 237039030.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK