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

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

We will hold AtCoder Beginner Contest 246.

The point values will be 100-200-300-400-500-500-600-600. We are looking forward to your participation!

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

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

as always looking forward to participate

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

How to solve E?

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

    Using Dijkstra with adjacency between each cell and the four next to its corners. Note that the cost does not increase when you make 2 moves using the same position change, e.g., up right. So the last position change needs to be maintained as well in the set / priority queue.

    EDIT: 01-BFS works as well because edge weights can be only $$$0$$$ or $$$1$$$. So, instead of always pushing to the queue tail as we do in the normal BFS, when encountering an edge with weight $$$0$$$ we can push it to the queue front. This guarantees that costs are processed in non-decreasing order.

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

    You can use normal BFS too (not just 01-BFS).

    Keep a 'seen' dictionary/map (seen[pos] = c where c is the min cost of getting to pos from (ax,ay) (the cost of the first time you get there since it's BFS)). Then for each pos loop on each diagonal adding (x,y,c+1) to the queue/deque (and to 'seen') until you reach a pos either outside the board or with a pawn OR in 'seen' with seen[pos]<c+1 (in order words you can reach that pos earlier from somewhere else so on the next move you'll have access to the rest of the diag).

    I haven't proven the complexity but it passed.

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

Problem C: Why priority queue got AC but the sorted array got WA on 6 test cases? Any suggestions

Update: Fixed Sorted Array. Got AC

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

    Hi! You seem to terminate your loop when m == 0, but doing so you won't add a[i] (when you have 0 coupon left, you still need to pay for the item :) ) to your sum value.

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

The definition of bad luck:

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

Can anyone provide a more detailed explanation for D? The editorial feels incomplete. Edit — Understood the approach. Thanks mohamedeltair

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

    Note that the largest number whose cube $$$\le 10^{18}$$$ is $$$10^{6}$$$. We can iterate on all the $$$10^{6}$$$ values, assume the current value is $$$a$$$, then use binary search to get the lowest $$$b$$$ giving answer $$$\ge$$$ $$$n$$$.

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

      The editorial uses the same general idea but with 2 pointers instead of binary search. Suppose for specific $$$i$$$ we got the lowest $$$j$$$ such that $$$(i, j)$$$ gives solution $$$\ge$$$ $$$n$$$. For $$$i+1$$$, the other value for sure is $$$\le j$$$. So we can keep decrementing $$$j$$$ until we get the lowest one giving a valid solution. And so on.

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

Can anyone give an example of a test case where bfs would fail in problem E. My bfs implementation fails on 8 test cases but I am unable to find a case where it fails

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

    I use this method as well, and get WA, but don't know why.

    The main idea is that we stop as soon as we meet a block or a cell which has beem visited or it is outside of board.

    Still have no idea why this is not correct. Can anybody tell why and provide some counter example?

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

      we stop as soon as we meet a block right

      or a cell which has beem visited wrong

      or it is outside of board right

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

    same approach. came here to post the same comment to get an answer. Can someone please answer?

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

      don't stop when a cell has been visited

      because it may be visited when the diagonal looks like / instead of \ when we bfs \ lines

      instead stop when bfs / and visited / also when bfs \ and visited \

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

    18o3 SummerSky nagaraj41 Try this testcase.

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

      Nice counter example! Thank you so much for your kind help!

      Now I have realized that it is not correct to stop as soon as we meet a node which has been visited before.

      Such an unblievable problem! I have learned a lesson that, when we use bfs, it is much better and safe to move only one step forward, rather than several steps at one time :)

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

      does CF stress works for atcoder problems too?

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

        Lol, It'd be kinda ironic if it did. (The name's literally CF Stress).

        But I do have some workaround on my local server to support Atcoder problems, not really sure if I'll officially support Atcoder in future. (As they already provide the testcases on Dropbox, so my website would be redundant).

        If you're interested in Problem E of today's contest, let me know, I can run your submission on my local server.

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

      Come here for saying thanks again, after I finally get accepted.

      But now, I feel quite confused :(

      For instance, given a board and each time we could move one step up/down/left/right, and we are asked to find the minimum steps after which we could reach [tx,ty], starting from [sx, sy]. I think this is a classic problem, and when we use bfs, we could stop as soon as we meet a node that has been visited before.

      But why, this is not correct in this problem? What is the essential reason behind this, and what is the essential difference between these two problems?

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

        Read the comment above. this scenario does not occur in a one-step bfs. that's the only difference.

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

        There is nothing special about this problems. BFS will 100% work, regardless of whether you are using a single step process, or traversing the entire level in one go. You just need to remember one philosophy of BFS, Every unvisited adjacent vertex must be added to the queue with a level difference of 1. (From your implementation, I can say that you've missed out on the every+adjacent keyword, let's see why do they matter).

        You are correct in assuming that whenever you see problems containing a grid with moves, BFS is applicable. However, remember that BFS was originally intended to be applied on an unweighted graph.

        How does our graph look like in this case? There are $$$n^2$$$ vertices, and there are some edges between them. Note that we need to add all the direct edges from the matrix in this graph.

        The resulting graph for the counter example I provided above

        Note that while performing BFS, on Level 2, you forgot about two adjacent edges that existed in the original graph and on Level 3, you forgot about one adjacent edge from the original graph. (I've marked them with Forgot keyword). Since you ran your BFS on a wrong graph, you got an incorrect answer.

        Try to run your algorithm on the graph I shared, it should produce the correct result.

        In conclusion, for matrix based problems, always visualize what the hidden graph looks like (by adding ALL direct edges) and then, all usual graph algorithms would work on it.

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

          oh my god!!!!

          I thought that I have truly understood everything about bfs, but I was wrong :(

          I really benefit a lot from your words "every unvisited adjacent vertex must be added to the queue with a level of difference 1", and get a better understanding from the Forgot word in your graph.

          It seems that this is not your main account, but anyway, thank you so much for your paticient help, precise and clear arguments, and detailed examples. I believe that you are not only a great coder, but also a great teacher :)

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

    BFS is not correct because moving one cell sometimes will increase the cost by 1 (when position change has changed, e.g., up right then up left) and sometimes will not increase the cost (when position change has not changed, e.g., up right then up right).

    So, it may happen that for the same cell it has been put in the queue earlier using the cost change 1 and then we encounter it again but with cost change 0, where it should be put and processed before the earlier one.

    EDIT: 01-BFS works as well because edge weights can be only $$$0$$$ or $$$1$$$. So, instead of always pushing to the queue tail as we do in the normal BFS, when encountering an edge with weight $$$0$$$ we can push it to the queue front. This guarantees that costs are processed in non-decreasing order.

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

You can check my video editorials of D and F if you want. I will upload E in a while

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

Binary search + Brute force was an obvious approach for D wonder why editorial didn't include it

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

Why My BFS got WA (problem E)

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

Does anyone know how to solve G? I try to read the editorial but got lost in the DP on tree part and wonder why that works

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

    I solved it the same way as editorial but don't really understand why it works... I was just using intuition

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

Can anyone please point out what is wrong with my approach in Problem E? I have implemented a 0-1 bfs solution Submission

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится
    Input
    Expected Output
    Your Output
    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      what about this submission?

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

https://atcoder.jp/contests/abc246/submissions/30780042 im getting wa for 10 cases out of 60 -_- . help , what im missing ?