When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

awoo's blog

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

1217A - Creating a Character

Tutorial
Solution (adedalic)

1217B - Zmei Gorynich

Idea: Roms

Tutorial
Solution (Roms)

1217C - The Number Of Good Substrings

Idea: Roms

Tutorial
Solution (Roms)

1217D - Coloring Edges

Idea: BledDest

Tutorial
Solution (Roms)

1217E - Sum Queries?

Idea: BledDest

Tutorial
Solution (PikMike)

1217F - Forced Online Queries Problem

Idea: BledDest

Tutorial
Solution (PikMike)
  • Vote: I like it
  • +94
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +21 Vote: I do not like it

Thank you very much for the round!

In E, it's said:

The problem now got reduced to: find a pair of numbers $$$a_i$$$ and $$$a_j$$$ such that $$$l$$$ $$$<=$$$ $$$i$$$ $$$<=$$$ $$$j$$$ $$$<=$$$ $$$r$$$, there is at least one position such that both $$$a_i$$$ and $$$a_j$$$ have non-zero digits on it and $$$a_i$$$ $$$+$$$ $$$a_j$$$ is minimal possible.

Shouldn't $$$i$$$ and $$$j$$$ imply $$$i$$$ $$$!=$$$ $$$j$$$?

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

why is the bound from problem D so low? the solution should be in $$$O(n)$$$?

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

    i'm not sure, but the checker may have a higher asymptotic time complexity, $$$O(n^2)$$$, for example

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

      the checker will create a graph with edges of a single color (at most two graphs) and do a toposort on each of them to detect cycles. Therefore the checker is in $$$O(n+m)$$$ and can also handle much larger inputs

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

    I think in general when you're setting problems, you should strive to exclude all the naive solutions, but let certain solutions pass. However, if possible, you should try not to give away desired complexity with the bounds, because that makes the problem easier. Usually, this second part is not possible because CF has a lot of problems where O(n^2) is naive and O(n) or O(n log n) is optimal, so any MAXN less than 10^5 will let O(n^2) solutions pass.

    However, here, the naive solutions are in the exponential range, and there aren't really any "bad" quadratic solutions, so I think it's okay to leave the bounds there so that it doesn't give anything away about the solution.

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

      well the idea may sound good but by setting the limit like this you risk that weird stuff gets AC... for example those:

      randomized (which seems to be correct even without randomisation the author just did not realized that it would always work...): 60128957

      slow solutions in $$$O(n^2)$$$: 60121420 60123728

      • »
        »
        »
        »
        5 years ago, # ^ |
        Rev. 2   Vote: I like it +13 Vote: I do not like it

        IMO, this problem $$$O(n^2)$$$ solution isn't really bad. Checking cycles in $$$O(n)$$$ is important, but that's not the main idea and it leaves more freedom to slow languages and implementations.

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

here is written if r-l>18 then f(l,r)>2*10^5.and why? example: 00000000000000000000000000001. it's obvious that that binary number is 1.

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

    also here is written — "lets iterate over the right boundary of substring and high 1-bit position (denote it as r and l respectively)", which means that we consider only the cases when a[l] == '1'

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

      Can you share more questions like this?

      Use of nxt array was something extraordinary... is it a general technique like building prefix sum array?

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

      can you explain where we are using binary search in this problem because the problem is tagged as binary search

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

    if f(l,r) > 2*10^5 then the binary number can't fit in the string

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

While solving A by fixing the condition on minAddI, I get WA.

minAddI = ceil((str+exp-int-1)/2)
ans = max(0, max(minAddI+1))

What's the mistake here? Can't we look at the various ways of points to be added for Intelligence?

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

can somebody explain me problem c? you can write short answer, but bigger is better :D

  • »
    »
    5 years ago, # ^ |
    Rev. 4   Vote: I like it +5 Vote: I do not like it

    My solution is like this: Iterate through the string (i), then if encounter a '1', start another loop from the i position (j), this time calculating the decimal expansion of binary from i to j, then checking if number of previous consecutive zeros + (j — i + 1) is larger than or equal to the decimal expansion (cuz the length is equal to the decimal expansion).

    for example,

    s = 00001010
    
    1. iterate until encounter 1
    
        00001010
            ^
    2. iterate until not enough zeros behind the 1
    
        number of previous consecutive zeros = 4
        00001010
            ^
        4 + 1 >= 1 (binary "1")
    
        00001010
            ^^
        4 + 2 >= 2 (binary "10")
    
        00001010
            ^ ^
        4 + 3 >= 5 (binary "101")
    
        00001010
            ^  ^
        4 + 3 not>= 10 (binary "1010")
    
        so break and iterate i again
    
    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thanks, but i get idea after 5 minutes of that comment, thanks anyways.

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

      What will be the time complexity ?

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

      wow so op

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

      I have written the same algorithm still I am getting wrong answer on one test case. Can you please check it?


      include<bits/stdc++.h> using namespace std; define ll long long int main() { ll t; cin >> t; while(t--) { string s; cin >> s; ll len = s.length(); ll count = 0; ll prevZero = 0; for(ll l=0;l<len;l++) { if(s[l]=='0') { prevZero++; continue; } ll num = 1; count++; for(ll r=l+1;r<len;r++) { num *= 2; num += (s[r] == '0') ? 0LL : 1LL; int right = (r-l+1); if(right+prevZero>=num && num>=right) { count++; continue; } else { prevZero = 0; break; } } } cout << count << endl; } }
    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      please i am not able to understand where we are using binary search in this problem because the problem is tagged as binary search

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

      thank you so much i was struggling with this question from 2 days when i read your explanation i got it...!

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

Can somebody explain B? How does the formula come? I think sorting on the basis of difference is enough. After sorting we just have to see the first element, isn't?

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

    if max damage >= head, you only need 1 hit if it's not and max ratio pair of damage and regen <= 0, you wont be able to win

    lastly, you know you need to deal x times max ratio pair of damage and regen + 1 times using max damage to finish, so the answer will be x + 1 ( 1 times using max damage).

    • (x * ratio) + max damage >= head
    • x >= (head — max damage) / ratio
    • remembers, x can be floating number here, and we need to round it up
    • so, you can add 1 to x if (head — max damage) % ratio > 0
    • or you add the divisor — 1
    • x >= (head — max damage + ratio — 1) / ratio
»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Another way to color edges in problem D. Neither simpler nor harder but just another XD.

Let's iterate over all vertices and color all "$$$in$$$" edges in color $$$1$$$ and "$$$out$$$" edges in color $$$2$$$. For every cycle, there will be the vertex which was considered last in it (let's call it $$$u$$$). It implies that color from the previous vertex in the cycle to $$$u$$$ and color from $$$u$$$ to the next vertex in the cycle are different.

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

Does anyone know how to solve F in LCT implementation?

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

can you explain c? such algorithm does not support substrings like 00000000000001, because there hightest position is 13 and lowest 0, i don't understand please explain to me

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

I have a doubt in E, in the proof that an unbalanced set can never be made balanced again. Where are you using the assumption that $$$a\subset b$$$ ?.

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

    We've made some set unbalanced by having two numbers with non-zero digits at some position — that's the set $$$a$$$. So now we can always find such a position in the set $$$b$$$.

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

Another easier approach to problem D:

Suppose a graph $$$ G = (V, E) $$$. Partition $$$ E $$$ to one group contains $$$ in > out $$$ only, and one group contain $$$ out > in $$$ only. Clearly no cycle in two sets. So the maximum number of colour is 2. One can just check if 1 colour is OK.

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

    Can you explain your solution in detail? Thanks.

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

      The lower bound is trivially 1.

      Construction of the upper bound:

      Consider two sets

      $$$ \{ (i, j) : 1 \leq i < j \leq |V| \} \bigcap E $$$

      and

      $$$ \{ (i, j) : 1 \leq j < i \leq |V| \} \bigcap E $$$

      Note that they are disjoint and their union is $$$E$$$. Also note that no cycle exist in both two sets. So there always exist one valid coloring method when number of colour is 2, i.e. the upper bound of the answer is 2.

      Check if answer is 1 by cycle detection.

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

    Hey. Can you please tell me why two coloring the edges when the graph has cycle doesn't work? Here's my code: https://codeforces.com/contest/1217/submission/60264580

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

      If you simulate your algorithm for test case 3 you will see.

      I used to implement the same as you.

»
5 years ago, # |
  Vote: I like it -10 Vote: I do not like it

Can someone explain me why my code to F with block = 500 gets AC, but with block = sqrt(n) + 10 gets WA? https://codeforces.com/contest/1217/submission/60218374, https://codeforces.com/contest/1217/submission/60218188.

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

You missed a trivial solution to D: If there's a cycle, paint each edge $$$(u,v)$$$ such that $$$u>v$$$ black and all others white.

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

    Nice solution! Actually I think that this is almost the same as the one in the editorial. Just that I don't like the complicated proof that was presented.

    I think that we can easily prove that your solution works because if we consider the indexes of nodes that are in a cycle, there will obviously be a distinct maximum and minimum index because there are at least 2 nodes in a cycle.

    The predecessor of the node with max index clearly has a smaller index. This means that we have an edge (u, v) where u < v. The predecessor of the node with minimum index clearly has a larger index. This means that we have an edge (u, v) where u > v.

    Therefore, in any cycle, we will always have an edge (u, v) where u > v and an edge (u', v') where u' < v'. In this way, we will always have at least one black and one white edge in a cycle.

    I believe this proof works for the official solution as well.

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

Why I got TLE in E,my solution is only O((n+m)⋅logn⋅log10MAXN).link

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

In problem C why we are doing precalculation of nxt array?? can someone elaborate this?

  • »
    »
    5 years ago, # ^ |
    Rev. 12   Vote: I like it 0 Vote: I do not like it

    I'm not sure I understand the editorial solution either, but I coded up a simple brute-force / two pointer solution with a pruning heuristic: 60334519

    To summarize, we iterate through the string from left to right. If $$$s[i] = 0$$$, we increment a $$$len$$$ accumulator and move on. If $$$s[i] = 1$$$, we start a secondary loop, where we keep track of the potential values of $$$f(z,j)$$$ (where $$$z$$$ is an imaginary pointer $$$\leq i$$$ that includes the "streak" of $$$0$$$s just before this $$$1$$$ if it exists). $$$ans$$$ is incremented whenever $$$f(z,j) \leq len(z,j)$$$ since it means we have found a good substring (you can "cut" a substring of the right length), but as soon as $$$f(z,j) > len(z,j)$$$ no more good substrings can be found from this position as $$$f$$$ grows faster than $$$len$$$, so we cut off the search and reset $$$len$$$ before moving on. Since the inner loop will never iterate more than $$$\lfloor \log_2 n \rfloor + 1$$$ times before being cut off, the algorithm runs in $$$O(n \log n)$$$ time.

    Edit: Actually it might be in $$$O(n)$$$ time, because an adversarial input can only force more inner loop iterations by including zeros before the one, but zeros don't trigger the inner loop, and increasing the number of zeros has rapidly diminishing returns

    Edit 2: Just noticed that toshinaritong explained a similar solution

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

      Can you please elaborate how can we cut a substring of right length if f(z,j)<=len(z,j)

      • »
        »
        »
        »
        5 years ago, # ^ |
        Rev. 10   Vote: I like it 0 Vote: I do not like it

        $$$f(z,j) \leq len(z,j) \Rightarrow \exists! x : z \leq x \leq i \land f(x,j) = len(x,j)$$$, i.e. substring $$$x..j$$$ is "good".

        In less formal terms, the indices between $$$z$$$ and $$$i$$$ indicate the "pool" of zeros we can use to manipulate the length of the substring; as long as $$$f(z,j) \leq len(z,j)$$$ we can always choose to include the exact number of zeros required for the substring to be good. When $$$f(z,j) > len(z,j)$$$, we don't have enough zeros, so we break the inner loop and move on.

        Or in more concrete terms, take the example $$$s = 00001010$$$

        When $$$i = 5$$$ (one-indexed), $$$z = 1$$$. Since $$$s_5 = 1$$$, we start the inner loop.

        When $$$j = 5$$$, $$$f(z, j) = 1 \leq len(z, j) = 5$$$. Pick $$$x = 5$$$. Note that substring $$$5..5$$$ is good cause $$$f(5, 5) = 1 = len(5, 5)$$$

        When $$$j = 6$$$, $$$f(z, j) = 2 \leq len(z, j) = 6$$$. Pick $$$x = 5$$$. Note that substring $$$5..6$$$ is good cause $$$f(5, 6) = 2 = len(5, 6)$$$

        When $$$j = 7$$$, $$$f(z, j) = 5 \leq len(z, j) = 7$$$. Pick $$$x = 3$$$. Note that substring $$$3..7$$$ is good cause $$$f(3, 7) = 5 = len(3, 7)$$$

        When $$$j = 8$$$, $$$f(z, j) = 10 > len(z, j) = 8$$$, so we break the loop and continue iterating $$$i$$$.

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

Problem E. While updating, why don't we consider the case when a[pos] was the minimal number with non-zero j-th digit in the interval [l,r), and x > a[pos]?

For instance we have the array [1,2] and we're getting the update query with pos = 1, x = 5. When updating, t[v] for [1,2), min_digit[0] still will be 1, but it should be 5.

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

Can someone please help in problem D.

I am using the concept of back-edge only, If I found a back edge, color it with '2', else 1.

My solution: https://codeforces.com/contest/1217/submission/60415966 Its based on this article: https://cp-algorithms.com/graph/bridge-searching.html

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

    The article you are referring to is for undirected graphs, not directed ones.

    DFS on digraphs induces more types of edges, you can read it here.

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

There's something weird with problem F. I got accepted with 748ms by setting P=5*10^4. If I use P=sqrt(m), my submission receives TLE verdict. What is going on?

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

I am getting a TLE on test case 5 for problem E — Sum Queries. Can someone tell me how to make my solution faster ? 62117228

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

can i do the same in problem d as in the editorial, but instead just make the first edge of a cycle into a second color, all the others edges are the first color? if im not mistaking the logic of the proof of my solution is the same as authors solution, but my solution gets wa3. can someone tell why approach with first edge in cycle is not working, or its working and my implementation is bad submission

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

I think setting such TL in problem E is stupid. I don't care about cache-friendliness or whatever. If my solution has the right complexity, it should pass.

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

For the above case(problem D), a cycle is possible with black edges. Black ones are black edges and purple edges are white edges. Visiting order of DFS is 1, 2, 3, 4, 5. If I'm missing something, can anyone say what is it?

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

Fantastic editorial for problem C