Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

### pikmike's blog

By pikmike, history, 10 months ago, translation, ,

1217A - Creating a Character

Tutorial

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)

• +94

 » 10 months ago, # |   +21 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$?
•  » » 10 months ago, # ^ |   +8 I think it does
 » 10 months ago, # |   +9 why is the bound from problem D so low? the solution should be in $O(n)$?
•  » » 10 months ago, # ^ |   +10 i'm not sure, but the checker may have a higher asymptotic time complexity, $O(n^2)$, for example
•  » » » 10 months ago, # ^ | ← Rev. 2 →   +8 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
•  » » 10 months ago, # ^ |   +32 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.
•  » » » 10 months ago, # ^ | ← Rev. 2 →   0 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...): 60128957slow solutions in $O(n^2)$: 60121420 60123728
•  » » » » 10 months ago, # ^ | ← Rev. 2 →   +13 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.
 » 10 months ago, # |   0 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.
•  » » 10 months ago, # ^ |   +5 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'
•  » » » 10 months ago, # ^ |   0 Can you share more questions like this? Use of nxt array was something extraordinary... is it a general technique like building prefix sum array?
•  » » 10 months ago, # ^ |   +5 if f(l,r) > 2*10^5 then the binary number can't fit in the string
•  » » » 10 months ago, # ^ |   0 i still couldn't understand why, when the string can store any length of the binary number
 » 10 months ago, # |   0 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?
•  » » 10 months ago, # ^ | ← Rev. 2 →   0 the res has to be lower than exp + 1! testcase: 4 1 0
 » 10 months ago, # |   0 can somebody explain me problem c? you can write short answer, but bigger is better :D
•  » » 10 months ago, # ^ | ← Rev. 4 →   +5 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 
•  » » » 10 months ago, # ^ |   0 thanks, but i get idea after 5 minutes of that comment, thanks anyways.
•  » » » 10 months ago, # ^ |   0 Thanks a lot much clearer and simpler solution!!!
•  » » » 10 months ago, # ^ |   0 What will be the time complexity ?
•  » » » » 10 months ago, # ^ |   0 n^2
•  » » » » 10 months ago, # ^ |   0 C*n, where C is a constant.
•  » » » 10 months ago, # ^ |   0 Hello, could you elaborate more on why do I have to keep track of consecutive zeroes? and why comparison between consecutive zeros + (j — i + 1) is larger than or equal to the decimal expansion is made? Thank you
•  » » » » 10 months ago, # ^ |   0 consied the case for s = 0110. here the count of zeros preceeding the 1 is 1. so when you start the j loop the first value you get "11" is 3. the lenght of this string is 2 and the decimal value for 11 is three and hence doesnt satisfy the questions "f(011)=3,f(00101)=5,f(00001)=1,f(10)=2,f(000)=0 and f(000100)=4.". However, If we were to include one of zeroes we encountered before find '1' in string at i = 1, the newly formed string would become 011 which in length of size 3 and the decimal value of this is also 3, thus a goodstring. we continue by increasing the j counter and encounter string 110 which has length = 3 but a decimal value = 6, so for this string to become a good string we obviously need "110" but we also need three zeroes "000" preceeding the string "110", but since we already know the count of preceeding zeroes, ie 1, we know we cant find any bigger number in the given seeuqnce so we can just break and look for the next "1" in loop i;
•  » » » 10 months ago, # ^ |   +18 wow so op
•  » » » 10 months ago, # ^ | ← Rev. 2 →   0 I have written the same algorithm still I am getting wrong answer on one test case. Can you please check it? include 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=num && num>=right) { count++; continue; } else { prevZero = 0; break; } } } cout << count << endl; } } 
•  » » » » 10 months ago, # ^ |   0 I think it should be (right + prevZero >= num)
•  » » » » » 10 months ago, # ^ |   0 yes, thank you. But still, it is giving wrong answer on test case:2.
 » 10 months ago, # |   0 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?
•  » » 10 months ago, # ^ | ← Rev. 2 →   0 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 winlastly, 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
•  » » » 10 months ago, # ^ | ← Rev. 3 →   0 Then why did you write "x >= ((head — max damage + ratio — 1) / ratio)+1"(adding an extra one) in your codeinstead of x >= (head — max damage + ratio — 1) / ratio
•  » » » 10 months ago, # ^ |   0 Why did you delete your reply lol But it helped me clarify the doubt nonetheless. So, Thankyou!
•  » » » 10 months ago, # ^ |   -8 Thnx man you helped me clarify my doubt.
 » 10 months ago, # |   0 Can anybody explain problem-C .I am not able to understand the problem.
 » 10 months ago, # |   +6 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.
 » 10 months ago, # |   +8 Does anyone know how to solve F in LCT implementation?
 » 10 months ago, # |   0 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
 » 10 months ago, # |   0 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$ ?.
•  » » 10 months ago, # ^ | ← Rev. 2 →   0 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$.
•  » » » 10 months ago, # ^ | ← Rev. 2 →   0 could you please elaborate about "implementing multiple segtrees" ? i dont get itedit: i mean, we build 10 segment tree bcs the max value is 10^9, but i dont know how to get the answer from this segment trees
 » 10 months ago, # |   0 For B, I am giving you a simple hint without confusing you. Just try to find the maximum damage in the query(d) and the maximum difference(d-h) in all query. If you find the maximum difference is below zero, you are never going to make it. so output zero.After you once decrease the number of heads of the monster using maximum damage and after you can continuously use the maximum difference to decrease number of heads to make the number of heads below or equal to zero. Output the number of blow where you will make his head equal to or below zero.
•  » » 9 months ago, # ^ |   0 after we decrease the heads using maximum damage the number of heads should also increase by an amount of h[i] corresponding to that maximum damage.How will we take that into account?
 » 10 months ago, # |   0 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.
•  » » 10 months ago, # ^ |   0 Can you explain your solution in detail? Thanks.
•  » » » 10 months ago, # ^ |   +1 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.
•  » » » » 10 months ago, # ^ |   0 That's a very nice and intuitive solution. Thanks
•  » » 10 months ago, # ^ |   0 thank u very much............u saved my time ....
•  » » 10 months ago, # ^ | ← Rev. 2 →   0 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
•  » » » 2 months ago, # ^ |   0 If you simulate your algorithm for test case 3 you will see.I used to implement the same as you.
 » 10 months ago, # |   -10 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.
 » 10 months ago, # | ← Rev. 2 →   0 .
 » 10 months ago, # |   +37 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.
•  » » 9 months ago, # ^ | ← Rev. 2 →   +10 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.
 » 10 months ago, # |   0 Why I got TLE in E,my solution is only O((n+m)⋅logn⋅log10MAXN).link
 » 10 months ago, # |   0 Can some one explain why 2addS>exp+int−str goes to 2addS≥exp+int−str+1
•  » » 10 months ago, # ^ |   0 In simple x>3 means x starts from 4 and goes to infinity. Also, x>=4 has the same meaning. So x>3 is the same as x>=3+1.
•  » » » 9 months ago, # ^ |   0 Thanks
 » 10 months ago, # |   0 In problem C why we are doing precalculation of nxt array?? can someone elaborate this?
•  » » 10 months ago, # ^ | ← Rev. 12 →   0 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: 60334519To 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 returnsEdit 2: Just noticed that toshinaritong explained a similar solution
•  » » » 10 months ago, # ^ |   0 Can you please elaborate how can we cut a substring of right length if f(z,j)<=len(z,j)
•  » » » » 10 months ago, # ^ | ← Rev. 10 →   0 $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$.
•  » » » » » 10 months ago, # ^ |   +3 Thank you, now I understand.
 » 10 months ago, # |   0 Can anyone help me understanding the Tutorial for Coloring Edges with an example. I am having trouble getting the logic they are using. Please help. It would really be appreciated!
 » 10 months ago, # | ← Rev. 2 →   0 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.
•  » » 10 months ago, # ^ |   0 Ok, I understood it.
 » 10 months ago, # |   0 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
•  » » 10 months ago, # ^ |   0 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.
 » 10 months ago, # |   0 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?
 » 10 months ago, # |   0 Can anyone explain to me why there is 1 added in the inequalities equation
 » 10 months ago, # |   0 can anyone explain the formula written in B question editorial how it came?
 » 10 months ago, # | ← Rev. 2 →   0 Hi, i'm really new to programming and I can't understand the first problem solution. Can anyone tell me how to do it using c++? Edit: nvm. found a way to do it myself
 » 9 months ago, # |   0 public static void main(String[] args) { Scanner s = new Scanner(System.in); int q = s.nextInt(); while(q-->0) { int count = 0; int str = s.nextInt(); int inn = s.nextInt(); int exp = s.nextInt(); int strExp = str+exp; while(strExp-->inn++) count++; System.out.println(count); }Can someone tell me which test cases would make this wrong? I basically just compute the max strength, then from there decrease this max strength by 1 and increase the intelligence by 1.
 » 9 months ago, # |   0 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
 » 9 months ago, # | ← Rev. 2 →   0 Oops. I posted the answer for the wrong round. Comment deleted.
 » 3 months ago, # |   0 in solutiob of B : why author writes m-=maxd;can someone explain me please .