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

Автор gen, история, 7 лет назад, По-английски
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Разбор задач Codeforces Round 599 (Div. 1)
Разбор задач Codeforces Round 599 (Div. 2)
  • Проголосовать: нравится
  • +62
  • Проголосовать: не нравится

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

Isn't div1 B a weaker version of 920E - Connected Components??

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

It seems that the data is so weak.

I used std::map<int,int> in Div1. C but it passed the system test.

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

I suspect 7500 in div1 C editorial is a typo of 75000.

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

Hi, my implementation for Div2 B2 is O(N^2).

This is how my algorithm works: If s[i] != t[i], I used an inner loop (with index j=i+1) to find s[i] == s[j] or s[i] == s[j].

However, the editorial states that the complexity is O(N). Is my analysis for the complexity wrong? Or is there a more efficient way of implementation?

Thanks!

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

The system test for 1C is too weak.

sys. hacked lots of submissions after contest.

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

I think the editorial of div 2 B2 is incomplete.

  1. There is no explanation on how to efficiently find characters in the strings. My implementation (64439067) used TreeSets, which pushes the complexity up to $$$O(n \log n)$$$. I think it can be done in $$$O(n)$$$ using arrays/vectors and pointers but the implementation is non-trivial. (Edit: Apparently it doesn't matter too much though cause $$$n \leq 50$$$)

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

Can someone please explain the time complexity in Div2C Editorial

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

    It is not similar to this problem, during contest i also thought it is similar to this problem but after making required change it kept giving me wrong answer on pretest then again after some modification i passed pretest but it fail on main test.

    And the reason behind that in Div1B is we can have the edge in its own vertex set as well that differ the complete approach. In 1228-D it is strict that we won't have edge in own vertex set but that is not the case here

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

for problem C in div1,15·5000=7500???

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

Can someone please explain the case of n=pq for 1242A Tile Painting

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

Can people who submitted "gcd of all divisors except 1" in div.2 C describe their way of thinking? I know that it works, you can prove it with editorial's proof, but to me it makes no sense and seems like a random guess.

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

    Think about it in terms of Bezout's Identity.

    Let us take two integers $$$x$$$ and $$$y$$$.

    Suppose, $$$n = Ax + By$$$

    Let $$$g = \gcd(x, y)$$$

    $$$n = Agx' + Bgy' = g(Ax' + By')$$$

    This means that we can write $$$n$$$ as a linear combination of $$$x$$$ and $$$y$$$ if $$$g$$$ divides $$$n$$$.

    Conversely, if we find a combination such that $$$Ax + By = g$$$, we can reach any multiple of $$$g$$$ too.

    Suppose, we want to reach $$$kg$$$, then $$$(kA)x + (kB)y = kg$$$

    Now, let us take $$$N = p_1 ^{a_1} p_2^{a_2} ... p_k^{a_k}$$$

    Now, gcd of any two primes is $$$1$$$. This means that if there are at least $$$2$$$ primes, we can reach all the integers as $$$A p_1 + B p_2$$$

    I suspect that this was the idea behind writing gcd of all the divisors. Suppose $$$g$$$ is the gcd, then we can paint $$$[1, g]$$$ with different colours but after that we cannot use any more colours.

    The editorial and the intended solution went one step further and noticed that we do not really need to know the GCD of all the divisors since $$$2$$$ primes are always coprime to each other and if there is only $$$1$$$ prime, then that prime number is the gcd.

    Let us generalise the question. Suppose we were given a set $$$ S = (a_1, a_2, \dots , a_n)$$$ and told that if $$$(i - j) \in S$$$, then $$$i$$$ and $$$j$$$ have to be the same colour, the solution is the gcd of $$$S$$$. We can reach any integer that is a multiple of the gcd of $$$S$$$. So we can only choose the colours in $$$[1, g]$$$. Whenever we colour $$$x$$$, the colour of $$$x + g$$$ is decided.

    However, this question has an additional step that $$$S$$$ is only the divisors of an integer so this allows us to make it sharper. :)

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

DIV 2B. if both strings are already same then answer is yes.

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

I had a different, possibly simpler solution to div1B.

All nodes with more than $$$\frac{n}{2}$$$ edges of zero cost are in the same zero-cost component. Loop all nodes with more than n/2 edges with connected to each other, so connect them in the DSU in O(n log n). Loop all nodes with at least $$$\frac{n}{2}$$$ edges with cost 1, and connect all nodes they have zero-cost edges to in total time O(m log n). Total complexity is O((m + n) log n).

Code: 64382712 (I use $$$\frac{n}{2} - 10$$$ here because I didn't want to think about the constants)

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

Div 1 B can be solved using Prim's also.

I keep a set of pairs,which are disconnected vertices , first entry will be number of edges from connected part to the vertex and select will be vertex number. and i keep a map from second entry to first entry of set.

Every time fetch the first element in set, if its degree equal no of connected nodes, increment answer by 1.then update map and set with edges of this node.

Here is my submission : https://mirror.codeforces.com/contest/1242/submission/64390575

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

Could you give me a intuition explanation for Chinese Remainder Theorem?

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

Well, I did the same thing, except that I determined, for every vertex i, any other vertex j that:

a. i isn't connected to j with a 1-weight edge,

b. i and j lie in different connected components.

I do that by initializing j to 0 and looping over the vertices incident to the current i, incrementing j if necessary.

However, this gives WA 13... :(

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

In div2 C, my implementation is: if n==1, ans is 1. Otherwise get the divisors of n. Get gcd of all the divisors except 1. Answer is the gcd. Here is my JAVA code: 64462563

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

My solution for div1B:

Let G1 be the graph where only edges with cost 1 are considered. Pick any vertex (S) with minimum degree.

Let X be the set of vertices which have 0 cost edge with S and Y be the set of vertices which has 1 cost edge with S. Note that the size(Y) <= m/n.

Build a new graph as follows:

  1. For each vertex in X add an edge to S (O(n))

  2. For each vertex in Y add an edge to all its 0 cost neighbours (O((m/n)*n))

Now, the answer is Number of connected components of this new graph -1.

My Submission

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

Can anybody explain Div2D in a more intuitive way? Like what is the key observation over here? With an example? I have read some of the comments explaining the solution but not how they got there.

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

    I solved it after reading the editorial and implemented each step as it is written, https://mirror.codeforces.com/contest/1242/submission/65618215.

    Key observation: 1. If number of 0-weight edges are less than<1e5. Then you would have made DSU and answer would #connected_components -1. (This is relatively simple and almost everyone would've got it. ). Tough part was total 0-weight edges could n^2 — m (~10^10). You need to build the same DSU in some other way.

    Implementation:

    map<int,int> m;
    for v in 1..n:
      for(int u: adj[v])
         m[root(u)]++;
    

    This map m will hold number of edges from component of u to v.

    for each component: 
        if (sz[component] > map[compnonent]) 
            unite(component, v);
    
    

    Catch is to how to iterate over all components?

    Wrong/sub-optimal Approaches:

    1. Iterate only for entries of map. This is wrong.
    1. Iterate over 1..v-1. This will TLE.

    Right one

    Mainatain a set for that. After the vth iteration, push v in it. All those components which got merged, remove them..

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

    101944622 This is the code implemented according to the editorial.

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

I solved Div2 D using the approach mentioned in the editorial,solution. However when I applied the same approach to this similar question 190E-Counter Attack, it is giving TLE although the time complexity is still the same,. Any help would be appreciated!

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

I found the typo of editorial of Div1D. It will be fixed soon.

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

    On the editorial says "Every interval has exactly one non-self number", it means that the non-self numbers are periodic $$$($$$if n is non-self the $$$n$$$ + $$$k^{2}$$$ + $$$1$$$ is also non-self $$$)$$$; however, with $$$k$$$ = $$$2$$$, the numbers are $$$3, 9, 13, 18,...$$$ and clearly there is not a period. Thanks in advance.

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

I solved div1B using the algorithm provided int the editorial. But I still wonder why the complexity is O(nlongn + m), as we iterate every vertice from 1 ~ n and every time we iterate every zero weight component to try to unite them, it seems that the complexity should be n^2. Are there anyone explain it for me? Thanks a lot!

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

    I will try to give you some intuition: Yes, you are right, for every vertex we are going over all the zero-weight-components, and you are right there can be N of them. But the complexity is not N^2, here's why: The number of zero-weight-components you can have at any given time is actually bounded by M/(N — 1), the number of edges. Because in order to leave a vertex unmerged, it needs to have a degree equal to N — 1. In other words, it needs to have an edge between it and every other node. This can happen at most M/(N — 1) times. Since M is actually min(10^5, N*(N — 1)/2), you can see that M/(N — 1) cannot exceed 10^3. And now, every node after that will have all of it's edges with weight 0, because we have already used all our weight 1 edges. So every subsequent node we consider will be merged.

    It's not very rigorous I know but I hope I could provide some intuition

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

Did anyone solved div2D exactly as explaind in editorial.

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

Could anybody help me with my code of Div1.C? It did output the correct answer when running with the first example input on my own computer, but I don't know why it just printed 0 when submitted.

Here's the local output:

Yes
10 4
5 1
7 2
2 3

Here's the online output:

Yes
10 0
5 0
7 0
2 0

I'm really confused about this. And here's my submission 64595444. Thanks a lot!

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

Can someone explain 0-1 MST solution with DSU? I understood the general idea that we should count the number of zero-weight components. The part that I couldn't get starts from "For each of the zero weight components, we count the number of edges from this component to v."

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

I did not understand the editorial for D. I understood the claim but did not quite understand the proof neither how to come up with such claim out of blue. Did anyone come up with the solution in some other way? Or any better explanation?

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

    I also think my sentence is bit messy.

    Suppose you have $$$k^2$$$ self numbers and $$$1$$$ non-self number in $$$k^2+1$$$ size interval. Then each $$$k$$$ numbers of $$$k^2$$$ self numbers will be appended to $$$s$$$ at the same time, and they will generate new $$$k$$$ non-self numbers in total. Each non-self numbers will be included in next interval($$$k \cdot i, k \cdot i + 1, \ldots , k \cdot i + k - 1$$$ th intervals), because each summation will be fit in corresponding interval as I described as formula in editorial.

    Now let's see this formula:

    $$$ \sum_{j=1}^{k} (i \cdot (k^2 + 1) + t \cdot k + j) + offset = (k \cdot i + t) \cdot (k^2 + 1) + \frac{k \cdot (k+1)}{2} - t + offset $$$

    $$$k$$$ numbers of $$$t$$$-th subinterval(size $$$k$$$) of $$$i$$$-th subinterval(size $$$k^2 + 1$$$) will be $$$(i \cdot (k^2 + 1) + t \cdot k + j) + r[j]$$$ for $$$j = 1, 2, \ldots, k$$$ where $$$r[j]$$$ is $$$0$$$ or $$$1$$$, because if non-self number is inserted at the left(or inside) of the $$$t$$$-th subinterval, then part(or whole) of the subinterval will be shifted, so that $$$r[j]$$$ is offset consideration, and $$$\sum_{j} r[j] = offset$$$.

    Since $$$0 \le t \lt k$$$ and $$$0 \le offset \le k$$$, this is always true: $$$1 \le \frac{k \cdot (k+1)}{2} - t + offset \le k^2 + 1$$$. This means generated sum of $$$t$$$-th subinterval will always be in $$$(k \cdot i + t)$$$-th interval. ($$$i$$$-th interval represents $$$[i \cdot (k^2+1) + 1, (i+1) \cdot (k^2+1)]$$$.)

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

I can not understand the solution of div2E-div1C until i read the problem statement again and realized that i did not read statement carefully before. I did not notice on the important condition "All of the integers are distinct".

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

for 1242B if we find the number of connected components of the given 1 weighted graph complement the problem is solved!

we can find the vertex v with minimum degree of the given 1 weighted graph.

Merge all vertices except ones adjacent to v into one component and make the root of them to v in the dsu.

the number of remaining vertices (adjacent to the v) can't be large!!

Let degree of v be d then d<n & n*d<=2m so d<=sqrt(m).

Now brute force for all zero edges between neighborhoods of v and the remaining vertices and complete the dsu.

complexity is o(n*sqrt(m)).

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

Div2E/Div1C: can we get more details about the graph we should construct?

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

Can someone please help me out in finding the reason for getting TLE for problem 1243D - 0-1 MST ??Here's my submission 64726030 ...According to me it's O(m)...

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

hello, could someone help me with the explanation of the problem D div2? Please.

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

In problem div2D ,

For each of the zero weight components, we count the number of edges from this component to v. If the number of such edges is less than the size of the component of u

Using DSU , how are we going to count the number of edges for each component, can someone help please?

Thanks in advance!

Edit : Found the code above. Sorry for the trouble!

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

can someone expalin me how answer is 1 for n = 6 in tile painting question??

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

can anyone please tell me why This submission is getting accepted while This is not? Thanks in advance

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

0-1 MST ( Div2 D ) , Prim's Algorithm Solution Without DSU
Time Complexity: O( (n * log n) + m )

Commented Code