Pyqe's blog

By Pyqe, 13 days ago, In English

Selamat pagi Codeforces!

We're going to hold The 2024 ICPC Asia Jakarta Regional Contest.

We invite you to join the online mirror contest 2024-2025 ICPC, Asia Jakarta Regional Contest (Online Mirror, Unrated, ICPC Rules, Teams Preferred) on Codeforces. It'll be held on Dec/01/2024 08:05 (Moscow time) and will last for 5 hours.

The mirror will be held using ICPC-style scoring with 10 to 13 problems. The problem statements will only be available in English. We encourage participating as a team using only one computer for coding (as in ICPC contests). The mirror will be unrated.

Some useful links:

We hope that you'll enjoy the contest. Good luck and have fun!

UPD. The mirror has ended! You can find the editorial here. The onsite scoreboard will be unfrozen after the closing ceremony. Thank you for participating and we hope that you enjoyed the problemset!

UPD2. Mistake in Problem D. Aquatic Dragon

The act of activating a shrine is supposed to be mandatory once you reach an island instead of being optional. During contest preparation, the initial problem had a statement that implies the aforementioned obligation of shrine activation, which the judges had a correct solution for (the one in the editorial). However, when rewriting the problem statement for more clarity with adjustments to the story, we ended up making it such that it's optional, without too much thought about it, thinking it's essentially still the same problem. Unfortunately, as we have recently found out after the contest, it's in fact not the same problem.

The action we're taking now is fixing the statement of the problem after the contest with the correct intended version without changing the judges' solution. We are very sorry for the inconvenience.

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

»
10 days ago, # |
  Vote: I like it +23 Vote: I do not like it

std_abs will win.

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve icpc squares?

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I did some magical stuff here.

    First, reduce the input $$$(N, D, S)$$$ into $$$(n, d, s) = (\lfloor \frac{N}{S} \rfloor, \lfloor \frac{D}{S} \rfloor, 1)$$$.

    First observation is that the answer is clearly $$$x_1 x_2 S$$$, with the variables being such that $$$1 \le x_1 \le d$$$, $$$1 \le x_2 \le \lfloor \frac{d}{x_1} \rfloor + 1$$$ and $$$1 \le x_1 x_2 \le n$$$.

    Thus, now we can try every $$$x_1$$$ and $$$x_2$$$ to find the answer.

    Magical parts (that I myself can't yet prove why they worked): I only probed $$$x_1$$$ and $$$x_2$$$ until each of them reached either $$$10^6$$$ or $$$d$$$, whichever is lower. Had to probe both variables independently — $$$x_1$$$ in range $$$[1, \text{lim}]$$$ and $$$x_2$$$ in range $$$[2, \text{lim}]$$$ — only doing one of them caused WA.

    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thanks

    • »
      »
      »
      9 days ago, # ^ |
      Rev. 2   Vote: I like it +5 Vote: I do not like it

      This is how i solved it: If s>d , answer is s. Else answer will be some factor of s,also answer must be less than equal to min(2*d,n) because we can't obtain anything greater than 2*d using given operation.

      Let res=min(2*d,ans),do res=res-(res%s) (to make res a multiple of s) then the answer will either be res or (res-s).

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

        Proof

        Let the series of floors is ( s — p1*s — p1*p2*s — p1*p2..pk*s ) where res=(p1*p2*..pk*s),now for this to be valid,it must satisfy following inequality:

        (s(p1-1)<=d , (p1*s(p2-1)<=d) ,.... (p1*p2*..p(k-1)*s(pk-1)<=d)

        Now observe that if the last inequality is satisfied ,others will also be ok.

        To satisfy last inequality we need the pk to be as small as possible(obviously greater than 1) , find the smallest factor of res and see if it satisfy the inequality (since n<1e12, we only need to check at max 1e6).

        Now, observe that if res is not the answer , then res must be odd otherwise putting pk=2 will satisfy our equation ( since res<=2*d, (res/2)<=d), for res to be odd ,s must be odd (as res is multiple of s) which makes (res-s) even and hence the answer with pk=2.

        THANKS.

        P.S. — This gave me a headache but is was worth it.

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

How to do E? My team thought about a $$$\mathcal{O}(5^3 \cdot n \cdot \log{n})$$$ solution using segment tree of matrices, but we didn't feel confident that it'd pass TL.

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

    You can think about contribution of each element. Suppose u want to see if an element x contribute, the segment it is on must have all elements on its row before < x and after it >= x (to handle duplicate x cases, allow only the earliest x to contribute), then on the other row it must have an element > x. So for each element u can calculate the ranges that satisfy for your row and the range it dont satisfy for the other row, which can be done fast Then, you just do probability calculations for each element to find probability it contribute to sum once u have the ranges

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

how to solve H

  • »
    »
    9 days ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it
    • Consider adjacent words $$$s[i,j],s[j+1,k]$$$.It should be $$$s[i,j]<s[j+1,k]$$$,you can see that the valid $$$k$$$ is an interval, or more accurately, a suffix.Define $$$k_{mn}$$$ as the minimum value of $$$k$$$.Define $$$dp_{i,j}$$$ as the maximum number of words with the first $$$i$$$ characters ending in $$$s[i, j]$$$.You can see $$$dp_{i,j}$$$ can update $$$dp_{j+1,k}$$$ with a time complexity of $$$O(1)$$$ using suffix marking.
    • You can use $$$exkmp$$$(aka.$$$Z\;function$$$),$$$suffix\;array$$$ or others to calculate $$$k_{mn}$$$ quickly.
    • Forgive my Chinglish.
    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thx, I got TLE while trying to find k using binary search.(Had a brain fart during the competition.)

    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Additionally, calculating $$$k_{mn}$$$ could also be done with a second (simple) DP too, as in the editorial :)

      • »
        »
        »
        »
        9 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thank you, you're right. I was thinking about this simple DP yesterday but couldn't figure it out. At the same time, my teammate pressured me to work on the data structure, so I ended up being forced to write some mindless advanced algorithms.

»
9 days ago, # |
  Vote: I like it +60 Vote: I do not like it

The problem D editorial seems to assume that you always activate a shrine when you first reach the corresponding island. Is this true and if so, why?

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

    Thank you so much for pointing this out. We've updated this blog with an announcement.

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

Wondering how the grader for L is implemented, does it run faster than $$$O(k)$$$?

  • »
    »
    8 days ago, # ^ |
    Rev. 2   Vote: I like it +36 Vote: I do not like it

    Yes, the following grader is a modification to the function BDFS() that runs in $$$O(N+M)$$$.

    BDFS_Grader(K):
      let S be an empty stack
      let FLAG be a boolean array of size N which are all false initially
      let counter be an integer intialized with 0
    
      push 1 to S
    
      while S is not empty:
        pop the top element of S into u
    
        counter += num_of_neighbours(u)
        if counter > K: return WA()
        if visited[u] is true: continue
        visited[u] = true
    
        for each v neighbour of u in ascending order:
          if visited[v] is false:
            push v to S
    
      if counter != K: return WA()
      return AC()
    

    This is also part of the observation to simplify brainstorming the valid construction, although we are aware it is possible to find the construction without this observation.

»
8 days ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve G? I read the editorial but did not understand :((

  • »
    »
    8 days ago, # ^ |
    Rev. 2   Vote: I like it +14 Vote: I do not like it

    Hello, here is the original editorial that I wrote when I proposed the problem. I thought this version is a bit long-winded and I tried to simplify it. I apologize for it ended up being harder to understand.

    There are 4 simple yet important lemmas to solve this problem.

    Lemma 1. The answer is either INVALID for all scenarios or the total penalty of all cycles in the grid is $$$0$$$.

    Proof. Suppose that there is a cycle with a total penalty of $$$x$$$. Notice how the cycle in the opposite direction has a total penalty of $$$-x$$$ (negative cycle).

    Define this property of "all cycles have total penalty of $$$0$$$" as zero-cycle.

    Lemma 2. A grid is zero-cycle if all cycles from two-by-two sub-grids have a total penalty of $$$0$$$.

    Proof. This lemma is the hardest to prove. I saw many solutions that just run DFS from $$$(1, 1)$$$ and check if a negative cycle exists. This is basically doing the same thing but without realizing/proving this lemma.

    As mentioned in the editorial, any cycle can be transformed into any other cycle through a series of substitutions of the penalty. It is hard to explain it in words but easier to demonstrate. Here's a rough sketch that I drew, hopefully this helps to understand why it is correct.

    With Lemma 2, we can check whether the grid is zero-cycle or not with a simple $$$O(NM)$$$ loop. Now, we just need to calculate the shortest path.

    Denote $$$d(u, v)$$$ as the minimum total penalty from cell $$$(r_u, c_u)$$$ to $$$(r_v, c_v)$$$. Note that all cycles have a total penalty of $$$0$$$ (because the scenario is invalid, otherwise).

    Lemma 3. $$$d(u, v) = -d(v, u)$$$.

    Proof. $$$u \rightarrow v \rightarrow u$$$ is a cycle. Therefore, $$$d(u, v) + d(v, u) = 0$$$.

    Lemma 4. $$$d(u, v) = d(u, k) + d(k, v)$$$.

    Proof. $$$u \rightarrow k \rightarrow v \rightarrow u$$$ is a cycle. Therefore, $$$d(u, k) + d(k, v) + d(v, u) = 0$$$. By Lemma 3, $$$d(u, k) + d(k, v) - d(u, v) = 0$$$.

    Lemma 4 is very important. Note how the minimum total penalty is not affected with what path you take. This simplify the problem drastically, for instance, you can choose to only move left/right for the horizontal move, and only move up/down for the vertical move of the path. Using 2D Prefix Sum, you can solve each scenario in $$$O(1)$$$ and this is an alternate solution. However, there is a simpler and more elegant solution.

    Denote cell $$$s$$$ as the most top-left cell, or cell $$$(1, 1)$$$. Note that $$$d(u, v) = d(u, s) + d(s, v) = d(s, v) - d(s, u)$$$, which basically turn this problem into single-source shortest path. It sounds dumb to move back to cell $$$(1, 1)$$$ first, but as proven in Lemma 4, the minimum total penalty won't change.

    Furthermore, using Lemma 4 again, we can precompute $$$d(s, u)$$$ easily by moving right first, then moving down. This can be easily done in a simple $$$O(NM)$$$ loop. Finally, each scenario can be answered in $$$O(1)$$$ using the precomputed $$$d(s, u)$$$. Funny enough, the final solution doesn't even contain any graph/shortest path algorithm.

    I hope this editorial version is clearer to follow.

    UPD1. Here is my code.

    Fun fact 1: you can use Lemma 2 to generate a random zero-cycle grid easily. This is how I generate the test cases.

    Fun fact 2: Originally, the problem doesn't have any query (only ask one scenario). However, I found Lemma 3 and Lemma 4 right before I submitted this problem. I found this transitivity property on the distance pretty interesting (I think I called it "vector addition" in the editorial) but unfortunately I can't find a better application than making it a "query" problem.