Блог пользователя nika-skybytska

Автор nika-skybytska, 5 лет назад, По-английски

Problems, my solutions, YT link

A. Three-Point Shot

The team behind has min(x, y) points, and the team ahead has max(x, y), so we can check if min(x, y) + 3 > max(x, y).

B. Orthogonality

Just compute inner product according to the given formula. Use int64_t if you are as paranoid about overflows as I am :)

C. ABC Tournament

Two finalists are max of their halves, so left = max(a[:middle]) and right = max(a[middle:]). Second place is min of finalists.

D. Snuke Prime

All these interval processing problems can be solved in the same way, by splitting each interval into two events: start and end. After sorting events we can process them in a sequential fashion, maintaining the current daily cost.

E. Peddler

DP on DAG, which is already conveniently represented in its topological sort order. Create a dp storing min price at ancestors, and update it along the edges of the graph.

F. +1-1x2

Solve problem backwards: try to get from $$$y$$$ to $$$x$$$ with $$$\pm1$$$ and $$$/2$$$ operations. If we make at least one $$$/2$$$ operation, then it is reasonable to make at most one $$$\pm1$$$ operation before each $$$/2$$$ operation. Therefore, we can write a recursive solution as follows:

  • base cases are $$$x \ge y$$$, with $$$x - y$$$ operations, and no $$$/2$$$ operations with $$$y - x$$$ operations;

  • if $$$y$$$ is odd then take min with $$$2 + \text{solve}((y-1)/2)$$$ and $$$2 + \text{solve}((y+1)/2)$$$;

  • if $$$y$$$ is even then take min with $$$1 + \text{solve}(y/2)$$$;

It works in logarithmic time if you cache answers, because on each layer we have only two consecutive numbers: $$$\{2k, 2k+1\} \mapsto \{k,k,k+1\} = \{k,k+1\}$$$, and $$$\{2k-1,2k\}\mapsto\{k-1,k,k\}=\{k-1,k\}$$$.

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

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

(...) then it is reasonable to make at most one ±1 operation before each /2 operation

why?

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

thx

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

Your logic for F is nice , I got stuck with increment operations and was trying to solve from x to y which confused me much. Thanks for your short and sufficient editorial.

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

your F solution is very nice. Also, Solution passed in only 6ms.

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

hi i need some help in D my approach -> i tried to store each day's individual cost using a hashmap and then check each day cost individually, if it is more than given C than add C else add that cost

my code works fine for smaller test cases but gives TLE/error in large test cases it is giving 2213ms (just a little over given time limit) for larger test cases

my submission — https://atcoder.jp/contests/abc188/submissions/19345764

can you suggest any improvement in my logic and way of storing individual day cost?

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

Nice explanation of F. I think my solution was equivalent to yours (I formulated it using Dijkstra, which is a bit messier), but I didn't actually bound the runtime -- I liked your explanation.

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

Please add your YouTube Channel Link somewhere in your Github Bio. It will help a lot.

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

Can we prove that at any layer there will be at most 2 distinct numbers? I tried a few examples and it seems to be working

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

    We can prove by inductive argument.

    If $$$y$$$ is even , then first layer will have only one number $$$y/2$$$. If $$$y$$$ is odd , then first layer will be $$$(y-1)/2,(y+1)/2$$$ and difference between them is $$$1$$$. Hence they will be also consecutive .

    Now suppose any layer contains only single number say $$$y_1$$$ , then by previous argument layer next to it will contain at most 2 consecutive numbers.

    If layer contains two numbers say $$$y_1$$$,$$$y_2$$$ and both are consecutive and let's say $$$y_1$$$ is odd , then next layer will have $$$(y_1-1)/2$$$,$$$(y_1+1)/2$$$ (it's equal to $$$y_2/2$$$) and thus only two consecutive numbers.

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

The following code, for problem F, is giving me TLE. How can i improve it? Please help.


public static long findAnswer(long x, long y) { if(x >= y)return x - y; if(y % 2 == 0) return Math.min(1 + findAnswer(x,y/2), y - x); else return 1 + Math.min(findAnswer(x, y + 1), findAnswer(x, y - 1)); }
»
5 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

What is handmade_03.txt in problem F? I got one WA on this test and don't understand what the error is.

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

    Good question! I don't know about handmade_03.txt specifically, but I tested your latest submission against mine, and your fails on $$$x = 1$$$, $$$y = 39$$$ with $$$8$$$ operations compared to my $$$7$$$. I think that problem with your logic is that you either always add $$$1$$$ to odd numbers or always subtract, and I see no reason for this to be optimal. In the example above your operations are $$$39 \mapsto 38 \mapsto 19 \mapsto 18 \mapsto 9 \mapsto 8 \mapsto 4 \mapsto 2 \mapsto 1$$$ and optimal solution is $$$39 \mapsto 40 \mapsto 20 \mapsto 10 \mapsto 5 \mapsto 4 \mapsto 2 \mapsto 1$$$, where both $$$+1$$$ and $$$-1$$$ for odd numbers are used.

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

Does anyone had Solved D. Snuke Prime using Coordinate Compression?. Please Share your submission.