Hello, Codeforces!

I am glad to invite you to take part in Codeforces Round #577 (Div 2), which will be held on Sunday, 4 August at 16:35 UTC.

You will be given **5 problems**, one of them will have 2 subtasks and **2 hours** to solve them.

The round will be **rated** for the second division.

Huge thanks to _kun_ and KAN for helping to prepare the round.

I would like to thank to 300iq, isaf27, V--gLaSsH0ldEr593--V, pllk, mohammedehab2002, tractor74.ru, Rox, opukittpceno_hhr for testing the round.

And thanks to MikeMirzayanov for the great codeforces and polygon platforms.

This is my first Codeforces round. Hope you will enjoy it.

Good luck and have fun!

**UPD:** The scoring distribution is: 500 — 1000 — 1500 — 2000 — (2000 + 1000)

**UPD2:** Editorial

**UPD3:** Congratulations to the **winners**:

Div2:

1: wwaynetuu

2: jerome_mei

3: satu0king

4: Yazmau

Unofficial Div1:

1: neal

2: uwi

3: scott_wu

5: ecnerwala

All the " Author's First Codeforces round " are always carefully designed. Can't wait attending this contest.

Wish you the best of luck for organising your first contest

Which question has two subparts? Please share the points of all problems.

Does it matter?

Usually you know whith the scoring distrbution

I told you It doesn't matter XD

The time is not friendly for Chinese

midnight is acceptable for OIer

Also not friendly for Vietnamese too

I hope it's the first of many.

We expect problems would be interesting and this round will be remarkable for PARTICIPANTS & JUDGES.

Which problem has two subtasks?

And the what's the markes for each problem?

qwq. I don't know. Probably is D1 and D2. I guess :)

But it's E1 and E2 XD

i have a question... current my email "not visibled" in codeforces, how to fix???

It means other users can't see your email.If you want to show it,you can change it in "settings".

thank bae :))

Hopefully, I will become spec.

Keep me posted.

why do you want to become spec with your fake account ? try to become pupil with your real account AriaBanazadeh

previous contest I was newbie and it is not my first contest with this account so I had the same rating as my account. and anyway it's not your business.

okay man . it was just a suggestion... i couldn't understand what you wrote but I'm sorry if i upset you.

good luck!

Hopefully, You will still pupil and don't become newbie

+

Hope C1 and C2 I want rate inflation

SpoilerWait, I rarely solve C in live contests :|

Good luck

coding sucks

what a stupid comment... how the hell is he getting these upvotes?

why are you replying yourself...

This is why

Hope this round goes well.

As always, I find out everything at the last moment( Good luck to you to!

Как всегда, узнаю всё в последний момент( Удачи и Вам!

It's interesting that writing of contest problems is a free creativity. So many authors use imagination and fantasy almost without limits, sometimes even insert jokes.

i love good jokes

here is one:

what did the microscopic cow say?

Spoilerμ

This is anime cow

Meow -> nya

Moo -> μ

i really do love farmer john! μ~~~

It's a most funny joke that I see in problems

B. Tavas and SaDDas

Once again Tavas started eating coffee mix without water! Keione told him that it smells awful, but he didn't stop doing that. That's why Keione told his smart friend, SaDDas to punish him! SaDDas took Tavas' headphones and told him: "If you solve the following problem, I'll return it to you."

This is my first time！ gogogo~！

Guess I'll be master after this round.

LOL

You can try

There are fewer people take part in this contest.

Maybe it is too late,but I'll still enjoy it!

I hope the problem statements are short and clear.

I exactly agree with you :)

Please don't be stuck, server

When codeforces adds another speedforces.

WHY CANT I HACK PROBLEMS?

me too =))

And doing it really slow with tons of WA... Rip rating

relatable

Maybe you shouldn't post meme which everyone see thousands of times?

When you wait the end of system tests and the change of rating

[Same picture]

I wish I were you :)

SO TRUE!! i did the first three so fast but after that i was just staring at my moniter thinking

D is too hard for D.

And E is too hard for E as well

D is not too hard. It is too shitty.

What is the solution? I feel fucking stupid. :'(

think it's just some dp on leftmost/rightmost element in each row. lots of typing

DP. Iterate over rows and for each treasure holding how fast you can reach that treasure with all treasures in the same row be gotten too.

That's just my thinking. Late for submiting. RIP rate:)

I think modifiable BFS or something similar.

$$$dp_{row, lasty, lastcol}$$$, where $$$row$$$ is the number of row (we need to consider only rows with at last one treasure but in fact it does not matter), $$$lasty$$$ is either $$$0$$$ or $$$1$$$ and means that the last $$$y$$$ on the current row we visited is the leftmost $$$y$$$ of some treasure on the current row or the rightmost such $$$y$$$. And $$$lastcol$$$ is either $$$0$$$ or $$$1$$$ too and means that after visiting the leftmost/rightmost $$$y$$$ with the treasyre we go to the left safety column or to the right safety column from this $$$y$$$. Transitions are very easy to understand but hard do code.

Can you elaborate more ? I am not able to understand :(

Since you can go only up, never down, you have to collect all treasures of one level at once. This end in standing on the leftmost treasure of one level or the rightmost one.

From these two positions there are again four paths to the left/right treasure of the next level.

These paths are simple. You can allway either go left, then up, then to the treasure, or go right, then up, then to the treasure.

So, you need to write that down in terms of dp recurrence, which is no rocket science, but nasty index fiddling.

Said Vovuh and get WA)

You lost chanse to become a master on this contest((

You can see the bug in diff between WA42 and AC. It is so stupid. Just like me.

In the contest I had more stupid mistake in this problem)

But, I fixed it)

Exactly, it is just tons of typing. And E seemed like this as well :(

TypingForces. Also D is unusually tough.

I can't see B solution meh feels stupid

How to solve B?

You can always make everything to zero if and only if :

Try 3 3 3 5

???

I suppose you mean 4 numbers, (3, 3, 3, 5).

(1, 4), (1, 4), (2, 4), (2, 4), (3, 4), (1, 3), (2, 3).

Or if you meant 3 numbers (3, 3, 5), answer is NO since the sum is odd.

3 3 2 4

Use the last element with the first three, then it's 2,2,2,2 and it's easy.

Could you explain the second point please?

Try to think number $$$x$$$ as $$$x$$$ balls of one color. We can match a ball with another ball with another color (pair with same balls is not allowed). Then, we note that if there are odd balls, we can't complete matching. This is first point. If we have too many balls of one color (by "too many", I mean more than half of all balls), by the pigeonhole principle (although it is pretty straightforward), at least one pair must contain two balls of that color.

What about 2 2 2

please is there a proof for point 2?

If a color has cardinality over n/2, there is no solution because you will need to pair up two elements of a color at least once. Otherwise, we construct a solution as follows: make each move by choosing of the two largest groups of colors. It is intuitively clear to me that making this kind of move will ensure that, in subsequent moves, no color gets to cardinality over (total number of balls)/2. And if we always preserve this property, there will always be at least 2 piles.

ohhhhh, makes sense. Thanks

I also thought first one but the second was too away from me :'( how did you find this solution? (I mean the second approach)

It seems quite awkward for me to explain how I approached this problem since I struggled twice (If you take a look at my submission, you'll notice that I tried 2 completely different logic which both were not working).

However, I think for this problem some caseworks can help. I basically noticed second approach when I thought of following case :

2 numbers, (1, 7). Sum is even, but we can't do the task.

I also noticed that (1, 7) case but I couldn't bring more :(

Maybe more casework would have been helpful. Restricting to 2 or 3 numbers (which is hand-doable)? Such as (1, 3), (2, 2), (2, 3), ... and thinking with more data.

How do I prove it?

Here is the idea of proof

Try 12 3 9 4

how do you know that these are all the necessary conditions? How do you know there aren't more? I thought of these in the contest but didn't think it was that simple

can you prove it?

sum all elements. Than if the sum is odd then return "NO" else check if the largest element is bigger than the sum/2, if it is than "NO" else "YES"

Just think nothing!

How to solve D?

I think DP or Modifiable BFS possibly.

Unfortunately, there were no server issues today as well :(

Is N^4 the intended complexity for E1 ?

What is pretest 5 in B ?

4 1 1 1 3

My code is giving YES

my code gives yes and logic also say yes but got wrong answer

Try 3 2 2 2

My code is giving NO

Answer is YES

NO -_-

How?

2 1 1 , 1 0 1 , 0 0 0

$$$1<=a[i]<=1e9$$$

i just think 3 2 2 2 --> 1 0 0 0 -_-

sorry. I have entered , but don't know why.

Ohh.

Pretest 5 for problem B is: 5 1000000000 1000000000 1000000000 1000000000 1000000000

Answer is YES.

let's make a simplified sample

how can this results YES ?!

Logic behind B?

sum(1,n) % 2 =0 and sum(1,n-1) >=a[n] with array a sorted -_- -_-

bad at english sorry -_-

Think about this test case: 6 6 4. I think you will understand the logic behind solution of B.

Got it...Thanks.

If the biggest element is not larger than the sum of the rest, then it is possible. If the sum of all elements is even, of course.

Basically, if all this holds, then you can take pair "smallest element — largest element" and decrease them until the smallest one becomes 0. Then you just reduced the number of elements, but the two main conditions still hold. You can show by induction that you cannot end up in a situation when you have only one number left -- otherwise the first condition did not hold from the very beginning.

wow!

Consider the following array: 4, 17, 19. Sum % 2 == 0 and 19 < 17 + 4. So it should work.

If I apply your method of taking smallest — largest pair and decreasing the following happens:

First step, take 4 and 19 and reduce and you get 0, 17, 15. Next, take 17 and 15 and you are left with 0, 2, 0.

Did I misunderstand anything?

You don't have to reduce 4 all the way to 0 because when we have 1 17 16 then we will decrease 1 and 17 because they are the biggest and smallest right now. This will retain our claim

Ah I see, thanks for clearing it up.

:)

So, basically you have to sort the whole array after each turn to make sure the arr[0] and arr[n-1] are getting reduced by 1 where they are smallest and largest pair. Got it. Looks a lot simpler this way.

I solved without sorting. 'YES' if sum of all elements % 2 == 0 and there's no element which is more than all other values' sum, 'NO' otherwise

What in case of 2,4? Will it not be 0 2??

4 > 6/2. Condition does not hold. Should be "NO".

any guesses on test case6 of question 3??

Test 5 B == killer.

compilation error on Test 1

Anybody know what pretest 7 in D was?

Dafuq was B omfg

Logic behind c?

binary search

More precise please

If you want the median to be at least X, you need at least (n / 2) + 1 elements to be greater or equal than X.

Nice Approach

Then binary search the value of the median. Let's say the value you want to check is X. Then you have to check, whether you can increase all numbers with indexes from N/2 to N up to X, using only k moves. For this you need: a) prefix sums of array elements, b) to search the first element in the second half of the array which is already larger than X (also binary search).

You sort the array. Find out the middle element each time. Increment it by 1. But array must remain sorted throughout. Thus, a multiset might help. (I didn't implement it during contest, but I guess the logic is correct. Correct me if I am wrong)

I tried this solution but WA in pretest6

binary search gave me TLE on 2 solution one n*log(n) & the other log(n)² , had to implement the O(n) solution

If you implement binary search correctly, it must be sth like ($$$n \log n + n \log A$$$) where $$$A = 2e9$$$ and $$$n = 2e5$$$. BTW I would really like to learn how $$$O(n)$$$ solution works...

sorry was wrong it's o(n)+n log(n) bcs of the sort at first, the idea is to start from middle and check if you can make all the numbers from middle to current index equal to the next element value on the sorted array, and substract the cost. sol

Your binary search was TLE because mid was overflowing. int mid = (l+r)/2; l = 10^9 and r = 2*10^9

I implemented binary search but got WA on pretest 8. can anyone please check? http://codeforces.com/contest/1201/submission/58296147

but INT_MAX = 2147483647

Sum is coming 3*10^9 which is greater than INT_MAX

A, B, C are standard problems. D is a good problem. For this thank you author.

Can you explain D ?

Can anyone please tell me how to solve A? (seriously :)

i wanna to ask why my code wrong on pretest 2. https://ideone.com/rwIkPi. they say it is undefined behaviour, but i didn't figure out where it happens.

B reminded me of AGC 036 C.

By the way, could somebody hack my solution for D? I solved in a dijkstra-like style. I checked 998244353 times but I couldn't find my bug.

My solution is working fine on custom testing on cf for all sample 3 test cases . But failing for 1st test case while submitting. can anyone plss check it

https://codeforces.com/contest/1201/submission/58300351

So what about your mistake? At least, now custom invocation shows "10000000000000000", not the right answer.

OMG, edit distance was 1...

what is the mistake?

SpoilerWhile iterating through the key-value pair of map, I tried to sort each vector on the map. However, I forgot to write "&" in the variable declaration. As a result, each vector was copied into a local variable and that vector was sorted, while the original ones were not.

Always be aware that in C++ every assignment is regarded as copyunless you use reference or pointer.Wrong:

Right:

When you propose a contest, you is asked to indicate the features of the tasks that are given. To whom what today's tasks seemed unusual and interesting? What is their feature? Who, after solving the problem, got the joy that you came up with a cool solution and it came in? Personally, I did not like any of the ABC. Moreover, I didn’t have joy after the decision of the old C. In addition, D and E, in my opinion, are very difficult for D and E. Sorry from my poor English

My solution is working fine on custom testing on cf for all sample 3 test cases . But failing for 1st test case while submitting. can anyone plss check it

https://codeforces.com/contest/1201/submission/58300351

Hey guys, Could anyone please explain to me what is the hack means for contests?

If your solution's imperfect(even though you passed pretest, it's possible), the others who locked that problem can hack your solution with data which is counterexample.

Could you please more clear :D I am new for contests

During the contest, your submissions are only tested on a limited set of test (pretests). It's possible that an incorrect solution can pass pretests, so during the contest, other people in your room who have solved the same problem can choose to lock their problem and "hack" your solution (provide a test case where your soln will fail). Hackers are awarded +100 pts for succesfull hack, -50 for unsuccessful.

Got it :), Thank you very much.

The pretest doesn't cover all of test data. So although you passed pretest, it can be a wrong answer. After pretest passed, you can lock the problem. It means you cannot change your answer and you can read other user's code. When you reading code, if someone's code have counterexample, you can enter counterexample and if the hacking attempt's successful, you can get 100point. but if it failed, you lose 50point. Can you understand? (sorry for my poor english)

Yes finally I understood :), Thanks a lot for your reply

Wasn't the contest a bit unbalanced? Still, quite a good contest for a first timer setter. :)

In any way, I think we have to thank for all efforts :)

Problem D looks similar to a recent task from TCO, except that the one from topcoder restrict the number of safe columns to exactly two.

--deleted--

`(le + ri)`

may not fit in signed int.Yup. Thank god towards the end I realised this and got it AC.

Someone got overflow...

A nice version of problem D: 1066F - Yet another 2D Walking.

Okay, why is E1 worth 2000 and E2 worth 1000? E1 is nothing but dumb coding that doesn't even require much thinking from my perspective, while E2 is a beautiful (in my opinion) and hard problem.

Subtasks are getting a bit annoying on CF :(

From perspective of Div2 users both of them are almost unsolvable)

P.S. Sorry, not almost:D

"Dumb coding"

When even with the unoficials contestants, only 2 persons for 7000 contestants solved it, and that even high rated like scott wu, ecnerwala and a lot others red contestants couldn't solve it, I'm not sure the problem was so "dumb".

Well, they tried to solve E2 instead of E1

Yeah, I didn't consider this

Do you see better scoring solution under assumption that subtasks are there?

I agree that it sounds questionable when you look at it from this perspective, but it all kinda makes sense. Proper solution gives you E1+E2, for which 3000 is reasonable. During the contest I spent 25-30 minutes thinking on E2 before abandoning it — and the case work that I had at that point was more complicated and messy than the editorial.

Yes, E1 is straightforward coding which requires even less thinking than D — but it is, by div2 standard, quite a lot of tedious coding. I don't think it would be fair to say that 58306483 is worth less than solving D. Is it really that wrong to look at it like

"either you just code the trivial part and get 2000 for it, or you solve a full problem for 3000"?Even though I may not have solved most of the problems, I quite enjoyed the contest. The questions were challenging and intriguing at the same time.

Can anyone recommend some problems to solve to develop skills to solve problems like B ?

A, B and C are too easy and D was too tough I think this contest just focuses on speed.

SPatrik, can you explain to me why did you need this statement that knights are allowed to capture each other? Does it affect anything except solutions that fall as mine because the case was forgotten when I need to capture an opponent`s knight? And why was not it covered by pretests?

Without capturing, the problem would be really easy. Just select the knight that can reach it's target faster. With capturing, there is a situation where you have to move more tricky. First go to the opponent target, then to your target. Read the editorial for more details.

Of course, I meant that it would have been greater if knights hadn`t been able to move under attack at all, not allowing moving under attack and being immediately captured afterward.

Yeah, that could be done. That would remove the possibility of those minor error. I didn't think that way. There wouldn't be any other differences, capturing just makes the game more realistic.

If you like this so, this case should be in pretests at least :). But as I would think in your shoes, the statement that doesn`t affect should be removed. Anyway, thanks a lot, I like this problem very much despite that mistake!

Are you kidding? A B C are too easy and D is so hard. it is not suitable as D.

For E & F , are you sure you were not make a kidding to us?

There is no F problem in the contest...

I think he wants to say E1 and E2.

Isn't standard DP fine for D? E was interesting.

Thanks to SPatrik for problem C. The same problem was given to the participants of Lipetsk Municipal Olympiad and some participants who wanted to qualify for Sirius (December 2018) about one year ago. So, I had the same problem prepared in Polygon and you helped me realize that my testset wasn't full and some authors' solutions weren't right. Thank you very much :)

for god sake WA on C because of int overflow, Shame ._.

WOW fast system testing

Can someone help me find out why is this submission getting MLE? 58305730

How deep can your recursion be? If it is too deep, it keeps all variables in the stack.

In the worst case the recursion's depth will be equal to the number of states which is N^4 * 2 which is ~ 5 million. Is it really enough to fill up 256 MB?

I do not really know, what exactly each call stores. For sure it is your method variables (passed + 2 internal), it is below 15 B though. But there should be something else, I guess.

I dont get why a lot of people failed to solve B.

can somebody explain

Can you explain how you find it to be easy ? Also can you recommend similar problems ?

the theoretical problems about invariance would help

Thanks, Any source you aware of ?

i didn't know any english source.

but i found THIS LINK for you. and the problems are good.

Is there an answer key for that link?

i didn't find any. you can ask here.

How to solve it ?

the sum of all elements must be even.(because we decrease the whole sum by 2 after each operation)

also the biggest element should not be larger than half of the whole sum.(because for every operation that we decrease the biggest element there should be another element bigger than 0 to decrease)

Ok try n = 4, element {12,4,3,9}

reduce 12 and 9 till you get

3 4 3 0

then

3 3 2 0

2 2 2 0

2 1 1 0

1 0 1 0

0 0 0 0

Thx

i forgot that i have to reduce by 1 instead was trying to solve it by reducing full number from other

They didn't guess that one of elements maybe greater than all another. In this case also there is impossible do it

First I thought that you can simply take two biggest numbers, and reduce them as much as possible — and then repeat this procedure until all become 0. But this logic does not always work, for example, for the case 99 100 101.

did this soulution pass the pretest?

No. And it took me some time to figure it out.

one should never overthink B otherwise, it cost him his rating

True, At the first glance I thought about a partitioning problem and this would have got TLE easily lol

This is my last Div2 Round. But I was late in submitting E1 about 10 seconds so I missed the last chance to win in Div2. QAQ so sad....

I was not sure that my solution is correct and my implementation is correct. So I inserted some assertions in my code. But I accidentally did a wrong assertion. So you can see that I got some REs in my contest submissions. And then I replaced those assertions with exit(0) so I got WA. Then I realized that my assertions was wrong. As soon as I finished to erase them, the contest ended.

Maybe it's not your last Div2 round :)

My rating did not update. Why?

just wait for some moments

Okay. It's my CF Contest. That's why I'm excited. Thanks

It will be updated a bit later, don't worry :)

Okay. It's my CF Contest. That's why I'm excited. Thanks

How this case could be "YES"? Problem B.

Test case 5 :

5

1000000000 1000000000 1000000000 1000000000 1000000000

Answer YES

Just because i dont want do write zeroes, here is explanation for 5 10 10 10 10 10

9 9 10 10 10 9 9 9 9 10 9 9 9 8 9 9 9 8 8 8 8 8 8 8 8 (and repeat until all are 0)

take half of 1000000000 and half of the next 1000000000 ,they cancel out one 1000000000 now we left with: 500000000 500000000 1000000000 1000000000 now it's trivial...

Got it thanks.

make it

1000000000 1000000000 1000000000 0 0

then you can make them

1000000000 999999999 999999999 0 0

999999999 999999998 999999999 0 0

999999998 999999998 999999998 0 0

as you can see ,you can reduce all of them by 2 after 3 operation. so you can make all of them zero as well

A similar example is {2,2,2,2,2} you can take operations like {1,1,2,2,2} {0,1,1,2,2} {0,1,1,1,1} {0,0,0,1,1} {0,0,0,0,0}

can anyone help me finding error in this code

What wrong with me? It's second consecutive time I passed pretests and got wrong answer in system tests. It's not wrong approach, it is overflow integer. So sad for my rating.

How to solve E? Is it SG function or something else?

The code of neal’s problem D has a problem.In its last loop,safe_index might equal to safe.size(),and safe_col has random value.It might lead to “wrong answer”.(Sorry for my poor English)

Problem D:Could anybody tell me why I always fail on test 13? After reading the editorial,I found that my idea is correct.Maybe some details!

code:58360651Check this case: 2 2 1 1 2 1 2 (Ans:3), here the first row does not contain any treasure.

In problem div2 C i think like the position of median is not changing right. so sort the array take sum from mid to n add k and divide it with number of element from median position to end that will give the max median . //{sum of(a[position of median to n]+k) divide (n-mid)} but its not working for all case. somebody have any idea