Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

# | User | Rating |
---|---|---|

1 | MiFaFaOvO | 3520 |

2 | tourist | 3461 |

3 | Um_nik | 3367 |

4 | apiadu | 3351 |

5 | mnbvmar | 3332 |

6 | Benq | 3330 |

7 | LHiC | 3276 |

8 | TLE | 3271 |

9 | Radewoosh | 3251 |

10 | ecnerwala | 3241 |

# | User | Contrib. |
---|---|---|

1 | antontrygubO_o | 189 |

2 | Errichto | 188 |

3 | tourist | 180 |

4 | Radewoosh | 173 |

5 | vovuh | 166 |

6 | pikmike | 165 |

7 | ko_osaga | 162 |

8 | Um_nik | 161 |

9 | rng_58 | 155 |

10 | farmersrice | 153 |

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial of Codeforces Round #562 (Div. 1)

Tutorial of Codeforces Round #562 (Div. 2)

Tutorial of To-Do problem

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jan/23/2020 21:30:23 (f2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Doesn't the solution for Anagram Paths run in quadratic time on a star? "upd" function iterates over all children of all vertices on the path when processing each query, and there can be O(n) of those.

I spent a lot of time thinking how to handle this situation, couldn't come up with anything better than 26 heaps in each vertex and overall O(n*sqrt(n)*log(n)) running time, and couldn't implement it before the contest ended :(

How does a star graph work in a binary tree?

Oh, here's what I missed :) Thanks!

Isn't it binary tree?

I first implemented 1168D - Anagram Paths in $$$O(n \log n)$$$ with segment trees and only then saw that it doesn't say YES/NO correctly. But if the answer is YES, then I can correctly find the value to print (max number of each letter). But I later needed $$$O(n \sqrt n)$$$ anyway to check YES/NO.

So, how to find value of the answer in $$$O(n \log n)$$$? Run DFS to get preorder and postorder of every vertex. Create $$$26$$$ segment trees linearly, one for each letter. If there is a character on an edge, look at segment tree for this letter, and add $$$+1$$$ to segment corresponding to the subtree below the path. For each letter, you need a path to some leaf that has biggest number of occurrences of this letter — this is max in a segment tree.

This solution doesn't actually rely on the fact that there are at most two children. Maybe it's possible to extend it to get YES/NO too?

It is very smart!

But I don't think that it is possible to extend it, honestly. Problems about dynamic tree DP are solved with some spooky HLD solutions :(

Why doesn't this sol, say YES/No prorpely. I can't think of a reason. Do you have a test case or any intuition?

Other than checking depths to be equal, the answer is NO if there exists a vertex such that the height of its subtree is smaller than the number of fixed characters below — for every letter, we take a path (to a leaf) with max number of occurrences of this letter, and we sum these $$$26$$$ values. The issue with my solution is that I only check the root for this inequality, while I should check all vertices with two children.

The counter test is: $$$N = 4$$$, edges: $$$(1-2, ?)$$$, $$$(2-3, a)$$$, $$$(2-4, b)$$$. The answer is NO because we need at least one

`a`

and at least one`b`

below the vertex $$$2$$$.Point it out if I am wrong:

Let sum[v] denote the sum of maximum occurrences in a link for each character in the subtree v.

After replacing a '?' with a letter, what we need to do is a link addition.

Fix sum[v] + dep[v] and its minimum(which is required to answer "Shi" or "Fou" queries), which can be done in O(n log^2 n) (HLD) or O(n log n) (LCTs with some extensive information).

Another issue: how to find the length of the link we're going to update. Fix the 26 segment trees as you've mentioned above and do a binary search on the one that we need. Then, simply climb the tree through HLD or binary encoding.

What is LCT?

link cut tree

I implemented this idea and it works correctly. But it is slower than bruteforce because of large constant factors.

My submission: 54747584

Can anyone please explain their solution of Problem C (Increasing by modulo) ?

Sure.

First we notice that the number of overall operations is equal to the maximum number of operations used on a single index.

Next we notice that, if we can solve the problem in <= X operations, then we can also solve it in <= X + 1 operations. This is a monotonic sequence, so we can solve for the minimum value of X with binary search.

The question is, how do we check for <= X? Well, notice that it is never worse to have the leftmost index to be lower. However, we can only increase-- until it loops around. Thus we either don't change the value at x, or we set it to zero. (we check if the required number of operations to do this is <= X). Next, we move on to the next index, this time setting the "base" value equal to the previous index's value to ensure our sequence is nondecreasing. If at any point our current index is less than the base and X operations on it are not enough to make up for this, then we need to raise our value on X.

Now we have O(log m) searches and O(n) checking, so we have an overall O(n log m) solution.

Excuse me what do you mean by "However, we can only increase-- until it loops around. Thus we either don't change the value at x, or we set it to zero. (we check if the required number of operations to do this is <= X)."

lets say the first value int the array is 3, and m is 6.

after each operation, we get the following values: 3, 4, 5, 0, 1, 2

If we increase 3 through the operation, we don't want to make it 4 or 5, as this just makes getting a nondecreasing sequence harder, we want it to become less than 3.

However, to make it 1 or 2 we need to go past zero. Zero is better for nondecreasing sequences, and it is also easier to get to.

Therefore, our only choices for the first one are zero or the initial value.

Right! I just got it an hour ago after looking at your submission :) thanks again!

thanks for great explanation.. can u give some question related to C i.e. increase by modulo

wanna know too

Your explanation is awesome! Thanks a lot!

"First we notice that the number of overall operations is equal to the maximum number of operations used on a single index."

How did we get to this conclusion? I am not able to convince myself. Pls help.TIA

As an example, suppose you have an array of length 5. You do 1

incrementon the 1st element, 2incrementson the second, 3 on the third, 4 on the fourth and 5 on the fifth. As per the question, anoperationis defined as any number of indices to be chosen and incremented. So in our example, the first operation could be incrementing all elements. The second operation would be to increment all elements except the first one, and so on. Now you see, the last operation would always be the increment the index (or indices) with the maximum number of increments required.https://medium.com/@harryjobz/increasing-by-modulo-c057eadfa1f0 for a bit more explanation

how we come to conclusion that in first case we have make a[I] equal to prev if we have enough operation….. but it is okay to leave a[I] as it is as the sequence is still non decreasing... plz tell me the proof of this, as I cant understand it and thanks for great explanation.. sry for bad English.

In the case when prev > a[i] we explore the possibility of increasing a[I] to previous by taking prev — a[i] steps but why don't we consider the other possibility of decreasing all other values before previous to a[I]. I mean we should consider the minimum of the two possibilities as our value for k. Please provide a mathematical explanation.

That should work to in my opinion. But you cannot mix two strategies. You can edit the check function to your strategy and check if its right.

But to decrement the previous values would increase the complexity to quadratic time which would not pass.

yeah, but you can check if it works with a few samples

Are there many other programmers who compete in Rust?

My solution to 1168C seems similar to the editorial, but it times out and I don't know why.

Also, why isn't the i-j-k loop in the official solution n log^2 n?

Edit: locally, my code runs a lot faster if I use -O. What rustc compiler options does Codeforces use?

It's roughly 10^8 operations. Should be fine with C++. I've heard Rust is faster than C++?

I got word from Mike, we use: rustc -O -C link-args=/STACK:268435456 --cfg ONLINE_JUDGE %1

The inner loop should only run 300,000 * floor(19/2) * ceil(19/2) = 27 million times, so it's still puzzling that my solution is so slow.

Edit: I figured it out! Unfortunately, it turns out stdout is line-buffered, so it's slow if you print a lot of newlines, such as using println!() or print!("\n").

Here's a passing submission using writeln!()

Interesting!

Is there anyone who has a proof for the lemma of problem D ? Why there are no strings without such x,k of length at least 9 ?

Just try out all bitstrings of length 9? Is it that hard?

Yes, there is a proof

Hey there, can you provide more sites like above mentioned(mathematics and proofs).

Thank you :)

Amazing site I had ever seen! I love it.

what do these lines do?

If running on local machine (ONPC!), use a constant seed for random (228), but when submitting to codeforces for instance, use the time as seed to avoid hacks. This will make debugging your random code easier, because the values produced by rnd(228) are always the same every time you run the code on your machine.

Hey!! Can you please explain how does generating seed help with hacks? Maybe I dont know how hacks work. Can you please tell me about it too?

I checked your solution of problem D Good Triple. I don't understand why get the largest r. If we fix l, and we find the smallest possible r, and r of value from r to n-1 (inclusive) is ok. I think we should get the smallest r, and I changed your code by add a break if I get a smallest r.

And the code get AC. Is there any bug of the solution or the system test does not cover all cases?

This part already ensures that you will make vl smaller, so if you will update vl, you will break because of the for condition.

Then the code works to find the smallest $$$r$$$, not the largest.

If we find largest $$$r$$$, then it’s obviously n-1 or bust. So it’s a typo.

How to solve 1169B using graphs? Thanks.

Actually its a question of determining whether the vertex cover set has atmost 2 elements or not

Isn't that a NP Complete problem for general graph?

yes it is np complete for general graph but that problem is finding minimum set of vertex with contains all edges of a graph,here the problem is different

Can you explain the difference? Don't you still need to cover every edge of the graph? I tried implementing

https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pearson/10ExtendingTractability-2x2.pdf

but it did not work out.

Can you please explain further , possibly with a pseudo code .

go to this link https://codeforces.com/blog/entry/67248

hey can you point out why my code is wrong ? I swapped the x and y if x is smaller than y, then put it in a vector and continue to sort it, and then picking the last and the first only to check, why is this wrong ?

ok thanks!

can us pzl explain little more or provide some link to related article

@devanshsaxena16 Thanks for the reply. I got it now and I am a Dragon ball fan too.

Hi,

In B Question in the solution of author ==>

a--, b--;Why the author use this line when his getting pairs?

Because , he want to store them in the range between [0,n-1]. Many programmers are used to this notation .

Yes you're right after that he defines vector(n) but if the line wasn't there he should do vector(n+1).

Can someone explain 1169E? I didn't get it from the editorial.

Me too.I need help

Problem 1168E is just same to the problem in the Topcoder! Here is the address: https://community.topcoder.com/stat?c=problem_statement&pm=14159

The problem is in SRM686.

why this approach doesn't works for E — AND Reachability, for every bit i merge all numbers have that bit on and to answer the query if two numbers lie in same component then they are reachable otherwise not.

Its not guaranteed that two numbers that are reachable share a bit. (the and checks two numbers for adjacancy, not the whole sequence.)

For example: 8 12 4

We can reach the 4 from 8, but they don't share any bit.

what I'm trying to do is for each bit I'll merge every number whose bit is on so for your case 4&12 will get merged when we check 2nd bit then 12&8 will get merged when we check 3rd bit so every number lies in single component

Assume you need to find reachability from index 3 to index 4 and a3&a4 is 0 but a3&a1>0 and a4&a1>0, then your code will return reachable as a1, a3 and a4 share a component but since index 1 is not between 3 and 4 this route shouldn't be used.

but it's mentioned in the question we can use any permutation of indexes.

In problem B How are we going to fix who is x in the first pair?

Is there any other way to solve 1169C, instead of the one provided in the editorial. The question was really good BTW!

https://codeforces.com/blog/entry/67241?#comment-513902

see this comment for better explanation

Can anyone please explain their solution of Problem 1169E (And Reachability) ?

Complexity of Div2 E is $$$n(log(n)^2)$$$ not $$$nlog(n)$$$

Hey guys, I understood how 1169B works, but my code keep failing on test case #4. Can anyone find the problem? Here is my code: https://codeforces.com/contest/1169/submission/54742878

Thanks

In tutorial for problem C, can somebody explain what does the below condition checks: (lf <= prev && prev <= rf) || (lf <= prev + m && prev + m <= rf)

some one pls explain problem B 6 9

6 2

4 2

3 6

4 6

2 6

1 4

2 6

4 5

4 2

****the ans is yes why not NO coz there r 9 pairs if lets say x=2&y=6 there is a pair no 8 where neither is x nor y .if u put any other x nd y u will definitely get one pair where either x is not present or y ??** **so why ans is YES not NO ****

4 6

ohh yes man! that ws dumb

please anyone explain problem D proof.

I am not able to prove it any formal way but i can do this in informal way.

if x=1,k=1, positions are 1,2,3

so if we want to find upper bound of k it is better not to put triplets of same numbers together.

Now consider x=1,`0011001100110011`

k=1->[1,2,3],2->[1,3,5],3->[1,4,7],4->[1,5,9]

for k=1,2,3 condition does not satisfy but for k=4 this conditions satisfied.so our maximum position will be atmost 9.

Here we iterate from backward find the minimum possible r for this l.

You can use the "pigeonhole principle" to understand this lemma. For a 01 string of length 9, it means at least five zeros or five ones. Assuming there are five ones, then no matter where 0 is, there will be a k satisfying condition.

can someone explain me the div2 B solution using graph..? Thanks.

Div 2E:AND Reachability is a beautiful problem with a beautiful solution!

Please Can you explain me how to solve it. I cann't understand the editorial.

I got it nvm.

Explain problem B please

Why is Div2B tagged as "Graphs"?

Can somebody help me my code is below I think I have done little differetn but getting WA at test case 13 please help me ?

## include<bits/stdc++.h>

using namespace std;

int main() { string s; cin >> s; long long n = s.length(); long long ans = 0; long long l = 1; vector<pair<long long,long long>> v;

}

Huh. So I solved E with machine learning. I thought for some time, didn't prove anything useful except the form of the answer if the input is a permutation as well and $$$p = I$$$, so I just took that as the starting point and did some random swaps until I got the target sequence. It should be slow theoretically and doesn't have to terminate, but it worked pretty damn fast after some speedups.

Specifically, I keep $$$p = I$$$ and only care about the number of occurrences $$$c_i$$$ of each number in the target (input) sequence compared to the number of occurrences in my current sequence $$$p \oplus q$$$. I calculate the loss function $$$L = \sum |c_i - c^*_i|$$$ and perform only swaps in $$$q$$$ that don't increase $$$L$$$. I go through the whole permutation $$$q$$$ multiple times in random order, ignore some elements that probably won't decrease the loss when swapped and try all swaps otherwise. Swaps that strictly decrease $$$L$$$ are made immediately. If there are currently no such swaps and $$$L > 0$$$, I also make swaps that keep $$$L$$$ constant.

It's really just hill climbing — small incremental improvements to minimise loss, with some "epochs" that start by shuffling and a bit of extra swapping after each epoch to increase entropy.

I made Youtube videos about two problems from this contest, both with an explanation how I got to a solution, and then code walk-through. It might help some people.

Please solve c problem also

I have seen many solutions of problem c but no one is thinking of this statement (ai+1)mod m,I mean they are just comparing adjacent elements and despite of modifying by the given way they are just assigning the min value

Link to solution of this contest https://www.youtube.com/watch?v=APi5XhkflZM

lf <= prev + m

Line #32 in Solution to 1169C is not really required.

can anyone explain the solution of problem 3?

300iq in Div2 D, instead of

"for each l find the, shouldn't it belargestr""for each l find the?smallestr"And also isn't the complexity of Div2 E: $$$O(n*log^2 + q*log)$$$. This is because you have 2 nested loops each iterating $$$O(log)$$$ times?