fcspartakm's blog

By fcspartakm, history, 11 years ago, translation, In English

610A — Pasha and Stick

If the given n is odd the answer is 0, because the perimeter of any rectangle is always even number.

If n is even the number of rectangles which can be construct equals to n / 4. If n is divisible by 4 we will count the square, which are deprecated, because we need to subtract 1 from the answer.

Asymptotic behavior — O(1).

610B — Vika and Squares

At first let's find the minimum in the given array and store it in the variable minimum. It is easy to understand, that we always can paint n * minimum squares. So we need to find such a minimum in the array before which staying the most number of elements, which more than the minimum. In the other words we need to find 2 minimums in the array which are farthest from each other (do not forget about cyclical of the array). If there is only one minumum we need to start paint from the color which stay in the array exactly after the minimum (do not forget about cyclical of the array too). It can be done with help of iterations from the left to the right. We need to store the position of the nearest minimum in the variable and update her and the answer when we meet the element which equals to minimum.

Asymptotic behavior — O(n), where n — the number of different colors.

610С — Harmony Analysis

Let's build the answer recursively. For k = 0 the answer is  - 1 or  + 1. Let we want to build the answer for some k > 0. At first let's build the answer for k - 1. As the answer for k let's take four copies of answer for k - 1 with inverting of values in last one. So we have some fractal with 2 × 2 base: 00, 01. Let's prove the correctness of such building by induction. Consider two vector from the top (down) half: they have zero scalar product in the left half and in the right, so the total scalar product is also equals to zero. Consider a vector from the top half and from the down: their scalar products in the left and the right halfs differs only in sign, so the total scalar product is also zero.

Note the answer is also is a matrix with element i, j equals to \texttt{+} if the number of one bits in number i|j is even.

Complexity O((2k)2).

610D — Vika and Segments

At first let's unite all segments which are in same verticals or horizontals. Now the answer to the problem is the sum of lengths of all segments subtract the number of intersections. Let's count the number of intersections. For this let's use the horizontal scan-line from the top to the bottom (is can be done with help of events — vertical segment is open, vertical segment is close and hadle horizontal segment) and in some data structure store the set of x-coordinates of the open segments. For example we can use Fenwick tree with precompression of the coordinates. Now for current horizontal segment we need to take the number of the opened vertical segmetns with value x in the range x1, x2, where x — vertical where the vertical segment are locating and x1, x2 — the x-coordinates of the current horizontal segment.

Asymptotic behavior: O(nlogn).

610E — Alphabet Permutations

Consider slow solution: for operations of the first type reassign all letters, for operations of the second type let's iterate over the symbols in s from left to right and maintain the pointer to the current position in alphabet permutation. Let's move the pointer cyclically in permutation until finding the current symbol from s. And move it one more time after that. Easy to see that the answer is one plus the number of cyclic movements. Actually the answer is also the number of pairs of adjacent symbols in s that the first one is not righter than the second one in permutation. So the answer depends only on values of cntij —- the number of adjacent symbols i and j.

To make solution faster let's maintain the segment tree with matrix cnt in each node. Also we need to store in vertex the symbol in the left end of segment and in the right end. To merge two vertices in the segment tree we should simply add the values in the left and in the right sons in the tree, and update the value for the right end of the left segment and the left end of the right segment.

Complexity: O(nk2 + mk2logn).

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

| Write comment?
»
11 years ago, hide # |
 
Vote: I like it +56 Vote: I do not like it

For C, the answer can be found easily if one already knows about Hadamard Matrix. :)

https://en.wikipedia.org/wiki/Hadamard_matrix

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

    Wow! I had never heard of that. Thanks for sharing!

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

    Thank you buddy

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

    I read that in my Analog Subject.I wished i had paid attention to that at that time :(

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

    Yes, I had not given the contest but it seems the problem has been taken from the september long challenge 2015 on codechef. For those who are actually interested to solve this problem with harder constraints, try this problem. (The problem may not seem actually same as the one given but once you mathematically or using program deduce the pattern in TERVEC, the problem becomes the same).

    Here is a link to the question https://www.codechef.com/SEPT15/problems/TERVEC

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

    It seems that both I and Hadamard thought the same...

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

    Actually, this is also the same diagram shown in iterative FFT (by the way, FFT and FWT are quite similar, only different in calculation step)

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

    Now I am curious, after reading this. What is the physical significance of a matrix's determinant? What does it signify about a matrix(I don't mean properties, I mean significance, eg: scalar product of force and displacement signifies the magnitude of work done in physics) .

    Does anyone know this? If it's a very basic question, then I guess I've forgotten my school stuff.

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

Nice! Thanks for the great contest and fast editorial! ;D

»
11 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Well, there seems to be a mistake in problem E's complexity. Shouldn't it be O(n * k^2 + (m * k^2)*logn)?

»
11 years ago, hide # |
← Rev. 4  
Vote: I like it -9 Vote: I do not like it

,

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

    Codeforces is very fast. It can process more than ~1e9 operation per second.

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

    I had the same problem .... i tried to hack similar O(n) solution of kishan197 but unsuccessful . :(

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

    Asymptotic Big-O notation can be inaccurate especially when the program is extremely simple. But yes, it is very vague whether the hack will succeed. Personally, I also lost 50 points because of that code, but I don't feel that bad, since I knew that there was a good chance of resulting O(n) solution to pass thanks to the powerful optimizations.

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

    Me too :(

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

If someone has a clean code for D, please share it. I could not implement the exact logic fast enough.

»
11 years ago, hide # |
 
Vote: I like it +33 Vote: I do not like it

D can be reduced to the classical union of rectangle areas problem

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

Can someone tell me where am wrong in this Div2 B problem : http://codeforces.me/contest/610/submission/15053920

»
11 years ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it
»
11 years ago, hide # |
← Rev. 2  
Vote: I like it +6 Vote: I do not like it

610E: Complexity: I think it was something wrong?

»
11 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

What are some possible alternative approaches for E? I see multiple solutions using <15 mb of memory, some of them even without segment tree — but with some sets instead.

»
11 years ago, hide # |
 
Vote: I like it -14 Vote: I do not like it

For C: "It's guaranteed that the answer always exists. "

If k == 0, there is no "scalar product" and there is no answer at all, but it seemed that k == 0 is checked and the expected output must be "+" or "*". Should we output "*" or "+" when there is no answer?

My WA submission

If the code print '*' or '+' when k == 0, it will be accepted.

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

    Well, because there are no pairs to produce a "scalar product" when k == 0, we should output any set of one vector. So '*' and '+' are fine.

    • »
      »
      »
      11 years ago, hide # ^ |
      ← Rev. 2  
      Vote: I like it -9 Vote: I do not like it

      But "It's guaranteed that the answer always exists. " When there is no anaswer, "0" can't be accepted but "*" and "+" can be accepted. Why? It would be fine if any other output can be accepted when there is no answer.

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

        I think that '0' is not accepted, because '0' isn't a vector in this problem. I mean, you cant print 'welfihwogiwe', because it's just not a vector, and you have to print a set of vectors.

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

          The rule: "It's guaranteed that the answer always exists. " Neither "0" nor "*" can be accepted since there is no solution.

          But if we accept any other output when k == 0, we does not break the rule though this is not necessary. "Any other output" is just a workaround to keep that rule without removing "k=0".

          I think it would be better if "k==0" can be removed.

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

            Why do you think there's no solution? There are two possible solutions — "*" and "+" :)

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

              I had thought that "scalar product" rule should be applied to any solutions then there must be no solution if there is only one vector.

              After I read the description again, I found that this rule should be used only if there are two vectors.

              I am sorry for the misunderstanding.

              Thank you all!

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

        When k=0 there exists a valid set of vectors that satisfy the orthogonal requirements, which is either '*' or '+'. So it is not a "no answer" situation.

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

          When k = 0, there is only one vector and there is no "scalar product". If we extend the definition, for example, we do scalar product with itself, we get 1*1 = (-1)*(-1) > 0. The only valid vector should be "0". But it is not the answer.

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

            The orthogonal requirement is that "any two vectors are orthogonal". If there exists only one vector, "any two vectors are orthogonal" still holds.

            • »
              »
              »
              »
              »
              »
              »
              11 years ago, hide # ^ |
              ← Rev. 2  
              Vote: I like it +8 Vote: I do not like it

              Thanks. I read the description again: "find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or  - 1 and any two vectors are orthogonal. "

              So if there are no two vectors, we can print any vector and this does not break the requirement about two vectors or more.

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

    I guess we were asked to output a set of basis. Even only one vector is ok.

  • »
    »
    11 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    • and + are both correct answers. Check the problem statement one more time.
    • »
      »
      »
      11 years ago, hide # ^ |
      ← Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      Thank you! I have checked the statement and found that "scalar product" should only be used when there are two vectors in the answers. Sorry for my carelessness.

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

      The contest is not about hiding solution in problem statements. k = 0 did not make a huge difference. I appreciate you have been providing many contests but not at the cost of poor and confusing problem statements.

      In problem A, end to end is mentioned right in the end in the output section and no where perimeter is used. Now tell me one reason why I would not think of area.

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

I'm wondering why the constrains was N <= 2*10^5. I had the same solution as the author but with lazy propagation so it has twice the memory which didn't fit in the memory limit.

I was just wondering if that was intentionally? :P

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

    I think the 512MB memory limit is quite sufficient for even a segment tree implementation that has redundant nodes and 10x10 attributes, lazy flag associated with each node.

    For 2*10^5, I guess maybe Codeforces machines are so fast now so that we need to set a higher computational workload to take down TLE solutions :)

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

    Actually my naive solutions works so fast that we have not any alternatives.

»
11 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

I just have to note that the English in this editorial is particularly bad, and I hope you would put a little more effort to translations, as most of this site's users can't speak Russian.

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

Can anyone pls point out what is wrong with this solution? Div 2B

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

I had a slightly different approach for B. We find the minimum element m and subtract it from each element of A[1...n]. Next, we find the length of longest continuous elements which are nonzero. Let this length be l . Then our answer is ~~~~~ ans = s1 — (s2 — l), where

s1 = sum of all elements in A[1...n] initially

s2 = sum of all elements in A[1...n] after subtracting m

We can find l using disjoint-set data structure . Here is my code~~~~~

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

please unblock problemset page

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

Can someone explain why some people use segment tree in problem D?

What are they storing there ?

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

I have a solution with sets and no segment trees for problem E. It takes O(n) memory and its complexity is O(qk^2). Here's the link if you're interested: 15058624

»
11 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

Explanation of problem C editorial is not clear :(

  • »
    »
    11 years ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it +7 Vote: I do not like it

    suppose, for 1 the initial matrix is

    11
    10
    

    so, for k=2 u just copy the solution for k=1 in 4 copies and set them into the matrix for k=2.. the matrix will be like

    1111
    1010
    1111 < 
    1010 < 
      ^^
    

    just flip the 4th part(marked by arrow). the final output will be

    1111
    1010
    1100  
    1001  
    

    do the same thing for next k....

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

D seems to be well known idea for some coders . Can anyone share some link to tutorials where i can learn the technique from . A little more detailed explanation may also help :)

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

Can somebody please give a better explanation of the soultion for problem B? I don't get it :-? . It seems simple but something tricks me into giving wrong solutions.

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

    Hello :) Yo can always iterate through whole circle M times, where M i the minimum of array (so M*N,where N is size of array). Then you have to find the largest piece of array without the minimum (and add this size to result) — there you go :)

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

I don't understand problem D, can anyone make it more clear? Does the problem just wants us to find the number of points all line segment covers? or, it needs to deal with area and square?

»
11 years ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

I am sorry, but the English version of this editorial needs some serious editing.

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

Do I the only one who use segment tree on D?QAQ

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

337B----I don't know why it's showing wrong answer at case number 7......the input is large so not being able to check it as well. did it the same way as the editorial. can any of you guys help me out? here is the link: https://ideone.com/hsCXCv

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

    same problem with my code too. answer for problem B test case 7 is 207723124205, but my code giving 207723124204. Answer is differ by 1. plz find tht 1.

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

      Generally speaking, I suggest to don't fix your solutions based on specific WA. You get much more experience trying to fix it in blind.

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

Coding D is a nightmare! 1. Merge segments 2. segment tree 3. line sweep + smaller stuff like compressing points.

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

So I have basically coded up everything necessary for D, but I still don't know an efficient way of merging the overlapping line segments. Is there some sort of standard way to do this?

  • »
    »
    11 years ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it +1 Vote: I do not like it

    Do you mean like merge horizontals with same Y?

    If so:

    1. Sort by (Y, X1)

    2. Iterate from 1 to N-1

    3. cur = line[i], last = line[i — 1]. If (cur.X1 <= last.X2) set last.X2 = cur.X2 and drop cur from list.

    Thats how I did. O(NlogN) for sort and O(N) for merge.

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

      Thank you! I considered this before hand, but for some reason thought it wouldn't work.

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

        Ooops, btw I messed it up a bit. Instead of:

        set last.X2 = cur.X2

        you need:

        set last.X2 = max(last.X2, cur.X2)

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

Can problem E be solved in $$$O((N + M)logN + MK^2)$$$ complexity.
Submission: 314447437

  • We maintain the frequency of adjacent pairs in string S say cnt[i][j] denotes frequency of adjacent characters i and j.

  • We can maintain a data structure storing intervals of same characters {L, R, char} whenever we make a type 1 query, we update the intervals accordingly, since each interval get inserted and deleted atmost once, amortized complexity is $$$O((N+M)logN)$$$

  • For type 2 queries: Since we know the values of cnt[i][j] for all adjacent pairs, we can use them to calculate the answer. answer += cnt[i][j] when occurence[i] >= occurence[j] in the permutation. Type 2 queries can be answered in $$$O(K^2)$$$ per query.