4

CodeTON Round 3 (Div. 1 + Div. 2) Editorial

 1 year ago
source link: http://codeforces.com/blog/entry/108504
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A — Indirect Sort

Authors: mejiamejia, Ecrade_

Solution
Code (Ecrade_)
Rate Problem

B — Maximum Substring

Author: tibinyte

Solution
Code (tibinyte)
Rate Problem

C — Complementary XOR

Author: tibinyte

Solution
Code (tibinyte)
Rate Problem

D — Count GCD

Author: tibinyte

Solution
Code (tibinyte)
Rate Problem

E — Bracket Cost

Author: tibinyte

Solution
Code (tibinyte)
Rate Problem

F — Majority

Author: tibinyte

Solution
Code (tibinyte)
Rate Problem

Challenge: Solve the problem in or ( modulo is prime )

G — Doping

Author: tibinyte

Solution
Code (tibinyte)
Rate Problem

H — BinaryStringForces

Author: tibinyte

Solution
Code (tibinyte)
Rate Problem

#LeafTON

17 hours ago, # |

The Solution code is not revealed

17 hours ago, # |

tibinyte orz

17 hours ago, # |

omg green editorial

  • 16 hours ago, # ^ |

    Omg green editorial

    • 16 hours ago, # ^ |

      omg green editorial

      • 16 hours ago, # ^ |

        omg green editorial

        • 16 hours ago, # ^ |

          break the chain

    • 16 hours ago, # ^ |

      Omg green editorial

17 hours ago, # |

Rev. 2  

0

Great contest! Seems like the editorial was prepared before the contest :D
tibinyte orz

17 hours ago, # |

Rev. 2  

+64

I use divide and conquer to solve E. Assume that ( is and ) is . Let be the total balance of the string and let be the minimum prefix balance of the string. Then there are two main observations:

  1. Cost of making the string balanced is ;
  2. Concatenation of strings and yields .

Using this, we may just compute sum of and of for every sub-string separately by splitting a string in two equal halves, computing answers on them recursively and then computing answers for strings passing the middle point by sorting prefixes of right half by and by , and by using some prefix sums on them.

  • 16 hours ago, # ^ |

    Rev. 2  

    0

    can you explain more that how you got ans for merged string str1+str2 from answers of str1 and str2 and t1 t2 m1 m2 ,ok that answer of this string is |m| + max(0,t) but how to get answers of all substrings that pass from the concatenation point of str1 and str2

    • 16 hours ago, # ^ |

      Rev. 2  

      +4

      Balance of the concatenation is just the sum of their balances. And minimum prefix balance is either in the left half, in which case it's or in the right half, in which case it's . You just pick the minimum.

      To compute contribution, note and:

      1. Sort right half by ;
      2. For each possible , find first such that ;
      3. For elements to the right of it, contribution is and to the left it's .

      To compute contribution, note and:

      1. Sort right half by ;
      2. For each possible , find first such that ;
      3. For elements to the right of it, contribution is and to the left it's .

      Actual contributions then may be computed with prefix sums. My sol is 179624832. Now that I think about it, my complexity is actually because of sorting... Though one could've used counting sort instead.

      • 16 hours ago, # ^ |

        got you ORZzz!!!

17 hours ago, # |

Can anyone explain in problem D, how to use the inclusion-exclusion principle to find the count of numbers in range [1, m / a[i]] that are co-prime to a[i — 1] / a[i] ?

That's essentially what we are trying to do for each i, right? To find out the count of numbers that are co-prime to a given number x in a given range starting from 1.

  • 17 hours ago, # ^ |

    Rev. 4  

    +4

    Let the prime factors of a[i - 1]/a[i] be p1, p2, ..., pk. The goal is to compute the number of numbers in range [1, m / a[i]] such that they are not divisible by any of pi. To do this, let A_i be the set of numbers in range [1, m / a[i]] such that they are divisible by pi. We can apply PIE on all A_i to find the total number of in-range numbers that are divisible by some pi. m / a[i] minus this number is the desired result

    • 16 hours ago, # ^ |

      Thanks! I missed the point the total number of possible pi is small.....

  • 5 hours ago, # ^ |

    Video Solution for Problem D

    Hint:

17 hours ago, # |

Fast editorial? orz

17 hours ago, # |

First BinaryStringForces round on codeforces.com

  • 15 hours ago, # ^ |

    Rev. 2  

    +7

    I really couldn't make all the tasks about binary strings, but that was the intention...

    I was inspired by antontrygubO_o's xor contest idea on codechef.

17 hours ago, # |

Problem statements were short and quite simple to understand. No unnecessary stories. Thank you.

16 hours ago, # |

Quickest Tutorial Ever, WOW!

Good Job boys ..

16 hours ago, # |

Rev. 2  

+4

My solution for D is similar but instead of factorizing a1, I use the fact that a[i]/a[i+1] is amortized:

We notice that b[0] = a[0] and let's look for transition from b[i] to b[i+1]:

Exist a number b[i+1] s.t. it is possible to do the i -> i+1 transition <=> gcd(a[i], a[i+1]) = a[i+1]

b[i] must fulfill that gcd(a[i], b[i+1]) = a[i+1] <=> b[i+1] € {x s.t. gcd(x/a[i+1], a[i]/a[i+1]) = 1 and a[i+1]|x and x <= m}

Those are the number of co-primes numbers of a[i]/a[i+1] in range [0, (m/a[i+1])]

To get the number of co-primes number of a[i]/a[i+1] in range m/a[i], we can go through the factorization of a[i]/a[i+1], keep unique primes in a vector, let's call it "decomposition", and then go through all the 2^(|decomposition|) subsets with inclusion-exclusion to get the number of non-co-primes numbers of a[i]/a[i+1]. (we get the number of multiples of every subset of decomposition by (m/a[i]) / LCM(subset) and add it or subtract depending on the parity of the cardinal of the subset (this comes from inclusion-exclusion)).

Considering 2 sqrt(m)/logm the number of primes in [2, sqrt(m)] we get this is O(n*(2 sqrt(m)/logm) + sqrt(m)log(m)), but it turns out that a[i]/a[i+1] is amortized and then (2 sqrt(m)/logm) is a constant, so we get O(n + sqrt(m)logm).

Sorry for not using LaTex, I should definitely learn LaTex

16 hours ago, # |

Rev. 4  

0

In problem C if all elements of both arrays are equal to 1, the editorial solution will do 2*n operations , right?

wasn't it suppose to make at most n+5?

or am I mistaking something?

  • 16 hours ago, # ^ |

    nvm I'm dumb

    • 16 hours ago, # ^ |

      could you please c's logic i'm finding it hard to understand

      • 12 hours ago, # ^ |

        Notice that every operation can be inverted, so you just need to move to a case where it is trivial to check if it is possible

      • 10 hours ago, # ^ |

        Video Solution for Problem C.

        Hint:

16 hours ago, # |

Rev. 2  

0

I want to plug my solution for Problem E here which does not use a BIT, just prefix sum, a map (which couldve been a vector, but I was too lazy for negative indices...) and two for-loops.

Solution: 179633595

Explanation: Link to comment in Announcement.

16 hours ago, # |

can someone explain C logic .

  • 16 hours ago, # ^ |

    Hint1
    Hint2
    Tutorial
    Solution
    • 3 hours ago, # ^ |

      why you are doing l++ when flag1 is true

16 hours ago, # |

For problem D, I am generating the prime factors of all the numbers but it is not giving me a TLE.

Example:

Then isn't the time complexity x and won't this give TLE?

179686581

  • 16 hours ago, # ^ |

    Your testcase is wrong, because you check in the very beginning, that divides

    And since every next number divides the previous one, then you'll need to factor a number except 1 only at most times. So we can limit the complexity to . And an even better limit would come from the fact that the product of all the numbers we decompose is , and the fact that gives us the total complexity of

    • 16 hours ago, # ^ |

      Thanks for the clarification.

    • 12 hours ago, # ^ |

      This is not true, for example, for :

      • 12 hours ago, # ^ |

        Rev. 2  

        +5

        Well, you got me there
        It works only for numbers starting from 4
        I guess you wouldn't have a problem factoring numbers 1, 2 and 3 in

  • 16 hours ago, # ^ |

    The condition A[i-1] % A[i] != 0 handles that

  • 108 minutes ago, # ^ |

    Rev. 2  

    0

    I generated primes for the first element and going forward from a[i — 1] to a[i] deleted the primes that are not present in a[i]. But it was not needed.

16 hours ago, # |

Okay I'm a bit annoyed (and it certainly is my fault) that I FST'd Problem D and once again (yes, previous times also I was reaching Master and then FST'd) I have to wait to touch that yellow colour again.

What was the mistake?

At this point, it seems like my fate doesn't want me to reach master again :(

Anyways, it was a great contest! A big thank you to everyone involved in the problem setting team. (Although I believe C was a bit harder than usual :P)

  • 16 hours ago, # ^ |

    The comment is hidden because of too negative feedback, click here to view it
    • 16 hours ago, # ^ |

      I care!

      • 16 hours ago, # ^ |

        The comment is hidden because of too negative feedback, click here to view it
    • 16 hours ago, # ^ |

      i care too

      • 15 hours ago, # ^ |

        The comment is hidden because of too negative feedback, click here to view it
    • 15 hours ago, # ^ |

      I also care!

      • 15 hours ago, # ^ |

        No one cares.

        blue_princess

        • 5 hours ago, # ^ |

          I care!

          • 5 minutes ago, # ^ |

            No one cares.

            blue_priness

16 hours ago, # |

Is the runtime complexity of problem D wrong ? Not sure what is the log in 2^9 * 9 * log + sqrt(M). Shouldn't it be 2^9 * 9 * N + sqrt(M) per test case, where N is the length of the input array A's length per test case ?

16 hours ago, # |

Why this solution of the first problem didn't get accepted?? Solution: 179614121

  • 16 hours ago, # ^ |

    Because you have out-of-bounds in line
    int pos[n]; for(int i=0;i<n;i++) pos[a[i]] = i;

    That leads to undefined behaviour so program can output arbitrary data.

    • 4 hours ago, # ^ |

      Ohh that's why after deleting this line my code got accepted. Thank you.

16 hours ago, # |

Problems Were really nice :)

16 hours ago, # |

The links in your blogpost are a bit weird, since clicking on the caption "A -- Indirect Sort" sends you to Problem B :) But it is a great contest and editorial!

  • 16 hours ago, # ^ |

    when you find that ans is zero( v[i-1]/v[i] is not integer ) you can break,but if you don't the value of v[i-1]/v[i] keeps jumping ex 1 1000000000 1 1000000000 1 1000000000 1 1000000000... so so your actual complexity is now N*sqrt(m) instead of sqrt(m)

    • 16 hours ago, # ^ |

      Damnn I often don't break out of laziness basically. Terrible mistake. Thanks for your time!

15 hours ago, # |

Rev. 2  

+18

What was the point of setting non constant in problem F? I had a very funny bug in my non-static template. I forgot that the isn't always prime, so for calculating to the power of I took modulo . And due to the small constraints on I didn't find correctly and didn't get accepted ;)

Anyway the problem was good in my opinion, and would be better with the constant .

  • 15 hours ago, # ^ |

    Otherwise you'd be able to precalculate the answer and submit a solution with an array of size 5000.

  • 15 hours ago, # ^ |

    You can precalculate answer with some slow solution with constant

15 hours ago, # |

How to make observations such as "The cost of s[l + 1, r] is max(bl, br) — min(bl, ..., br)?"
Really bad at binary strings or balanced sequence stuff.. Can someone please share what is the intuition/strategy for those problems?

  • 15 hours ago, # ^ |

    I can share what I did, it may be a bit complicated.

    So, let's divide all of our sequences into two types of groups — positive and negative. (positive have balance > 0, negative — <= 0) For a bracket sequence b, let's call P the absolute value of the minimum prefix balance that is achieved in it (ex, for )(()) it is 1) I claim that the cost for making a permutation good is P for negative BS's and P + balance for positives. How to prove? Let us prove at the start that for a bracket sequence with balance = 0 the cost is P. Let's take first P opening brackets and rotate them to the start — that is the construction. And given that one operation increases a given prefix value by no more than 1, this proves that less than P is impossible. Now, for positive BS's you always need at least BAL closing brackets, which you simply append (the optimality of this is trivial), and then you need P rotations. For negatives — we simply assign the required opening brackets to the start and repeat the proof. Now, you can either simply code this approach (two ST's + deque, my submission = https://codeforces.com/contest/1750/submission/179686679), or you could reorder the formula some more and get the author's one.

15 hours ago, # |

A tough contest that ended my positive delta streak for 10 rounds, however it was well prepared and I learnt a lot, Thanks. Orz tibinyte

14 hours ago, # |

In editorial of C, "For each operation, the interval changed by a sequence and b sequence is complementary, so you must judge whether all [ai=bi] are the same at the beginning."

Can someone explain in more detail?

  • 13 hours ago, # ^ |

    Maybe this will be easier:

    You can see that this

    will be true after every operation, so if we make every , then will hold for every i and j

    • 4 hours ago, # ^ |

      Great idea!! I had never thought in this way...

    • 3 hours ago, # ^ |

      bashkort Can you explain how to further approach the solution after this observation?

      • 2 hours ago, # ^ |

        If initially there are i, j such as , then answer is clearly NO, because if we could make a's and b's equal to zero, then there were no i, j such as .

        After we made every , then every or every , so if b's are equal to zero, then we solved the problem, or if every b is equal to 1, then we do 3 operations:

        (1, 1), (2, 2), (1, 2)

14 hours ago, # |

Can someone explain the solution to the problem F properly?

  • 13 hours ago, # ^ |

    I updated some stuff in the editorial. Does it make more sense now ?

  • 12 hours ago, # ^ |

    Rev. 2  

    0

    Let be the number of sequences of size whose maximum electrifiable prefix is of size . For example, , corresponding to any sequence starting with , and is the solution to our problem. Also note that .

    To calculate for we use a recursive formula. Such a sequence is made by a electrifiable sequence of length followed by either all zeros or enough zeros then a one. In the second case, there are at least zeros before the one, where is the maximum electrifiable prefix of the subsegment starting at the one. Thus, if is the length of that subsegment, we have , where is any amount of extra zeros in that segment. For each satisfying we have a set of sequences of size . Since we can determine in terms of and (having fixed and ), the sets given by every pair are disjoint because they either have a different number of zeros before the one or a second fully electrifiable subsegment of different size.

    The condition is equivalent to . Thus, the formula is

    To compute the sum we can something similar to 2d prefix sums but for a triangle instead of a square.

    Code: Submission

    • 12 hours ago, # ^ |

      I really like the name "electrifiable" :))

  • 12 hours ago, # ^ |

    Rev. 4  

    +52

    If somebody still does't understand the solution for F, I hope this would be helpful.

    Let's fix a binary string (assuming that the first and the last elements are ones). And let's imagine that wile we can make the following operation we make it. Of course it's always good for us. Then let's look to the final string:

    It looks like . When this string could actually be final (i.e. we can't make any operation)? It's not hard to see that must holds. And it's not hard to see that if it holds then it's not possible to make an operation.

    Let be the answer for the length . Then if we know and then for how many strings this string will be final? Answer: . Let's call it contribution of the final string.

    Let be the sum of contributions over all final strings of length , such that (there're zeroes in the final string). Then (cause as we said we only consider strings that starts and ends with one).

    To calculate we can write the following dp: let be the sum of contributions over all final strings of length that ends with zero and we can insert at most ones to it's end so it still would be final. can be negative, but .

    Note that in this dp we only consider strings that ends with zero and starts with one.

    To calculate there're following transitions:

    1. += . Here we insert one zero to the end.

    2. For all such that balance : += . Here we just insert to the end.

    If we knew we could calculate this in , because there're transitions of the first type and for second transitions we can fix and use suffix sums to make them fast.

    Final step: to calculate we need to know only for . And if for all we've already calculated then we can calculate . To do this we need to iterate over the number of ones in the end of the final string and using the same suffix sums update . Knowing we can calculate . So we can calculate and at the same time.

    Total time complexity: . Code

13 hours ago, # |

Can someone please explain why in problem D, a[i-1] has to be divisible with a[i]?

  • 9 hours ago, # ^ |

    consider a[i-1]=2*2*3 and the current b[i]=2*3*7.
    Then a[i]=gcd(a[i-1],b[i]) = 2*3 since 2*3 is their common factors.
    That mean that prime factors(a[i]) should be a subset of prime factors(a[i-1]) and this mean that a[i-1]%a[i]==0.

    • 3 hours ago, # ^ |

      Thank you! It was actually obvious from the associative property of the GCD. I have misread the problem.

11 hours ago, # |

Rev. 2  

0

In problem D ,I used inclusion-exclusion principle but why my code gives wrong answer in test 3 in the sample cases?

11 hours ago, # |

Rev. 4  

0

In problem C. Why does a[i] have to be different from b[i]?

In test case:

Is not a valid solution:

  • 7 hours ago, # ^ |

    Problem C is a bad problem, u know.

  • 4 hours ago, # ^ |

    For the answer to be 'YES', the necessary and sufficient condition is that the XOR of a[i] and b[i] for every i from 1 to n, must be equal. It is not that a[i] has to be different from b[i].

7 hours ago, # |

For proA what for the input is the sequence "2 3 4", it has already been sorted and A[0] != 1, why it output is "No"?

  • 6 hours ago, # ^ |

    Note that the problem input is a permutation

    • 5 hours ago, # ^ |

      Thanks and I forgot the permutation, for my bad english. Have a good day buddy!

6 hours ago, # |

Rev. 2  

0

The solution of problem B can be better by the count of 0 = n — count of 1

6 hours ago, # |

Rev. 3  

0

Can you tell me why when respect then we can just use right bracket at the end of string?

In case of ,1-Index,let . It's easy to see that 。But we should do operation 1 one time,and add two left bracket on the left. If we just work as the edtorial, we can make the last bracket be matched.

Sorry for my bad English. But can anyone teach me? Thank you very much.

5 hours ago, # |

About problemC, maybe follows this logic can be understood better. Firstly, we assume every char of string a and b is '0', let we operate it randomly, we can observe that when our operates is odd the string will be , and if operates is even the string will be . thus we know only if the string or for every index i, the problem has the solution.

5 hours ago, # |

Rev. 2  

+5

For E, you don't need to use BIT to calculate the answer, you can split it into two parts (I concatenated 0 to the start of prefix sums)

Which is just sum of max over all pairs in array (which you can calculate by sorting) minus the sum of minimums for all subarrays (which you can calculate using contribution technique and stacks(next and previous smaller element))
Submission Link

4 hours ago, # |

Why problem 2 gave TLE for same approach written in Java. I have done both work in one for loop , still it gave me TLE.

  • 2 hours ago, # ^ |

    Java scans and prints stuff very slowly, maybe scanner from here will be helpful for you

    114016483

4 hours ago, # |

Rev. 2  

+15

How to solve F in time ?$

3 hours ago, # |

Can anyone explain for C :

Observe that answer is only possible if both the string is the same or if we can get b after inverting each character of a.

How this stataement is true , i not able to understand

3 hours ago, # |

Rev. 4  

0

int x1=INT_MIN,y1=INT_MIN,tmp=0;
for(int i=0;i<n;i++){
  while(i<n&&s[i]=='0'){
  tmp++;
  i++;
  }
  if(tmp>x1){
  x1=tmp;
  tmp=0;
  }
}

tmp=0;

for(int i=0;i<n;i++){
  while(i<n&&s[i]=='1'){
  tmp++;
  i++;
  }
  if(tmp>y1){
  y1=tmp;
  tmp=0;
  }
}

This code is used to count the max number of continuous 0s and 1s. But it fails on some test cases in the second pretest. Can anyone provide a test case when this fails?

2 hours ago, # |

Rev. 2  

0

I can hardly understand the editorial for Problem G.

Can anybody explain it in a more detailed way?

2 hours ago, # |

I did problem B with same aproch, but I am getting TLE. Can someone tell me what am I missing? My submission: 179577559

93 minutes ago, # |

Here is my implementation of E. It uses the same idea as the official solution, but only requires an std::set instead of a BIT.

By inserting the indices to a set in sorted order of prefix sum, we can find the possible starting and ending points of a segment whose minimum is located at each index. The other part of the summation is easier to deal with; simply count how many times each element is the maximum of a pair.

60 minutes ago, # |

Rev. 2  

0

BIT seems very handy but I don't understand some parts of code in editorial. Can anyone explain how to use BIT to count Max and Min in Problem E?

57 minutes ago, # |

Actually, we can solve E in O(n) instead of O(n log n). 179627446 this is my solution for the problem. Now we can see that we can replace maps with arrays, because all keys in first map are from -n to n, and from 0 to n in the second one.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK