Hello! Codeforces Round 787 (Div. 3) will start at May/05/2022 17:35 (Moscow time). You will be offered 6-7 problems with expected difficulties to compose an interesting competition for participants with ratings up to 1600. However, all of you who wish to take part and have a rating of 1600 or higher, can register for the round unofficially.
The round will be hosted by rules of educational rounds (extended ACM-ICPC). Thus, solutions will be judged on preliminary tests during the round, and after the round, it will be a 12-hour phase of open hacks.
You will be given 6-7 problems and 2 hours and 15 minutes to solve them.
Note that the penalty for the wrong submission in this round (and the following Div. 3 rounds) is 10 minutes.
Remember that only the trusted participants of the third division will be included in the official standings table. As it is written by link, this is a compulsory measure for combating unsporting behavior. To qualify as a trusted participant of the third division, you must:
take part in at least five rated rounds (and solve at least one problem in each of them), do not have a point of 1900 or higher in the rating. Regardless of whether you are a trusted participant of the third division or not, if your rating is less than 1600, then the round will be rated for you.
Thanks to MikeMirzayanov for the platform, help with ideas for problems and for coordination of our work. Problems have been created and written by ITMO University teams: MikeMirzayanov, MisterGu, myav, Gol_D, Aris, senjougaharin, me Vladosiya.
Also many thanks to avevad, yorky, UncleSema, vsinitsynav, GoracioNewport, Tvorozh0k, any_nickname, I.AM.THE.WILL and Jostic11 for testing the contest and valuable feedback.
Good luck!
UPD:Editorial
Hope the pretests are stronger in this one.
Can you explain what happens if pretests are weak? I'm new in Codeforces and exploring the rules.
Test cases which are used to check your solution during contest are pretests; subset of the actual test cases.So sometimes it happens that the solution pass the pretests but it's actually wrong.
Thank you so much for such a clear answer!! <3
As it is written by link
Well, the "Remember" hasn't been written by link yet :)
UPD: Thanks, it is updated.
Hoping that everyone does great and everyone gets a positive delta in this contest. All the best everyone!!!!!!
Yeah,hope I will get to 700 after this contest!
All the best .
And to you. By the way, I want to ask,how are you able to send to messages in ten minutes?
Hope I reach candidate master after this round!****
Good one dud. But if you are serious, try AKing a Div2 round or smth because it is virtually impossible to go from pupil to CM in one Div3 round.
Excited for my 2nd contest
My 3th contest! Good luck!
never give any contest for increasing your rating. Just give it to learn something new, and rating will definitely increase sometime!
Hoping to reach Pupil!
Marinush
Hoping to reach Newbie!
tourist
When rating is calculated modulo 4000
I suggest to modulo 4001 because it's a prime number.
I have a question, before the contest div 3 before rated, i had been a specialist, and i register at this contest, so they said that i will be rated, but after the contest div 3 before rated, i am a expert, and they( codeforces) said that i was trusted participant in contest (dont have asterisk before name in register in this contest ), so I will be rated or unrated? Thank you.
Unrated
I think you should cancel registeration and register again.
I hope the contest be good like the latest one yesterday
do you expect a more-than-1000 hacked problem?
Good luck and have fun guys!
Pray for downvote
Pray for upvote.
As a tester, I wish all the beginners good luck!
Hope I reach Radiant this round
Hello MikeMirzayanov,BledDest,vovuh,adedalic.System
I find out some plagiarism activity in Codeforces Round 786 (Div. 3).
Please check out submissions of problem 1674B - Dictionary,submissions are 155621247 and 155678427 They both have same function with same variable but just comment down it and wrote it again with some variable change. Please look out at it.
Proofs given below and also you can check submissions...
Same templates != plagiarism
*Maybe they are learning from one sensei
Look at commented code.
I am excited about this round to be easy as the last Div 3 round. ♥
Hope everyone gets a good rank.
Bro how can everyone get a good rank?
Excited to enjoy this round on my birthday! :)
Happy birthday Serval!
Happy birthday and wish you good luck in this round!
That was an amazing contest! Thanks for the awesome problems!
good problems. Confused in E for half an hour. F and G are good educational problems.
Was thinking on problem B for more than 40 mins and after a bunch of unsuccessful attempts using math it comes to be solvable bruteforcely :) A hilarious thing to make it so and i guess i have to remember sometimes that it is just div3 (even tho i'm 1100 still).
I really enjoyed solving G. Great problems, very educational
can you share your approach
i'm trying to understand task c in 1hr still not able to understand what they trying to say (maybe my english is weak)
Did anyone solve F in Python? Keep getting TLE with python and MLE with pypy.
For future reference, here is a solution that is accepted in both python 3 and pypy 3 (surprisingly, about 2x slower in pypy): https://codeforces.net/contest/1675/submission/156014304
G is O(N*M^3) with very small constant factor?
My solution was $$$O(nm^2)$$$
I use dp[total number][last number] as status. How to improve?
Convert the array $$$a$$$ into array $$$b$$$ where $$$a_i$$$ is the frequency of $$$i$$$. Then, the dp state should be (current_index, number of sections, length of section ending in the current_index). Submission (sorry for the bad explanation, hope you can understand from my code)
Let $$$p[i][x][y]$$$ be the minimum of $$$dp[i][x][1]$$$ to $$$dp[i][x][y]$$$, and $$$pa$$$ be the prefix sum of $$$a$$$.
Then $$$dp[i][x][y] = p[i-1][x-y][y]+abs(pa[i]-y)$$$.
Note that $$$y$$$ is not greater than $$$n/i$$$ when enumerating, so the final time complexity is $$$O(nm\log(m))$$$.
Here is my code: 156012337
I guess I implicitly used the n/i condition so it is reduced to O(NM^2logM) at least.
I had an idea to do, like reverse the then problem will turn into minimum operations to make a non decreasing array then apply slope trick Optimization.. something like that. Is it possible ?
My approach for F: Vlad and Unfinished Business
We need to travel from $$$src$$$ to $$$target$$$. Root the tree at $$$src$$$. The path from $$$src$$$ to $$$target$$$ is now a vertical path. Call a node infected if we need to compulsorily visit it during our journey. Initially, the only infected nodes are $$$src$$$, $$$target$$$, and the given $$$k$$$ vertices.
Notice that if a node is infected, its parent would also be infected (since we cannot visit it without visiting its parent). Hence, we can propagate infection up the tree via a simple DFS.
To compute the answer, notice that any edge between 2 infected nodes would have to be traveled exactly twice. (One while going from parent to children, and the other while coming back). Hence, the answer is
However, we do not need to travel back to $$$src$$$ from $$$target$$$. Therefore, we need to subtract the depth of $$$target$$$ from our answer.
Code
There is a bug. My solution to A was accepted, but it's missing in the standings.
UPD: fixed, thanks
how to solve G
a common idea in problems where you increase $$$a[i]$$$ at the cost of $$$a[i+1]$$$ or $$$a[i-1]$$$ is to consider prefix sums/suffix sums.
in this problem, consider the prefix sum array $$$pref[]$$$. Each move of type $$$a[i+1]$$$ to $$$a[i]$$$ increases $$$pref[i]$$$ by $$$1$$$, and leaves all other values unchanged. Similarly, $$$a[i]$$$ to $$$a[i+1]$$$ decreases $$$pref[i]$$$ by $$$1$$$ and leaves everything else unchanged.
Converse is also true: every increase/decrease of $$$pref[i]$$$ corresponds to some move in the original array. [for $$$i<n$$$ , since $$$pref[n]$$$ remains same].
So problem is transformed to one operation which increments or decrements an index of the prefix array. Then you get the necessary and sufficient condition
$$$C$$$: $$$a[i] \geq a[i+1] \iff pref[i] \geq 2*pref[i-1]-pref[i-2]$$$.
Then you can define $$$dp[j][j1][j2]$$$ to be the minimal cost to convert $$$pref[1...j]$$$ so that $$$pref[j]==j1$$$ and $$$pref[j-1]==j2$$$, then you can figure out the states using condition $$$C$$$.
Oh I got it. Thanks
ah, I saw a problem recently that used this idea. Still, I couldn't remember it during the contest. Explains why I am still blue :(
Thanks for the great explanation!
I wrote this piece of code based on your explanation and got ac. However, I can't convince myself that it fits within the time limit. Here's the dp part of my code:
At first glance, the time complexity looks like $$$O(NM^3)$$$, but considering the fact that it managed to pass the time limit comfortably at 46ms, I doubt if this is the actual time complexity. Could you please help me figure out the time complexity of my code (and also prove it)?
Can someone please explain why I am getting TLE in problem B? This is my code- https://replit.com/@StephenV1/TrickyComfortableLanguage#main.cpp
you will go into dead loop when both numbers are 0
I don't think I understood what you meant to say. Can you please give an example?
Try 3 2 2 2
It's printing -1. I think that's the correct output right?
How about 0 0 0?
What problem C was all about:
problem c is really hard and it can solved by logic principles only!!
Can someone please explain how to solve E ? Only thing I figured out was in total at most 26 moves every char in string can be converted to 'a'. So, k>26 will always yields aaa...a which is the smallest lexicographical string ( Correct me if I am wrong )
your observation is correct. You just have to go from s(0) to s(n-1) in order and converting characters to lowest possible conversion at that point, you just have to maintain an array of size 26 to check how many characters you have already converted
you can check one of the way in my solution :
Can you please explain the main logic. I mean the lowest possible conversion at that point means what ? Third part I understood that like if suppose 7th char was 's' and we did the calculation for it's conversion then if we get 20th or any further char again 's' then we will simply ignore that since 's' calculation is already done
suppose n = 5, k = 4, string = cgdfb,
c -> a
g -> e (thats the best conversion for this stage)
d -> d(there is no conversion possible as k is used up)
f -> e
b -> a
thats what i meant if k was 6 you could convert g -> c -> a
Thanks, I understood now..I have to use already converted char value afterwards also..like if string is "ek" then ( e-> a in 4 moves & k->e in 6 moves & we already have e->a before so we will ignore this so total 10 moves for "ek" to "aa"...)..Thanks :)
Idk how the heck C has 7000 solves . Believe me D and E are a lot easier than C.
cheaters?
I don't think so, maybe we are missing out on something easy observation.
I solved A-E, and short 10 minutes to submit F. This contest is harder than previous div3, but judging from the number of AC submission, I'm sure there are a tons of cheaters.
Exactly . There is no way 7000 ppl would solve C and only 2500 solves on E. It's all because of cheaters.
C has fairly simple observations. There is a prefix of 1s, a postfix of 0s.
So the thief is between (including both) the last 1 and the first 0.
I don't think the contest was tough,
Problem A : Implementation
Problem B : Start from back
Problem C: prefix should not have any '0' and suffix should not have any '1'
Problem D: Basic dfs
Problem E: Just do whatever is written, decrease current character as much as you can Problem F: I have seen these types of problems many times, fix the root and then every required edge will be visited twice except between the path from x to y (So, basically again a normal dfs with some basic dp involved)
Problem G: It involved a nice DP, but it's fine, the last problem can be on the tougher side.
Problems were of the same level as of a normal Div3 contest.
Can you please share more problems like F
I didn't mean the questions were tough. I meant it should have smaller ACs compared to previous Div3. Also recently there are people who uploaded solution on YT mid contests. I don't see why it wouldn't happen again.
Problem D implementation was difficult. But the idea was simple.
Problem C implementation was super easy. But getting the idea was very tough.
Can you explain your idea for problem D. I was getting TLE
determine the root vertex first. it's easy, because it's the only one vertex with a[i] = i. then run dfs from the root and start writing the path to an array, and we end the path if and only if we came to the leaf node (there is no way to continue it). So, we return current path to the main function and do same thing again unless all vertices are visited. sorry for my poor english :P
Here is the implementation
https://codeforces.net/contest/1675/submission/156019404
answer is pretty simple, I think most people guessed it right instead of knowing how it passed
you mean problem C?
yeah
answer : pos(first(0)) — pos(last(1)) + 1
My idea for F — Find the path from x to y. Let the path array be composed of nodes u1, u2, u3 ... ul, where u1 = x and ul = y. Then for every u, do a BFS considering u as the root, and store the distances in a array dis. Then the answer is length of path + 2*(sum of all distances of mentioned k nodes). But it got WA on test 2. Am I missing something?
Submission
It will time out eventually Think of on the ideas of dp
You may choose visiting order. So this solution is wrong even not TLE.
But the algorithm I mentioned will give the correct answer, no?
jiangly's code is so concise!
can anyone please explain
question E
in simple English and explain the third test case? Please don't give any hint about the answer.I am not sure if I can explain better than the problem description.
We have to make the given string lexicographically minimum, by performing the given operation
maximum k times
.Operation: Choose any character in the String, and replace all of it by the previous character.
For example, replace all 'c' with 'b' or replace all 'a' with 'z'.
For String
cba
withk=2
, we can perform 2 operations as below.cba -> bba -> aaa
Note: Checking the test case below might be considered as a HINT.
This is the best order that could be done for this string.
g
->f
)f
->e
)e
->d
)d
->c
)c
->b
)thanks a lot
Thank you for the contest!
I especially liked problem C for the setting and problem F for key insight.
Can you please explain to me the test cases for C, I couldn't even understand how the test cases are working! May be I am getting something seriously wrong on this problem, for test case -> ? ? 0 ? ? how the answer is 3? Should it not be 5?
3rd person says that there is no picture in the room. so the only possible thieves will be 1st,2nd, and 3rd one
? ? 0 ? ?
If friend 3 was the thief and lying, the painting was definitely taken then so it would not be there for 4 or 5. If friend 3 was honest, the painting was taken by friend 1 or 2 (it had already been taken) so it was definitely taken before 4 or 5 came.
yep! I messed up my contest for this 1 problem :p though its a nice one!
Apart from the existing comments, let me try to explain how I interpreted this problem
C
for Testcase? ? 0 ? ?
. May be it helps someone.If 1st person is thief :
2,4,5
can't remember — so, no issues3
didn't see the picture — which is trueIf 2nd person is thief :
4,5
can't remember — so, no issues3
didn't see the picture — which is trueIf 3rd person is thief :
1,2,4,5
can't remember — so, no issuesIf 4th person is thief :
1,2,5
can't remember — so, no issues3
didn't see the picture — which becomes a false statementIf 5th person is thief :
1,2,4
can't remember — so, no issues3
didn't see the picture — which becomes a false statement5th
person is not the thief. So, INVALID.will you please share the approach to the problem F
My approach is as follows.
Consider a rooted tree with root in $$$x$$$. Let important vertices be the ones that we will have to visit. First, mark the input vertices as important, and also the vertex $$$y$$$. Now, a vertex is important if it is marked or at least one of its children is important. This characteristic can be found recursively with a single DFS.
So, we got a subtree $$$T$$$ of the original tree: we have to visit every vertex of $$$T$$$, and don't have to go anywhere else. Now, think what would we do if we had to return to $$$x$$$ in the end. Naturally, we would traverse each of the edges of $$$T$$$ exactly twice: once going from the root and the second time going back.
Remember now that we don't have to return to $$$x$$$, just end up at $$$y$$$. Naturally, we will consider a path that ends up moving from $$$y$$$ to $$$x$$$, and then omit that last part. The number of steps is therefore twice the number of edges in $$$T$$$ minus the distance from $$$x$$$ to $$$y$$$.
To me, the beauty of the problem is that some parts above can be omitted, leading to more and more tedious (but still quite working) solutions. So, the participant has a choice to either start implementing what they have, or ponder more to maybe cut out quite a bit of work. If a solution needed all of the above to pass at all, that beauty would have been lost.
Me after solving C
Can anybody explain why so many RE on test case 7 in problem D with Python? My submission: https://codeforces.net/contest/1675/submission/155950425
Probably running out of stack due to recursion? Test 7 appears to be a single path with every node on it.
I thought so too, but here is my submission with increased recursion limit, but got the same. https://codeforces.net/contest/1675/submission/155959621
That just stops python giving a recursion limit error, you can still run out of memory and crash.
"you can still run out of memory and crash"
That yields a "Memory Limit Exceeded" (e.g.): https://codeforces.net/contest/1675/submission/155991461
Which is different from the "Runtime Error" in the submission shared by khanter_
This works: https://codeforces.net/contest/1675/submission/156020448
I added a generator function called bootstrap. This takes a recursion and processes it as a stack instead. It's extremely useful in python for large N recursion problems. I use C++ now but when I used python I always used bootstrap for recursion and strongly advise you to do the same.
Got it, thanks a lot!
I use only
setrecursionlimit(300005)
, it got runtime error.I use
setrecursionlimit(300005)
and bootstrap, it got MLE.I use bootstrap only, it got AC.
Can I conclude that one needs bootstrap ONLY for recursion/dfs problems?
Thanks for sharing this tip!
I’m not sure. Perhaps just the act of setting the stack limit that high occupies too much memory. In any case, when I used python (see all my submissions pre May 2021) I only ever used bootstrap and it was always fine.
when will be the editorial available?
I see some people cant get the logic of C so probably my approach will be useful: first of all i loop through the whole string and count a total number of occurrences of '0' and '1' in a string (separately for each).
Then i start a second loop and in that loop i also count a number of occurrences of '0' and '1' till i-th element. The first thing we need to see that only the last '1' can be a thief (because for any earlier '1' the will be a '1' later, which confirms that a painting cant be stolen earlier). The same goes for '0', but only the first '0' can be a thief (because for any further '0' there will be a '0' earlier, which says that a painting has already been seen to be stolen before).
Now we move to '?'. Here we had to see that '?' can be a thief only if all '0' are on the right side from '?' and all '1' are on the left side of '?'. The logic is pretty straight-forward: if there is a '0' on the left then the painting was already confirmed to be stolen (which cant happen because its being stolen on '?' only). The same we can see for '1': if there is a one '1' on the right then a painting will be confirmed to be seen (which cant happen because the painting has already been stolen on '?').
You can find my code here: 155985751.
I hope I could help somebody out by doing this, have a great one and see you all tomorrow!
Screencast + Explanations
Here are video solutions (for D-F)
In Problem C , if before 0 any ? occurs why he/she won't be considered as Theif ??
Depends on what else we have before and after '0'. Give more detailed information/sample, please
Before
0
, if any?
occurs, he/she will be considered as Thief.Anything after
0
is only not considered as thief.Answer for
???????000
is8
. (Only last 2 person can't be considered a thief)I have tried to explain that in this comment — here.
If you speak about '?' considering a thief you have to keep in mind what you have on both sides too. There might be some '1' in between or '0' before '?' that break the logic
The statement of Problem C is a masterpiece.
It was the best text I've read this year.
Problem C, after you simplify the problem, it is only:
Set counter = 0
3 Cases:
It is a '?' increment counter
it is a 0, return counter + 1
it is a 1, reset the counter to 1.
A B C D E F
same
Problem C becomes simple if you think it like this: Suppose the ith person is a thief, then from [i+1 to N], there would be no painting present in room, and from [1,i-1], there would be painting present, also every person apart from ith one must be telling the truth, so prefix[i-1] can only have 1's or ? and suffix[i+1] can only have ? and 0's.
what if we have a case like "??0?1??"?
Not possible there is exactly one theif
well, i'm stuck with E again.
how can i find some problems like this and make some improvement, seems like something between greedy and dp.
want to become blue
I have seen such hard greedy always in Div.3, but rarely in Div.2.
Also, such problems are common on CodeChef, specially in Starters round.
You can try Div3s on codeforces or give codechef
thanks a lot
Twice in a row that Geothermal FST in div3 contest.
So i am dealing with a problem regarding problem B-Make it increasing.
In first two try I applied the logic that a[n-i]/pow(2, n) <a[i] and so I solved it by taking log in both sides and thus finding value of n and adding it to the answer for all i from n-1 to 1. I used a online ide and in many ide my answer for the 6th test case was coming 11 but in codeforces it is giving 10. Please guide me coz i couldnt find where my logic breaks.Though i later solved it using two loops but I suppose using log will save a lot of time in case of larger arrays. I am attaching the code below. Thanking you all.
You should avoid using log and pow functions as long as you can find other ways, as they might have precision issues. Here instead of using log to calculate p, you can instead run a while loop and keep dividing a[i-1] by 2 till it is greater than or equal to a[i] and greater than 0 as well.
Hey all, I have a question. For problems such as problem E, how to efficiently create an unordered_map of <character, position of character in alphabet>. For example name this unordered_map alphs: alphs['a'] = 1; alphs['b'] =2.. alphs['z'] = 26;
Note: by 'efficiently' i mean without copy pasting 26 lines and assigning each value one by one. i.e. create without typing too much or using few lines of code.
i am not sure if i am totally understand what u want but you can map the character to its position as follow: character = character — 'a' + 1 you don't even need an unordered_map (maybe im wrong) but if you for some reason want it here we go:
include <bits/stdc++.h>
using namespace std;
int main() { unordered_map<char, int>mp; for (char ch = 'a'; ch <= 'z'; ch ++) { mp[ch] = ch — 'a' + 1; } for (auto i : mp)cout<<i.first<<" "<<i.second<<endl; return 0; }
If I am understanding correctly, are you using the ASCII values of characters to find out the positions in alphabet? For example, ascii value of 'a' is 65. and ascii value of j = 74. so to find out position of 'j' in alphabet, i take ascii value of 'j', then minus the ascii value of 'a', then add 1. i.e. 74-65+1 = 10. Is that what you are doing. Also am i correct in understanding that that is what automatically happens in c++ when adding or taking away a character from an integer?
In the constest Codeforces Round #787 (Div. 3) Problem D (1675D) My submition is (Juliano1 / 155992661)
The code framework I submitted comes from "算法笔记 上级训练实战指南",a algorithm guidebook published in China in 2021 (ISBN 978-7-111-54040-3)
the frame comes from pages 294 ~ 295 // 层序遍历 (traversal) (I will attach pictures at the end of the comments), just replace the "queue" in the framework with "stack" and at the end of the frame The binary tree PUSH operation is replaced with a PUSH operation of the common tree. The frame code is as follows:
void BFS(int root){ queue q; q.push(root); while(!q.empty()){ int now = q.front(); q.pop(); print(now); if(Node[now].lchild !=-1)q.push(Node[now].lchild); if(Node[now].rchild !=-1)q.push(Node[now].rchild); } }
and here is my code:
void dfs(int root, vector p) { stack s; s.push(root); int cnt = 0; int index = 0; while (!s.empty()) { int now = s.top(); s.pop(); cnt++; ans[index].push_back(now); if (p[now].son.empty()) { ans_len.push_back(cnt); cnt = 0; index++; } else for (int i = 0; i < p[now].son.size(); i++) s.push(p[now].son[i]); } }
It can ensure that my code does NOT HAVE CHEATING in the contest. The same part as other contestants is due to the use of similar templates PUBLISHED BEFORE THE CONTEST, so I apply for restoration of my results.
pictures:
155992661