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

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

1898A - Milica and String

Author: n0sk1ll

Hint
Solution
Bonus

1898B - Milena and Admirer

Author: OutrunMyGun & n0sk1ll

Hint
Solution
Bonus

1898C - Colorful Grid

Author: n0sk1ll

Hint
Solution
Bonus

1898D - Absolute Beauty

Author: n0sk1ll

Hint
Solution
Bonus

1898E - Sofia and Strings

Author: OutrunMyGun

Hint
Solution
Bonus

1898F - Vova Escapes the Matrix

Author: n0sk1ll

Hint
Solution
Bonus
Разбор задач Codeforces Round 910 (Div. 2)
  • Проголосовать: нравится
  • +124
  • Проголосовать: не нравится

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

Lightning speed editorial!

Thanks for the greatly balanced round !

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

Can't say it's a good competition

P.S. Why is there no implementation?

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

Hello! Can someone explain the bug in my code at this submission? I really can't find it after spending hours on it and it's really annoying me. Thanks!

233462452

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

Delete

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

Thanks for preparing the editorial beforehand! I'm seeing a pattern of this, hopefully it will be the norm soon.

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

E = obvious treap: 233471686

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

problem B only has around 2500-2600 solves accepted solves. Div2B shouldnt have this low solves. I didnt like this round.

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

B problem -- SEE HERE

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

[redacted]

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

why B is too hard?

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

Will you add solutions in Russian?

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

Fun fact: we wanted put this D(but with minimisation) to our div.3 as F, but tester have seen it before and we removed it :)

n0sk1ll OutrunMyGun

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

Really like the idea behind the Problem D. There was similar problem in the contest before, back then it was impossible to figure out by myself, but now I got it.

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

When I registered for this round, I thought I was registering for Div 2, not Educational

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

Why is this editorial downvoted? Is it because problem B is repeated (from what I've read in some comments)?

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

A < C < E < D < B

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

.

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

.

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

what a fantastic well-made problems ?? I'm really impressed and horribly depressed ....

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

why this is wrong for the first test case in problem C ?

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

    Better question, why do you think it is correct? Can you outline the path of length 11 from the top-left corner to the bottom-right corner? Personally, I'm not able to see such a path. The closest I can see travels the left edge and then the bottom edge, and then takes a detour near the end, but the colors on the last two edges do not allow for such a detour. Can you elaborate on what path you had in mind?

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

Very balanced problems! (sarcasm)

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

didn't my favorite one.

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

im dumb and i apologize

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

B appeared as a subproblem in a different problem (https://mirror.codeforces.com/contest/1603/problem/C), and n0sk1ll even solved it during the contest (https://mirror.codeforces.com/contest/1603/submission/133683209).

Any comments from the authors?

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

I am very sad to find B in this contest is exactly same to this 2366. Minimum Replacements to Sort the Array ,a problem in LeetCode.And there are many people having Accept it but I haven't

What makes me even sadder is that I spent too much time on question B, which caused me to AC on question D one minute after the competition.

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

    ".And there are many people having Accept it but I haven't" not because you were unable to solve it means everybody who got AC had already seen the problem besides B is not that hard if you are specialist you gotta solve it iwas gray before this contest and i have solved it, besides the fact that it lost youu a lot of time is because of your bad time management

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

I'm gonna be honest, C was excruciatingly painful. The idea was simple but the implementation was awful.

I feel like a much more natural way to do D is as follows:

Let $$$S = \sum_\limits{i=1}^n |a_i - b_i|$$$, then a swap of two elements of $$$b$$$ at indices $$$i,j$$$ is equivalent to $$$S \to S + |a_j - b_i| + |a_i - b_j| - |a_i - b_i| - |a_j - b_j|$$$

$$$S$$$ is clearly invariant, so to maximise $$$S + |a_j - b_i| + |a_i - b_j| - |a_i - b_i| - |a_j - b_j|$$$, it suffices to maximise $$$|a_j - b_i| + |a_i - b_j| - |a_i - b_i| - |a_j - b_j|$$$.

We can iterate over $$$i$$$ and find the best $$$j \lt i$$$ that maximises the above, and then take max over all $$$i$$$. If we are at index $$$i$$$, then $$$|a_j - b_i| + |a_i - b_j| - |a_i - b_i| - |a_j - b_j| = |a_j - B| + |b_j - A| - C - |a_j - b_j|$$$, where $$$A,B,C$$$ are known "constants".

Then, we just need to maximise $$$|a_j - B| + |b_j - A| - |a_j - b_j|$$$. The way I thought to do this was to just use the fact that $$$x \leqslant |x|$$$, so

$$$(a_j - B) + (b_j - A) - |a_j - b_j| \leqslant |a_j - B| + |b_j - A| - |a_j - b_j|$$$

$$$(a_j - B) + (-b_j + A) - |a_j - b_j| \leqslant |a_j - B| + |b_j - A| - |a_j - b_j|$$$

$$$(-a_j + B) + (b_j - A) - |a_j - b_j| \leqslant |a_j - B| + |b_j - A| - |a_j - b_j|$$$

$$$(-a_j + B) + (-b_j + A) - |a_j - b_j| \leqslant |a_j - B| + |b_j - A| - |a_j - b_j|$$$

So I made 4 arrays that stored each type of modified element, then just took the cumulative max up to index $$$i-1$$$ (i.e. $$$\max(\text{arr}[0:i-1])$$$) of all 4 arrays at each index to find the best configuration, and added in the constants later.

233479793

(I don't think this idea works for the min variant, i.e. minimise the score when swapped)

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

im dumb and i apologize

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

Your solution for 1898C — Colorful Grid does not work for the edge case m=2, k=(m-1+n-1)+2

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

The C question was retarded. If you keep an implementation question try to keep it so that atleast some good pattern shows up.

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

Why so many dislikes? Problems are hard but good.

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

Problem B and D is repeated.Also a poor distribution of problems

I have a question. What did you learn from this contest guys?

Please encourage people to solve problems.

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

    I learned a lot from C. Mainly because I thought it would be smart to use two 2-dimensional arrays to calculate the solution and then output them.

    Only using one 2-dimensional array is a lot easier though. Easier to debug, easier to fill and not harder to output.

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

E could have been better, a little bit too detailed sample testcases for a div2E.

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

Can someone tell me why I got 'Idleness limit exceeded' in my submission? I couldn't get it. Thanks!

233470642

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

Problem E is very similar to 1187D - Subarray Sorting, the idea behind both problems are just the same, E just need one more not-so-hard-to-see observation. I just added a few lines of code and got AC: 233489174, 233489126

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

If you don't want to invent a new type of BFS for F but just want to use a normal BFS as a black box, you can still do it using a cool idea: we want to choose two exits and find distances from them to all points in the greed. Let's split all exits randomly into two types and run two different BFSs from the first exit and from the second exit. With probability $$$1/2$$$ our desired pair of exits will be in different groups, so we will find the answer. By repeating this process $$$T$$$ times for some constant $$$T$$$ we may bring the error probability down to $$$1/2^T$$$.

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

When submitting code, I observed that the speed of C's spj is very slow. Although the construction of C is indeed not difficult, I want to know how C's spj checks whether the construction scheme is correct. Does it find all the cycles? Thanks, n0sk1ll.

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

    I am also interested in the bonus for Problem C (how to implement the checker for C).

    My initial idea is to split each node into two ones (red and blue, representing the color of the last edge traversed when reaching that node).

    The problem would then be transformed into: how to determine if there exists a path of length l between two specified points in a cyclic undirected graph. However, I haven't found a straightforward solution to solve this problem.

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

    2 years late again but for anyone practicing past problems I think the answer is to think of it as a graph and then do matrix exponentiation / binary jumping DP.

    There are 512 states: you are at position (i, j) and must take either R or B edge next.

    Then if you represent it as a 512x512 matrix, you just have to do log(k) ~= 30 matrix multiplications. With bitset/AVX I think you can get this down to $$$O(\frac{n^3 \log k}{256})$$$ or $$$O(\frac{n^3 \log k}{64})$$$ which is approximately 16 to 64 million operations per test (but x 32 tests as well).

    There may be some more constant factor improvements. For example, if you take 2 moves at a time (solve twice for both first-moves if k is odd), then there are only 128 states, since you can only get to half of the points, and you'll always be in the same color. The runtime would then be improved by 32x (2x worse for two solves, but 64x speedup by reducing states by 4x).

    I also think a complete-search style solution like this one is the best you can do. While it feels like the graph / finding cycles structure might make things easier, I can also imagine inputs with cycles chained together where you kind of have to solve a subset sum problem to get exactly k when you reach the end where you can't do more clever tricks.

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

Solution for D

Code

submission 233513167

Can someone help with the proof of this solution!

i generalized all cases as A1<=B2 and A2>=B1

after swapping the contribution is(b2 - a1) + (a2 - b1) - abs(a1-b1) - abs(a2-b2)

= b2+a2-abs(a2-b2) - (b1+a1+abs(a1-b1))

MAX( b2+a2-abs(a2-b2)) - MIN(b1+a1+abs(a1-b1)) => ans

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

There is a no brainer solution for problem D. We can use the idea from https://www.geeksforgeeks.org/maximum-value-arri-arrj-j/.

My submission:https://mirror.codeforces.com/contest/1898/submission/233490749

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

This contest stole some problem. It is stupid and should be unrated.

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

Solution to bonus A: Only 1 operation.

Let numb be the number of letters of B in string S

Case1: if numb = k -> 0 operations needed

Case2: if numb < k -> 1 operation, iterate over every position in string, if A -> B, numb + 1 until numb = k

Case2: if numb > k -> 1 operation, iterate over string and any letter B change to A and numb-- until numb = k

Maximum 1 operation

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

Will be russian translations?

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

n0sk1ll, can you please tell why in 1898F - Vova Escapes the Matrix, it will always be optimal to find shortest path to 2 exit points in the 3rd type matrix??

Because , cannot there be a such construction of matrix where the shortest path to 2 closest exit points can flow the path in such a way that later on we will not be able to put maximum obstacles in remaining blocks ??

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

    We will fill all the cells that are not part of at least 1 path. Therefore, we need the least amount of cells empty, but they need to form at least 2 exit paths.

    I might have not understood your question correctly, if that is the case, please let me know.

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

      NemanjaSo2005, thanks for the reply.

      But, what I am asking is that how we are so sure that the shortest path to 2 closest exist points will be optimal.

      Because I am thinking cannot there be such a matrix which is constructed such that , in reaching those closest points you got to fill the maximum cells in the grid later and that will not be optimal then.

      And it can be rephrased also in the other way that " Cannot there be a path to some not closest points that can make the final answer optimal.(because in that unique construction of matrix the path to that not closest path is not very concentrated with initial obstacles compared to the path to closest exit points" I.e. will be able to fill out maximum cells with obstacle.

      Can there such a case exist ? Because greedy have to be true for all the possible cases that's why I asked the doubt ?

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

Well B is just a leetcode problem.

Link

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

Thanks for the fast editorial.

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

greedy algorithm is so hard

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

How to solve F's Bonus? The graph truly look like a LCA problem but I cannot find a way to construct the tree. I have tried to construct the tree by the distance from the start point, but i failed. Cound someone give me a idea? It's better to use the following graph as an example.

7 5
#####
#.V.#
#.#.#
#.#.#
#.#.#
#...#
#.#.#

Sorry for poor English expression.

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

Where can i attempt bonus problem??

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

C, i am getting this error on testcase 1:wrong answer Token parameter [name=answer] equals to "R", doesn't correspond to pattern "[Yy][Ee][Ss]|[Nn][Oo]" (test case 5)

my answer for 5th test case which is 4 4 8 is ~~~~~

YES R B R R R R B B B B B B B B B B B B B R B B R B ~~~~~

there's a path which follows. -> -> -> v v <- v ->

233730117

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

Any idea to solve the bonus for problem B? Thanks in advance!

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

ELDRVD and nosk1ll , would you please tell how to solve bonus part of problem B?

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

I think for the bonus part of problem C , We can use meet in the middle concept.

K-(n+m) is odd or negative then no solution exit.

k-(n+m) is multiple of 6 then k = n+m+6

k-(n+m) is multiple of 4 then k = n+m+4

k-(n+m) is multiple of 2 then k = n+m+2

Now k can be at max 38.

Use meet in the middle concept here. Find number of nodes({point,last edge as red or blue}) where it can end starting from (1,1) and (n,m). And then take intersection. If null color pattern is bad, else it is good enough is have atleast one path.

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

how to solve bonus problem E?

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

Can someone intuitively explain why it's okay to swap a[i] and b[i] in problem D? I did some casework and am convinced that it is correct, but I can't wrap my head around it intuitively.

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

I have no idea why my solution 241233386 doesn't work for 1898D - Absolute Beauty. Please point out the issue. Thank you!

[Explanation] Let $$$S = \sum{|a_i - b_i|}$$$. This represents the initial absolute beauty. Let $$$S(i, j)$$$ denote the beauty after swapping elements at indices $$$i$$$ and $$$j$$$. We can express $$$S(i, j) = S + \delta(i, j)$$$, where

$$$\delta(i, j) = |a_i - b_j| + |a_j - b_i| - |a_i - b_i| - |a_j - b_j|.$$$

Hence, it suffices to maximize $$$\delta(i, j)$$$. Now, fixing index $$$i$$$ and considering $$$\delta(i, j)$$$ as a function of $$$j$$$, we can identify four possible expressions after removing absolute value brackets:

$$$\delta(i, j) = \alpha_1 a_j + \beta_1,$$$
$$$\delta(i, j) = \alpha_2 b_j + \beta_2,$$$
$$$\delta(i, j) = \alpha_3 (a_j + b_j) + \beta_3,$$$
$$$\delta(i, j) = \alpha_4 (a_j - b_j) + \beta_4,$$$

where $$$a_k$$$ and $$$b_k$$$ are constants. Each expression can be regarded as a linear function of the variables $$$a_j$$$, $$$b_j$$$, $$$a_j + b_j$$$, and $$$a_j - b_j$$$, respectively. Since a linear function within an interval takes its maximum at either endpoint, the candidates for $$$(a_j, b_j)$$$ to maximize $$$\delta(i, j)$$$ are at most 8. (Candidates are to maximize $$$a_j$$$, $$$b_j$$$, $$$a_j + b_j$$$, or $$$a_j - b_j$$$, or to minimize one of them). These candidates remain invariant even when iterating over $$$i$$$. Therefore, it is sufficient to iterate over $$$i$$$, check $$$8N$$$ pairs of $$$(i, j)$$$, and calculate $$$\delta(i, j)$$$ straightforwardly.

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

anyone can give hint for bonus problem in B

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

OutrunMyGun I think you wanted to mention $$$s_{i_2}$$$ instead of $$$t_{i_2}$$$ in the above editorial of the question 1898E - Sofia and Strings. The editorial says

We need to prove that if we can move $$${s_{j_1}}$$$ to position $$$i_1$$$ and $$$t_{i_2}$$$ to position $$$i_2$$$, when we can move $$$s_{j_2}$$$ to $$$i_1$$$ and $$$s_{j_1}$$$ to $$$i_2$$$

According to me, it should be-

We need to prove that if we can move $$${s_{j_1}}$$$ to position $$$i_1$$$ and $$$s_{i_2}$$$ to position $$$i_2$$$, when we can move $$$s_{j_2}$$$ to $$$i_1$$$ and $$$s_{j_1}$$$ to $$$i_2$$$.