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

Автор chokudai, история, 2 года назад, По-английски

We will hold AtCoder Beginner Contest 237.

We are looking forward to your participation!

  • Проголосовать: нравится
  • +50
  • Проголосовать: не нравится

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

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

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

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 года назад, # |
Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

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

Edit: In example this submission

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

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

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

      I used max Dijkstra

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

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

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

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

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

            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 года назад, # ^ |
              Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

              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 года назад, # ^ |
      Rev. 2   Проголосовать: нравится +12 Проголосовать: не нравится

      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 года назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
        • »
          »
          »
          »
          »
          2 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

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

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

            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 года назад, # ^ |
            Проголосовать: нравится +1 Проголосовать: не нравится

          Your code fails for testcase added after contest

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

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

            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 года назад, # ^ |
        Rev. 3   Проголосовать: нравится +7 Проголосовать: не нравится

        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 года назад, # ^ |
          Проголосовать: нравится +3 Проголосовать: не нравится

        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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 года назад, # ^ |
        Проголосовать: нравится +8 Проголосовать: не нравится

      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 года назад, # |
Rev. 2   Проголосовать: нравится +13 Проголосовать: не нравится

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

Spoiler

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

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

    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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

problem G

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

    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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

      Arigato :D

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

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

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

        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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I solved Ex using a greedy maximal clique solution.

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

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

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

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

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

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

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

can someone explain problem e solution plz

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

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

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

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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

My submission