awoo's blog

By awoo, history, 3 years ago, translation, In English

1792A - GamingForces

Idea: BledDest

Tutorial
Solution (Neon)

1792B - Stand-up Comedian

Idea: BledDest

Tutorial
Solution (awoo)

1792C - Min Max Sort

Idea: BledDest

Tutorial
Solution (Neon)

1792D - Fixed Prefix Permutations

Idea: BledDest

Tutorial
Solution (awoo)

1792E - Divisors and Table

Idea: adedalic

Tutorial
Solution (adedalic)

1792F1 - Graph Coloring (easy version)

Idea: BledDest

Tutorial
Solution (BledDest)

1792F2 - Graph Coloring (hard version)

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +114
  • Vote: I do not like it

| Write comment?
»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Isn't it possible to have an O(n) solution for problem C (Min Max Sort)?

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    yeah, there's an easy $$$O(n)$$$ solution for problem C, involving using the array $$$pos[i]$$$, to get the position of index $$$i$$$ in the permutation, then start at the middle value and finding the $$$LIS$$$ to both side. My submission: 190340250

    • »
      »
      »
      15 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      could you please explain it in a bit more detail thank you

      • »
        »
        »
        »
        15 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        nibba that was 2 ducking years ago, how tf am I supposed to remember that

      • »
        »
        »
        »
        7 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        so basically think of it as a parenthesis

        first we know that the best operation is to choose the ith element and the n-i th element

        now let's mark them with their position and on a new array and sort them

        now all you need to check is starting from the middle now you need to check upto what does the parenthessis extend

        for 1 5 4 2 3 we have [1,0] , [2,3] , [3,4], [4,2] , [5,1]

        now starting from the middle we check how many are already in place for us so we dont swap them just thinking of them like this will make thing this easier

        (ik it's been 8 month ago)

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it +13 Vote: I do not like it

    maybe you read only the last line.

    read the third last line.

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can somebody explain how to come up with the equation in problem B.

  • »
    »
    3 years ago, hide # ^ |
    Rev. 3  
    Vote: I like it +10 Vote: I do not like it

    Here, the Optimal strategy to solve the problem is by first saying all the jokes that both Alice and bob likes. (type 1 jokes) then by saying jokes alternately that (Alice likes,bob doesn't) and vice versa.

    Equation: a1 + min(a2,a3) x 2
    

    Final strategy is to say remaining jokes : first by saying remaining type 2, type 3 jokes and then by saying the jokes that both doesn't like (Type 4). this should be compared with the points that he already acquired hence a1 is taken. We take a1 + 1 as the judges goes when points become negative. Hence we add 1 to make it -1. Finally, We take the minimum of both these values to form the final equation.

    Equation: a1 + min(a2,a3).2 + min(a1 + 1,abs(a2 - a3) + a4)
    
»
3 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Does anyone have an idea on proving the efficiency of bisearch-on-divisors approach for prob. E? Never expected it will be this fast = =

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can somebody explain what is the meaning of run binary search on k in problem C tutorial?

»
3 years ago, hide # |
Rev. 2  
Vote: I like it +62 Vote: I do not like it

For whatever it's worth, here's my $$$O(n \log n)$$$ solution for problem F.

First, we notice that the property for the set $$$S$$$ is the same as having a one-colored cut in $$$S$$$ (in other words, the set of vertices can be split into $$$S_1$$$ and $$$S_2$$$ so that all edges between $$$S_1$$$ and $$$S_2$$$ have the same color). The only issue is that a given set $$$S$$$ can possibly be split into $$$S_1$$$ and $$$S_2$$$ in several ways. So I introduce $$$f(n)$$$ to be equal to the number of graphs on $$$n$$$ vertices where the global cut is blue. Then the answer is $$$2 f(n) - 2$$$, since the global cut may be red, but $$$2$$$ extra cases arise when all edges have the same color.

To find $$$f(n)$$$ we need to partition the set of $$$n$$$ vertices into $$$k \geq 2$$$ subsets so that vertices from different subsets are connected by blue edges, but the subsets themselves obey the rules recursively (they have global cuts of red edges). Since $$$f_{\text{red}} = f_{\text{blue}}$$$, we have

$$$ f(n) = \sum\limits_{k \geq 2}~ \sum\limits_{a_1 + \ldots + a_k = n} C(a_1, \ldots, a_k) \cdot f(a_1) \cdot \ldots \cdot f(a_k),$$$

where $$$C(a_1, \ldots, a_k)$$$ roughly means the number of ways to choose subsets of sizes $$$a_1, \ldots, a_k$$$ from a set of size $$$n = a_1 + \ldots + a_k$$$. If $$$b_1$$$ occurs $$$c_1$$$ times, $$$\ldots$$$, $$$b_m$$$ occurs $$$c_m$$$ times within the multiset $$${ a_1, \ldots, a_k}$$$, then

$$$C(a_1, \ldots, a_k) = \frac{n!}{a_1! \cdot \ldots \cdot a_k! \cdot c_1! \cdot \ldots \cdot c_m!}.$$$

Define $h(n) = \frac{f(n)}{n!}.$ The base values are $$$h(0) = 0, h(1) = 1$$$. Via $$$H(x)$$$ we denote the generating function of $$$h$$$: $$$H(x) = h(0) + h(1) \cdot x + h(2) \cdot x^2 + \ldots$$$. After a careful examination (I don't know how to prove rigorously it though) we obtain

$$$ e^{H(x)} = 2H(x) + 1 - x.$$$

Everything else is the standard approach of how to solve these recurrences: if we know $$$H(x) \bmod{x^{m}}$$$, then from the equality above we can obtain $$$H(x) \bmod{x^{2m}}$$$. Underneath we need FFT (NTT) and an exponential generating function. Each step from $$$m$$$ to $$$2m$$$ takes $$$O(m \log m)$$$ time, so the overall complexity if $$$O(n \log n)$$$.

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I don't get why we have to make sure that no set of vertices is connected by both colors in F1. Doesn't the lemma proved it impossible to connect a set of vertices with both colors?

»
3 years ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

A-F1 video editorial for Chinese:

BiliBili

»
3 years ago, hide # |
Rev. 3  
Vote: I like it +18 Vote: I do not like it

In fact, we can calculate the convolution-like sequences (such as those in problem F) in $$$O(n \log^2 n)$$$ or $$$O(n \log^2 n / \log \log n)$$$ or even faster. One can find the approach from Elegia's report. The implementations usually has lower constant factor in time than those of the Newton iteration (if exist).

Here is my implementation, which imitates Elegia's implementations for other problems.

(but it's just as fast as a brute force lol)

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Do you have this report as a PDF?

    • »
      »
      »
      3 years ago, hide # ^ |
       
      Vote: I like it +24 Vote: I do not like it
      • »
        »
        »
        »
        3 years ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        Do you know how to find ref. [5] (罗煜翔。(2020)。浅谈 Nimber 和多项式算法。IOI2020 中国国家集训队论⽂集。) ? I wanna see how to do semi-relaxed multiplication in $$$ \frac{n\log ^2 n}{\log \log n} $$$(if that's what it says). And what nimbers have to do with it xd.

        • »
          »
          »
          »
          »
          3 years ago, hide # ^ |
           
          Vote: I like it +8 Vote: I do not like it

          Let's discuss about it here, actually in that report, most contents are just putting the general algorithm into the nimber framework. Nimber does not play an important role in the relaxed convolution.

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

"The array may large" in E made me realise how I don't trust my gut feeling at all.

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Where does the log(divs(m)) of the complexity in question E come from Isn't it only need O(divs(m)⋅z(m)) to calculate the dp array?

»
3 years ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

can somebody explain why in D if we use lower_bound ans EITHER result or the previous one? why not just result? thanks in advance

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    It depends on the value immediately after the longest common prefix. While searching for $$$p$$$, you find some inverse $$$q_1, q_2, \dots, q_m$$$ that starts with $$$p_1, p_2, \dots, p_k$$$ for some $$$k$$$. The next value is different. If $$$q_{k+1} \gt p_{k+1}$$$, then lower_bound will point at $$$q$$$ (or one of inverses with such prefix if there are multiple). However, if $$$q_{k+1} \lt p_{k+1}$$$, then $$$q$$$ will be smaller than $$$p$$$, and lower_bound will jump over it. The next inverse has to be greater than $$$p$$$, so you only have to look one step behind.

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For D, I thought while $$$n=5\times 10^4$$$, the number of subsets of $$$\{1,...,m\}$$$ is $$$10^3$$$.

So I constructed a map $$$M$$$ mapping each subset $$$L\subset \{1,...,m\}$$$ to $$$\{(a_i[p_1], ..., a_i[p_t])\ |\ i\in \{1,...,n\},\ \{p_1 \lt ... \lt p_t\}=L\}$$$. Then I iterated over every permutation and every $$$k$$$ to see whether the corresponding $$$k$$$ locations have the desired values.

For constructing $$$M$$$, it seemed that I can iterate over all subsets of $$$\{1,...,m\}$$$ and for each subset iterate over all permutations. This 2-layered loop is about $$$5\times 10^7$$$. But my actual implementation requires $$$O(2^m\times n\times m\times \log(n))\approx 8\times 10^9$$$ and thus got TLE. Not sure whether this is optimizable.

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I have some questions in problem F1

  1. Why iterate k — the number of vertices whick are in the same 'red' component as 1 but not iterate the blue component, is this because you are counting the blue component?

  2. What about the edges between vertices in the same componet as 1 and the rest vertices, are they must be the same color? how to proof?

Hope someone can help me, thx a lot

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it
    1. We are considering the case when the whole graph is blue-connected, so there's no need to iterate on the blue component. The case when the graph is red-connected is symmetric to it.
    2. It's easy to see that all these edges are blue, since any red edge between any vertex from the "red" component of vertex 1 and any vertex outside this component means that we haven't picked the whole component
»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone give a test case on which my code is failing? I couldn't view the 156th item of 2nd test case. Is there any way to view it?

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

has anyone solved problem C with binary search ?

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    Code
»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

NEED PROVEMENT FOR PROBLEM F

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

F1's solution is flawed I guess. Consider case with n=3, when A1=B1=1, A2=2, B2=1, you get B3=4 which is incorrect.

  • »
    »
    3 years ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    So what's wrong with the $$$B_3=4$$$?

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    Sorry, I initially stated the problem without the constraints "at least one edge should be red" and "at least one edge should be blue". I wrote the editorial for that version, but then decided to introduce these constraints. So, the actual answer is $$$A_n-2$$$ since we need to discard the case "all edges are red" and the case "all edges are blue".

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Time for problem E should be more strict. The following brute force solution passes:

Generate an array of increasing divisors. Make a copy and maintain it in std::set<long long>. For each divisor $$$d_i$$$ in the original array, find the number of divisors that has $$$d_i$$$ as its first row, i.e. start from divs_set.lower_bound(d_i * d_i) and iterate until the value is greater than $$$d_i * n$$$, counting the number of divisors that are a multiple of $$$d_i$$$, and remove them from the set afterwards.

»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone give some intuition why way 2 in E works

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In editorial of problem E,it is mentioned that " There is also a way to get rid of extra log(divs(m)) factor if you iterate through dp in a smart way ".

How to do so? Could not find anything in comments and figure out neither.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

is it possible to solve problem 'D' using trie?

»
5 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

In the explanation for Problem C. It says in the third paragraph:

"There are several ways to check, let's consider one of them. Note that if the segment [k,n−k+1] is sorted for some value k, then it will be sorted for large values as well. So we can start with the maximum value of k (which is equal to ⌊n+12⌋) and decrease it until the segment remains sorted. Now for each k we need only two checks that posk < posk+1 and posn−k+1 > posn−(k+1)+1, where posi is the position of the element i in the permutation."

Note "until the segment remains sorted". I believe it should be "until the segment is no longer sorted"

because if it is not sorted for larger values of k i.e, a smaller, inner segment, then it is not sorted for smaller values of k a larger outer segment

e.g.

goal: 1 2 3 4 5 6

problem case: 2 3 1 6 4 5;

let p(x) be the position of x in the problem case Our max operations = n/2 -> 3

It works for p(3) < p(4), p(4) > p(3), max_op = 2 it works for p(2) < p(3), p(5) > p(4), max_op = 1 but p(1) < p(2) breaks hence, max_op remains 1

we need only one.

This was my implementation which you can find here in the solve() function:

https://mirror.codeforces.com/contest/1792/submission/348216142

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can anyone explain why there is no comment on existence of an inverse for every q in the given set of permutations q, in question D? I know it is true because the multiplication is closed, associative and identity exists but is it required to mention in the solution or not?

  • »
    »
    7 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    A permutation is a bijection (i.e. it is one-to-one and onto) and so it must be invertible as all bijections have an inverse. I suppose the editorial solution takes this for granted and it might be worth mentioning, however, I assume this would be relatively common knowledge.

    If you are interested in proving this yourself, the easiest proof off the top of my head would be showing every bijection has an inverse and that a permutation is a bijection.

    (I understand this comment and editorial is quite old now but I'm just leaving this here for anyone else in the future who are curious about this)