Hi everybody,

Today there will be the first day of Moscow Open Olympid, that is the personal programming competition that is held in Moscow each spring. This contest is prepared by Moscow Olympiad Scientific Committee that you may know by Moscow Team Olympiad, Moscow Olympiad for Young Students and Megapolises Olympiad (rounds 327, 342, 345, 376, 401, 433, 441, 466).

Open Olympiad consists of the most interesting and hard problems that are proposed my a wide community of authors, and that is why we decided to give you an opportunity to crack the complete problemset of the contest by making some kind of an experiment. Tomorrow we are going to conduct a rated Codeforces round based on problems of **both days** of our Olympiad.

**We kindly ask all the community members that are going to participate in the competition to show sportsmanship by not trying to cheat in any manner, in particular, by trying to figure out problem statements from the onsite participants. If you end up knowing some of the problems of Moscow Open Olympiad (by participating in it, from some of the onsite contestants or in any other way), please do not participate in the round. We also ask onsite contestants to not discuss problems in public. Failure to comply with any of the rules above may result in a disqualification.**

Round will happen at 11:05, March 9th, Moscow time and will last for 2.5 hours. There will be 6 problems in each division.

Round problems were prepared by ch_egor, sender, flyrise, _kun_, malcolm, grikukan under supervision of your humble servant with a great help of _meshanya_, GlebsHP, Endagorion and Helen Andreeva. Some of the div2 problems were finalized with help of Codeforces team represented by fcspartakm, also we would like to thank round coordinator KAN for his help in deciding which problems to take and all the discussions.

Good luck everybody!

**UPD**: Announcement email contained incorrect start time. Instead of "12:05, March 9th, Moscow time, 2 hours" it should be "**11:05, March 9th, Moscow time, 2.5 hours**", as was originally in the round announcement.

**UPD2**: Round is postponed by ~~10~~ 5 minutes. Stay tuned :)

Congratulations to the winners!

**Division 1:**

**Division 2:**

**UPD3**: Finally, the editorial is there! Kudos to gritukan and ch_egor for making it appear in a text form.

Very bad time plz extends start time..

True,inappropriate for indian users

Codeforces is an international site for all people.

Not only indians

This is an international website. Not only work for you.

Different times are good for different users.

The time is right for Chinese students! That's great!

UPD: The Problem was fixed, and wish everyone will increase your rating.

Maybe we should say "Perfect time for Asian users" :P

(Although I found that I have to skip some classes just now if it actually begins at

`16:05, March 9th, UTC+8`

...qwq...)Not sure if perfect for all of us, as some (including me) still have school during the time.

Now I have to decide on whether to do contest or to attend class (most likely I won't :p )

The time in the blog is the correct one, it is same on the contests page and in the blog now.

Will this round happen at

`11:05, March 9th, Moscow time`

? But in CONTESTS, it will start at`12:05, March 9th, Moscow time`

. When can I enjoy it？ (sorry for my poor English :(UPD: Maybe Johnson_sky found another difference?

UPD2: The Problem was fixed. Wish everyone have fun.

The time I showed was UTC+8, so I didn't notice the difference in the start time.

utf-8 ? Do you mean UTC+8 ?

In the blog,

`11:05, March 9th, MSK`

`==`

`16:05, March 9th, UTC+8`

.But in the CONTESTS, it is

`17:05, March 9th, UTC+8`

.Obviously, they are different.

And thanks for that you found another difference.

UPD: The problem is fixed.Have fun.

I don't know if I should skip the math class tomorrow to participate in this contest.

PS: Now it coincide with my midterm exam...

I hope this contest can help my rating increase to 2000+

Perfect time for some Asian OIers...At least for me...

I like this website .I have never experienced this exciting feeling before.I like these competition.also I am a newer ,I will fight.

Wow, this round starts after my University exam. So no problem in attending. I wish everyone good ratings.

I have lessons at 14:00(UTC+8) and it will lasts for 2.5 hour. So I can take part in this round if it will start at 17:05(UTC+8) but it moves to 16:05.... how disappointing.

You will miss the chance to go to Div 1. xd

Hope the statements as short as the announcement,

wait it's not short :|Perfect time for Asian users!

Nope! It seems to be

Dinnertime...Something is wrong here. Originally it was 12:05. I also remember checking the time in the contest tab. Also in email it was mentioned 12:05 and now you are moving the whole contest by one hour just because you wrote this in the announcement?

First thing is that you should not move it. Announcement could be easily adjusted. Perhaps very few people clicked the url with time in this announcement. It is also a very short time for such a change.

Second thing is that you should send at least one more email with the correction. Why do you assume that everybody regularly checks main cf website instead of their email?

This round is already in an unusual time. If you won't make sure that everybody is informed about the change, the participation level may be very low but the frustration very high as a lot of people will appear here at 12:05 to discover that contest will have been running for an hour already...

Agreed. If it was postponed that might be acceptable but now many will turn up too late for the contest.

Lucky I happened to check this post once before going offline right now or I would have missed it too.

I check cf more often than e-mail.

Sorry, my fault. I found that the times changed just before a long train trip. I decided that the announcement in the post + the note in a registration form are enough. During the train trip I was out of Internet without any chance to send extra emails.

I guess your train trip is over. So why you still haven't sent the notification?

It finished ~30 minutes ago (I've sent the previous message from the train via phone). Anyway it takes 2-3 hours to send emails.

I see. Just in case, I didn't mean to be offensive. Thank you for all that you are doing for CodeForces!

i will participate,when ever, what ever happend..

my laptop battery is empty due to power outage for more than 12 hours now... wind storm cut Power cables .. so I did not participate. my luck ..

I'm really thankful to Zlobober and MikeMirzayanov for one more CF contest.Good luck for everyone.Only high ratings. :)

My score around the 1590 some times,hope I can overstep 1600,good luck to everybody!

I can't overstep 1600 again,so sad.final test C is wrong answer.God always jokes me.......I must be overstep in tomorrow

Delay :|

To increase the participation btw less participation this time :(

Postponed by 5 minutes... You really want to confuse people...

"UPD2: Round is postponed by 10 minutes. Stay tuned :)"

it only says 5 on contest page

*5 minutes

Fun contest! Enjoyed the problems, especially Div 2 D and E (Div 1 B and C).

is there anything to do with MST in div2E

No, there is no MST

There is Strongly connected components of directed graph.

But I'm not sure if my solution correct or not. But pretests were passed

How did you solve E?

Didn't solve it in time, but I think I had the right idea.

Basically, you create a dependency graph. That is, the vertices in the graph are the datacenters, and you draw an edge from datacenter A to datacenter B if bumping A's time an hour would force you to bump B's time as well. (That is, if an item x is stored in A and B, and B's time is the hour directly after A's time.)

Then, the problem boils down to: I want to find the vertex in this graph, for which, if I pick it, the number of vertices that I am therefore forced to pick is the smallest possible. (In terms of the graph, this means that I want to pick the vertex v, who can reach the fewest number of other vertices.)

So, we can observe that in any dependency chain, or dependency tree, or dependency DAG, we can always just pick some datacenter at the end of the dependencies — i.e. some datacenter, which might have edges coming into it, but no edges coming out. Then, if we pick that guy, we aren't forced to pick anyone else. So our answer is 1.

However, if there are cycles, then it's trickier. If a vertex is part of a directed cycle, then we can't just pick a vertex at the "end of the chain", since there is no end of the chain. Picking any vertex (datacenter) in that cycle forces us to take EVERY vertex (datacenter) in that cycle. More generally, taking any vertex in a strongly-connected component implies that we have to take the whole SCC. So, the problem then boils down to: we want to pick the smallest SCC that has no outgoing edges. We can use Tarjan's or Kosaraju's to get the SCCs of the graph, and then find the smallest SCC with no outgoing edges.

good explanation , I was thinking like what happens if chain length is more than h then it would be contradicting but if chain length is more than h then it wouldn't form a cycle .

Hacks?

我是中国人

我是朝鲜人

我系渣渣灰

我系古田螺

我系及及凭

我是美国人

Hmm... does anyone know about pretest 9 of Div1A/Div2C and Div1C/Div2E?

Can anyone give the full solution of E? Mine had something to do with the minimum sized SCC of the induced hour-based graph but failed on pretest 4.

I basically did SCC, and then you know that SCCs make a DAG, so you find the vertex with out-degree 0 in the DAG that has the smallest SCC size.

Note: SCCs are of the graph where (v, w) is an edge if (v, w) share some data and (u[v]+1)%h = u[w]

I did same and my code failed on pretest 5

My solution is also based on SCC as well. I used Tarjan's Algorithm to have the SCCs sorted topologically.

Anyway, it seems like I know what I did wrong, but I'm not so sure (since I couldn't find for myself any cases to prove it). If I was right, the mistake was in my way to choose the correct minimum-size SCC to answer.

My original code for Div1C/Div2E got WA on Pretest 9 and I found a bug. For the test:

5 6 4

3 2 1 0 3

5 4

4 3

2 3

2 5

2 4

1 4

My code would say the answer is 5, but the answer is 4 (just take 2, 3, 4, and 5). I don't know if yours also failed the same way, but the main idea was that cycles broke my initial algorithm.

Well sadly mine returned the totally correct answer. Should be another case...

My first wrong solution failed on 9-th pretest. The antitest for the solution was:

One of the correct answers is:

When mine output

`-1`

. Check yours, hope it is what you search for.Thanks, but my solution won't fail this test. It returns:

may be it may help. 01001010 answer: 2 7 1 2 3 5 6 7 8 1 4

Yes, my solution failed on this one. Thanks ;)

It is just 000111000111...111000 (block with len 3 repeated 66665 times).

Can you clarify more? Are they 66665 three-digit blocks alternating?

And the answer isn't

`-1`

, right?(000)(111)(000)(111)(000)...(111)(000)

Answer is yes.

You can see that one of the answers is 3-sequences length 66665:

`0101010...01010`

Ouch, I understood my mistakes now. Perhaps I should redesign another approach for this problem.

Thank both of you, ch_egor and nikich340 for helping me out! ;)

Yes, your answer output

`-1`

on short version of that test:`000111000111000`

bad explanation for B div2 :|

Div2B statement is horrible.

How to solve Div2 D?

Yes, I saw that 549 passed it and feeling that I'm a bit stupid :P

I managed to solve only the inverse task (find final position of some number).

Here's a kind of hand-wavy explanation

The optimal strategy is to always move the rightmost number.

If you simulate a decently sized example (like n=8), you can note that essentially every odd-indexed number stays where it is, and there's a steadily-moving contiguous "blob" of numbers moving from the right to the left.

Given an index i, we want to find out the index that it originally came from. To do this, first, let's try to find the index that it came from on its most recent jump. Let's say n=8, and we're looking at index 4 (using the 1-based indexing in the problem). (I highly recommended drawing out this example and simulating it. If you've done that, you'll see that the number that ends at position 4 is 7. So we want to find out where 7 was before its last jump.)

Every jump that takes place is of the following form: it's a number leapfrogging from the right of the blob/train to the left. And it's leapfrogging over ALL the numbers in the trainblob.

So, if we can figure out how big the blobtrain was at that point in time, then we can figure out how far our number leapt to get to where it is now. Well, we can note that every number that started to the left of index i has not moved yet. And, every other number besides those must be part of the train. So, the amount of numbers in the train is n — numbers_to_the_left_of_i.

By examination, we can see that there are i/2 numbers to the left of i. So, the amount of numbers in the train that 7 leapfrogged over was n — i/2 = 8 — 2 = 6 (including itself). So the position that it came from was i + (n — i/2). And before the 7 was at that previous position, we can calculate its previous-previous position in the same way. We can iterate that i = i + (n- i/2) calculation until i is odd, which is its starting place. And the value of the number at a given position p is (p+1)/2

edit: Tl;dr read xiaolongbao's comment

How do you do the inverse task?

I found position differences for all numbers in small array(8, for example) and find the next pattern:

So, initial position for number

xisa= 2x- 1, initial position difference isk= 1 + 2(n-x), which doubled on each iteration. Reduce number position while we can do it.Same with me .

that is all, if u still have problem can reply me

any logic explanation?)

u can see the kwangg's explanation. it's very detailed

now, i explain my code

it's obvious that if the num which less than n/2 will fixed

for example n = 6:

1 and 2 and 3 is fixed

so

if u wanna find the ans of position 4

when a number will go to position 4 , you can know that there is no space in the back of position 4, and the number which position is 8 will go to position 4, so u just need find the number which will go to position 8, then recursion.Until the beginning, there is a number in this position. Obviously, at the beginning, the number must be in the odd number.

I belive that everybody wrote this contest right.

Anybody initially stuck on test case 9th of (Div2 C/ Div1 A) and then later able to successfully surpass that??

I m still getting WA on 9th test case :(

Hope will help: http://codeforces.com/blog/entry/58229?#comment-419309

Yes. Though I was stuck in this case for only 5-10 minutes, still this might help. First of all, the test case is 000111000111000...000 for 66665 times. The length really doesn't matter, the shorter version f this input is

000111000111000. What you may have done wrong is that you may haven't considered detecting01010 type of strings with multiple 1's. In other words, this test case is the combination of0101010 (long zebra string)and001100 (overlapping zebra string). I handled each of them separately, but test 9 got me. Got AC after correcting this bug. I hope this helps.A straightforward implementation of Div1A/Div2C using sets passes.

See this http://codeforces.com/contest/950/submission/36136531

anything special about test 15 in F? And should there be answer YES or NO?

Just some random test with answer "Yes" and n = 2600.

Anything special about test 7 :D?

Small manual test.

Duh, I got WA on it because of overflows and I lacked few seconds to submit version with define int long long which already passes this test. I hope that my final version will not get AC (I think it won't), otherwise I can be declared incredible loser :P.

I tested it locally and it fails, so it's not so offensively.

Can anybody please explain the complexity of my code for problem Div2 C. I think it will fail system test.

Div 2 D?

If you're looking at an odd index, print the number that was there originally, i.e.

`(i + 1) / 2`

.Otherwise, assume that that location is currently the rightmost blank not filled in. For example, with N = 7, if we're looking at index 4:

`1_2_37465`

The number which will occupy the second blank is equal to the rightmost number in the current list. There are no blanks to the right of index 4, and there are 2 numbers to the left of index 4. In general, there are

`i/2`

numbers to the left of a blank. Thus, there are`N - i/2`

numbers to the right, and since they're all filled in, we can say that the answer for index`i`

is the same as the answer for`i + N - (i/2)`

.codeVery nice explanation. Thanks.

any intuition on how to calculate the time complexity of the above solution?

Upd: Nvm figured it out. this seems similar to a binary search relation where Right is always 2*N and Left keeps becoming MID.-->(left + right)/2 --> i = (i + 2*N)/2.

Two identical codes with just reformatting from moamen_ahmed and BaSSam_RaMaDaN.

Here are the submissions: 36101349 and 36100512.

all coders using two pointer -_-

Just a joke, nevermind.

What you mean ?

ok nvm also :P

That's obviously cheating and anyone who can think can catch it.

btw, here are 2 more submissions from the last edu round 36010393 and 36017149.

We aren't robots, we can think and decide wisely :))

Nice try, easy problems and two persons have the same training in the university and in the same team in ECPC, not so weird :P, focus on your local training/ranking plz

Please, let's face ourselves with the truth.

I swear I'm doing this just for moamen_ahmed, he is getting better, but with that way he will never be great, Also I know him personally.

thanks, my mentor will focus on that :P

Oh boy..

Idea for div2 E?

http://codeforces.com/blog/entry/58229?#comment-419327

thanks

Congratulations zscoder !

Thanks for the wish :)

I get motivated by your rating graph. I also showed my friends. This is what never give up. Congratulations...

I am a newbie and I feel sad. I know much more thing but in contest 1-2 problem-solving.After the contest I have done the problem but problem in the contest. Please give me some suggestions so I can improve my coding skill. I am waiting.....****

I also congratulate to Yuta yutaka1999 Takaya for being legendary grandmaster!

How the hell could this guy jackichul solve D just 3 mins after E? And his solutions of D and E also had different style from A, B and C.

36111831 can anyone help me to find out the runtime error??? TIA

That feeling, when you realize your solution is wrong and you know how to fix it :) But you had already locked the problem...

Problem div2 E & F have such long statements!

SOOOOO LONG!

Reporting a cheater on this round.

How to lose 10 places for nothing:

Write

`endl`

instead of '\n' in B, but pass pretests anyway. Panic and resubmit.Hello Sir. What is intended solution for problem div1. F. My friend from Russia say that author solution is . Is it true? KAN ch_egor sender flyrise _kun_ malcolm gritukan _meshanya_ GlebsHP Endagorion

No. Author's solution is

O(n^{2}). Stay tuned for the editorial.My solution (that should be quadratic on a random instance) gets TL on test case 17. Not surprisingly.

What is wrong with the 55th test case in problem Div.2E/ Div.1C?

Most of my friends using Tarjan get WA, while those using Kosaraju not. Wondering too.

I got correct answer using Tarjan

I used tarjan and got accepted too.

q

Div1D/Div2F solution?

How to solve problem Div 1 D ?

My idea is as follows -

From the initial arrangement, greedily try to send right half ( = n/2 * b) of the students towards right. And remaining towards left.

At any instant, when first instructor is at room i. All students till room (d + 1) * i can reach to the room. So at moment we know maximum how many students can reach room i. Call this

`lsum`

. Also, let's keep track of how many students are are locked in rooms with room number less than i, call this`lused`

.Now if

`lsum - lused >= b`

we will lock`b`

of them in current room. And conceptually push remaining to next room,`i+1`

. After this,`lused += b`

.If

`lsum - lused < b`

we will lock`lsum - lused`

of them in current room. In this case we increase counter,`c1 += 1`

indicating this will be reported by the instructor.Similarly we will proceed from right side, (left and right both proceeding simultaneously).

In case of odd n, simply ignore the middle room. Because it will always have

`>= b`

students.Implementation wise, I am moving two pointers

`l`

and`r`

so compute the`lsum`

and`rsum`

for each i. Code 36115092Where am I going wrong ?

Update :Nothing is wrong with this solution. Fixed implementation issue. Correct code 36121095I guess this

`If lsum - lused < b we will lock lsum - lused of them in current room. In this case we increase counter, c1 += 1 indicating this will be reported by the instructor.`

is wrong.By comparing your code, you can find it is better to let

`lsum - lused`

go to right room instead of lock them when`lsum - lused < b`

How do we solve Div2C fast? I tried 3 times and got TLE on test 8 for all 3 tries ... Can anyone give me hints or ideas?

You can keep the basic idea from your solution.

The reason why you get TLE is because you spend so much time searching for the next index

`i`

(O(n) in the worst case). You can find the index inO(1) time, if you keep track of the`arr[i]`

which end in`1`

, and the`arr[i]`

which end in`0`

. You can use two stacks/queues`ending_in_zero`

and`ending_in_one`

which store the indices.Wow thank you very much that was very clear to understand.

Just out of curiosity, how did you find my submission?

Your profile -> Submissions

Oh I didn't know I could view other people's submissions.. thanks!

I guess it can be found easily in your account, isn't it?

But, what would I maintain a set?

36120744 Time Limit :'( How can i reduce my complexity ? How should i do to ignore erase function ? please ,help :)

u can use two stacks, one for keep track of the index of the sequence where 0 is the last element and other for 1...

long live ,man! I was sending a message to you . :)

Perfect time for Chinese users!

I had solved Div 2 C in paper, but I couldn't implement that. My solution was something like: If first element is 1, then obviously output - 1, so assume the first element is 0. Now create a string

S_{0}and setS_{0}= 0. Now read of the digits after the first digit one by one: (a) If it's 0, then if there's a stringS_{j}ending with 1, add 0 to that string, or if they don't exist, create a new stringS_{i}= 0 and keep track of it. (b) If it's 1, then if you can find a stringS_{k}ending with 0, then add it to the end of that, otherwise if you can't find output - 1. Add the end, output the elements which form your strings one by one in each line.Now the reason I couldn't implement is that I couldn't figure out how you're supposed to keep track of the strings and the array indexes which create the string without creating an 200000 by 200000 array ? How you do that ?

Why would you need a 200000 by 200000 array to track the strings? You could maintain two lists of strings, one for the strings ending with zero and another list for the strings ending with one. You should be able to pick the a string from one list and move it to the second list in constant time.

OK, but how do you maintain a list of lists without knowing what will be the size of either one after the algorithm runs ?

you can use 2D vector for that case

You can use a linked list for example

What was the test case #8 in the problem Div 1 A? I have tried running my code on a lot of possible test cases and I am not able to find a test case that can cause a runtime error. I have even put an infinite loop to get a TLE in all possible situations where I might get a runtime error. Any help with it?

Your code get RTE when the input is a string of length

`n = 199998`

such that - first`n/3`

chars are`'0'`

and last`n/3`

chars are`'0'`

and everything in between are`'1'`

.Tested on custom invocation. Btw, why use

`goto`

?The error is because of order of

`line 44`

and`line 45`

.When you erase from the set, the iterator

`it`

may have changed. Infact, the earlier`it`

may already have been dumped (`free`

) and is hanging. And you are de-referencing that to assign value in`pos`

.This can be fixed easily if you reverse the order of those two lines. I hope, its helpful :)

OMG, wasted entire contest in finding this error. It was not triggered on my system upon generating so many tests. Thanks a lot for the help :)

I used goto because while writing code, I thought that there will be many places from where I will go to DONE, but turned out there were not much. In retrospect, I should have removed that and replaced it with another break statement.

Where is the Editorial? :(

Hi everybody!

Thanks again for participation, we were really busy with the awards ceremony of the onsite round here in Moscow and I've literally only got back home from the Olympiad.

The editorial will appear tomorrow, we were not fast enough to prepare a well-written text editorial for the round yet.

Maybe that will look more like bragging but I consider this funny enough to point this out.

I already:

1. Took 2nd place on Codeforces

2. Took 2nd place on TopCoder

3. Took 2nd place on AtCoder

4. Took 2nd place on CSAcademy

5. Took 2nd place on Hackerrank

6. Took 2nd place on ACM ICPC WF

but I never won any of these xD.

Moreover in high school biggest deals for me were Polish and international olympiads in math and informatics and I:

1. Took 2nd place in PMO

2. Took 2nd place in POI

3. Got silver medal on IMO

4. Got silver medal on IOI

Already at that time I received nickname "silver human" and it seems I proudly continue to follow this path.

Still waiting for the Editorial...

BTW, div.2 E has so many test cases...

But finally I got Accepet, yeah!

How did you solve it?

Can you please take a look at my code http://codeforces.com/contest/949/submission/36178442

got WA on test 101 :D

got it.

div2-D.

I find out the sequences how A number changes its position .

but I can't understand How I know which number is in the given index/cell . Any hints?????????

Div 2- Problem C

The question has greedy tag, how can one identify if a question is based on greedy algorithm or not?

Just a final update: the great editorial by gritukan and ch_egor is there, be sure to check it out.

See you in next Codeforces Round in cooperation with Moscow Olympiads!

Please someone help why am i getting TLE in ques C

http://codeforces.com/contest/950/submission/36191394

thank you

It works in linear time for std::set.

"On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average."Use

Thank You :D

How many tests does 950A have?