chokudai's blog

By chokudai, history, 2 years ago, In English

We will hold AtCoder Beginner Contest 237.

We are looking forward to your participation!

  • Vote: I like it
  • +50
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

for D I created binary tree and did inorder traversal on it. I'm sure there's better approach for it.

»
2 years ago, # |
  Vote: I like it +42 Vote: I do not like it

I don't know if it's a bug in atcoder or the problem was updated, but I got the statement for problem D as problem B at first before I reloaded: https://imgur.com/a/FFEzsaY

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    ya it happened with me as well.

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    I know I was wondering why it's taking me longer to do B.

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    Same for me, one wrong submission.

    • »
      »
      »
      2 years ago, # ^ |
      Rev. 3   Vote: I like it +4 Vote: I do not like it

      Check the clarifications tab.

      If you have submitted the answer to question D to question B, please contact us through the clarification tab within 5 minutes after the contest ends. We will delete your submission.

»
2 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

Can somebody explain why a super simple bfs in E does not TLE?

Edit: In example this submission

  • »
    »
    2 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Simple BFS worked for E ? :( I had to do Bellman Ford to solve it My Code with Bellman Ford

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +19 Vote: I do not like it

      I used max Dijkstra

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I thought of using it then remembered it does not handle negative edges so Thought of using this

        • »
          »
          »
          »
          »
          2 years ago, # ^ |
          Rev. 2   Vote: I like it +12 Vote: I do not like it

          negative edges are ok, but not negative cycles. And there are no negative cycles as this would be physically impossible

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
              Vote: I like it +3 Vote: I do not like it

            Why are negative edges ok but not negative cycles? Doesn't dijkstra work because before processing vertex v, we have already processed all vertices with distance smaller that distance to v, and negative edges would break this

            • »
              »
              »
              »
              »
              »
              »
              2 years ago, # ^ |
              Rev. 2   Vote: I like it 0 Vote: I do not like it

              Depends on your implementation, if you allow to visit a node more than once you will be fine. The runtime analysis is then a bit more complicated, but I believe it should better than Bellman Ford, as latter will do the maximum amount of iterations possible, while Dijkstra might stop earlier..

              Implementation using a set, to update keys inside the priority queue (from CP4)
    • »
      »
      »
      2 years ago, # ^ |
      Rev. 2   Vote: I like it +12 Vote: I do not like it

      How did you do bellman Ford? It's n^2 right. And how are dijkstra solution passing for E. :(. There are negative edge weights. It should fail!!

      • »
        »
        »
        »
        2 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it
        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Doesn't matter, even if you negate, positives become negative, and negative become positive. Still negative will be present.

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
            Rev. 10   Vote: I like it +4 Vote: I do not like it

            The problem with dijkstra in a graph that has negative edges, even if it doesn't have negative cycles, is that it works slower. I make an optimization in the dijkstra that is: when I remove a node from the priority queue, I mark it, so as not to calculate it again in the future.

            This works because this algorithm is greedy, and since the node that I remove from the priority queue at a given moment, I will not be able to update it in the future, because all the nodes that I analyze later will have a distance from the initial node greater than its distance. This would fail in a graph with some negative edge. The algorithm itself would work as long as there are no negative cycles.

            This is a graph that breaks the optimization I'm trying to explain:

            In this problem there is no difficulty of any kind with negative edges since we want to maximize the distance, so edges with negative weights do not suit us.

        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          Your code fails for testcase added after contest

          https://atcoder.jp/contests/abc237/submissions/28970593

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
            Rev. 2   Vote: I like it 0 Vote: I do not like it

            Thanks. I realized that I was lucky. Now, I saw a Tweet and followed the advice to change priority queue to queue. Then the after contest test passes: submission

            The Tweet says AtCoder should add one more after contest test case so that changing PQ to queue still TLE.

      • »
        »
        »
        »
        2 years ago, # ^ |
        Rev. 3   Vote: I like it +7 Vote: I do not like it

        The final happiness ending at node i can be viewed as (Initial height — final height) — All the edges where you travelled upwards. So make a graph where upward edges have positive weights and the downward edges have weight zero. Find minimum distance for each node from node 1. Submission

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Let dp[v] be maximum happiness reaching vertex v.

        We initialize dp with negative inf and dp[1] = 0.

        Let's say we have an edge between vertex 1 and 2, and {height of 1} < {height of 2}.

        When we're updating vertex 1's neighbors,

        dp[2] = max(dp[2], dp[1] - (h[2] - h[1]) * 2)
        

        The second one is obviously bigger since we set all values to -inf by default, and therefore values can be calculate with dijkstra's.

        My submission

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The while() cycle does what a normal while() in bfs does — goes over all nodes, so O(n) from here. For every node, we traverse all its edges and since every edge has two endpoints, this means we get O(2M) iterations from here. So all in all we get a complexity of O(n + 2m). Hope that helped!

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think it's SPFA, Shortest Path Fast Algorithm. It will run in approximately O(N + M). It doesn't get TL because it's hard to construct a good graph.

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Thanks for pointing that out. Actually, that is what I referred to as "super simple bfs". It looks like dijkstra but with a simple queue instead of a priority queue.

      And also the problem with this is described in the article, its worst case complexity is like Bellman-Ford, so would TLE.

      On the other hand, the way the tree is constructed makes to edge weights not arbitrary, they obviously determine each other. It seems that this makes the simple algorythm work.

      Which, frankly said, makes the problem a bad one. Given the fact that this is a beginner contest, we can assume that a lot, maybe the most partitipants who solved it did not because beeing smart enough to see that.

»
2 years ago, # |
Rev. 2   Vote: I like it +13 Vote: I do not like it

Me after solving A,B,C,D,E

Spoiler

Can anyone explain their approach for F as editorial is not published for now.

  • »
    »
    2 years ago, # ^ |
    Rev. 3   Vote: I like it +11 Vote: I do not like it

    I solved F with DP, so dp[i][j][k][l] can count the number of sequences up to the ith number, where LIS of length 1 end with j, LIS of length 2 end with k and LIS of length 3 end with l. In each of these states you can iterate through all numbers 1,..,m, and either make one number j, k or l smaller or we are not allowed to use it (as greater than current l)

    So there are n*m^3 states and each state needs O(m) -> O(n*m^4) = O(1000*10000) = O(10.000.000)

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +9 Vote: I do not like it

      If you use a bitmask to maintain the young diagram . You can know the number of sequence which LIS is k (k<=m) in O(n2^m).

      my solution: https://atcoder.jp/contests/abc237/submissions/28953647

      • »
        »
        »
        »
        2 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Is this a standard technique? If yes, can you provide resources (problems, or a blog-post) on it?

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it +13 Vote: I do not like it

        Gary2005 your approach is lit....

        Just amazed me as for LIS upto M you just only have to change if condition of popcount==3 to popcount==LIS and solution Time complexity is independent of LIS length required henced amazed me great Approach. Thankyou For sharing....

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    plz explain E same for me after doing 4 only also tell some resourses for graphs

»
2 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Ex and div1f of last round are exactly the same problem.(After building the graph)

»
2 years ago, # |
  Vote: I like it +3 Vote: I do not like it

problem G

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    Actually it can be solved much easily using just 3 lazy segtrees, as we just need to keep track of number X(say 1), and numbers smaller than X(say 0) and bigger than X(say 2).

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      It can be solved using 1 lazy segment tree, and just keeping track of the index of $$$x$$$ after every query.

      My submission

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you share your submission for that? I think I am too stupid to get this blog-post :(

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Anyone with Top Down approach for F ? It will be great if you can share your code

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it
    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Arigato :D

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      if(curIdx == n && rd != m+1) return ans = 1; if(curIdx==n) return 0; can u plz explaint these lines?

      • »
        »
        »
        »
        2 years ago, # ^ |
        Rev. 4   Vote: I like it 0 Vote: I do not like it

        Hey mate,

        I used m+1 as dummy value, when the rd ( meaning the smallest end of a LIS of length three — naming is bad here) is still m+1, we don't have no LIS of length 3.

        For example, If you choose 123 you will have fst = 1, snd = 2 and rd = 3. However if you have 111 you will have fst = 1, snd = m+1 and rd = m=1, meaning you only have a LIS of length 1.

        Not sure how the others implemented it. For me this felt the most natural.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I solved Ex using a greedy maximal clique solution.

»
2 years ago, # |
Rev. 2   Vote: I like it +10 Vote: I do not like it

why don't atcoder amalgamate test cases like cf and cc it will be easier to copy and check on local compilers

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The editorial is available in Japanese but not in English. Maybe there is just a script to run to make it available ?

  • »
    »
    2 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    The sample solution are written in C++ or Java or Python. Those are still readable without knowing Japanese.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can someone explain problem e solution plz

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    see above

    Note that also a simple bfs works fine. I am still not sure if this is caused by the construction of the tree, or weak tests.

»
2 years ago, # |
  Vote: I like it +10 Vote: I do not like it

When will be the editorials posted? It has not been posted yet.

»
2 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Since official English editorials never arrived, I translated all Japanese editorials myself. They are brief and not word-to-word precise, more like my own restatement of the official solution. Hope it helps.

https://atcoder.jp/contests/abc237/editorial/3347

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    In order to understand G, I spent 2 days now reading the segment-tree for sorting subsequences and I still didn't really get it. Now I read your translation, and after 2 mins I got it. Thanks a lot & well done :)

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can someone tell why max Dijkstra on problem E getting TLE?

My submission