I want to apologize once more for queue problems. It has also aggravated some tight ML/TL issues which probably would be not so big otherwise. I hope you enjoyed the problems nevertheless.

Tutorial is loading...

Tutorial is loading...

38799800 — logs

38799811 — case analisys

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

I hope that Petr is not mad at me for my joke.

Tutorial is loading...

38799850

38799853 — completely different solution with complexity *O*(6^{n / 2})

Tutorial is loading...

Tutorial is loading...

I hope that DarthPrince is not mad at me for my joke.

Tutorial is loading...

Can u explain this?

Let's look at strongly connected components of new graph. Each connected component of old graph lies inside one strongly connected component of new graph, and you can't go from one such strongly connected component to another. So we will run DFS from each that strongly connected component once.

Hihi Quang Dien brought me here.

Wow!! Do you mean DoctorRYA of VietNam???

In Div1 B, isn´t it easier just to transform one permutation into the other and count the number of steps it takes?

That's another way to count parity

But, isn't it possible to swap the same pair more than once?

Yes, we can swap the same pair more than once. But notice that it must take an even number of swaps to preserve the order, hence the parity and the answer will not be changed.

Ah, So, NicolasCassia should compare the

parityof the number of steps with the parity of 3n or 7n+1.Thank you very much :)

If from a given permutation the original can be reached with an even number of steps, why can not it also be with an odd amount?

I understand this truth, but don't know how to explain it other than like following:

After any number of

distinctswaps, if I want to return to the previous case (before those swaps) I need to do the same number of swaps I have done before (and this can be understood by testing). So, the number you need to return to a previous case starting from this case is even.So, if a permutation

P1 can be reached from a permutatuonP0 withnnumber of steps, and you didsnumber of additional steps (anywhere betweenP0 andP1) and still end inP1, then thissis even. So,n+shas the sameparityasn. Thus, ifnwas even, then any number of steps betweenP0 andP1 will be even, and vice versa.Can you explain significance of 3n and 7n+1?? I am unable connect the dots here.

The setter want to obtain two number which are surely different in the parity.

Firstly, we should know the following two rules (or observations):

odd*even=even,odd*odd=odd,odd+ 1 =even,even+ 1 =odd.Then, let's discuss the two case of

n:If

niseven, then 3nisevenand 7n+ 1 isodd.If

nisodd, then 3nisoddand 7n+ 1 iseven.And this difference in parity is necessary to solve the problem.

Hope I understood your question and answered it.

Yeah, i got that. I am referring to the coded solution above. (ans^=1 and why "ans=1" implies Um_nik... "ans=0" implies Petr).

Oh, I understood you now, and really don't know what's happening in the code you referred to XD. Sorry for that.

any prove?? for odd swaps will definitely give odd parity and even will even

Hello Um_nik Can you tell me why do you say to count the number of inversions because in the sample case of:

5

2 4 5 1 3

The inversion count is 5 whereas the number of steps necessary to arrive at the correct permutation is only 3 . Also i have applied the inversion count using mergesort, But it is still giving wrong answer. Can you help me with it? link:

38770498

I can't understand this solution. Can someone explain this? Thanks.

Because it's a wrong solution, this is counter-test. True answer is "Um_nik", but this solution returns "Petr".

The authors of the problem did not assume a probabilistic solution, so there are no tests against them.

You are right. But this solution can get Accepted. Emmm... Your test case is randomly generated data ? Maybe this solution based in some randomly theory?

Well, yes, this solution uses that tests are randomly generated and it is correct for big

n. However forn= 1000 +epsit has not very good probability of being correct. But I had very few (4 to be precise) tests with smallnand it happened to be correct on all 4.This test is specifically directed against a probabilistic solution.

For example and fun, I can generated tests when this solution will be Accepted:

But this will not be a complete set of tests for this problem.

You can use this method of solution if you do not need to solve the problem completely (on some olympiads give a few points per each test).

Edit: I didn't see that the editorial mentions the solution with logarithms in the first two lines. Later it talks about getting the solution in integers which is nice.

If someone got confused in Div2B by seeing the editorial like me, here is a easier(imo) solution.

Case1:x^{y}<y^{x}Takelog_{x}bothsideslog_{x}(x^{y}) <log_{x}(y^{x})y<xlog_{x}(y)y<xlog_{10}(y) /log_{10}(x)You can do the same for other cases and it's similar. If someone is thinking it's too much case work, here is the solution.

D Getting TLE in D could anyone Help?

for each good store vertices which produce it(in a vector).now run BFS for each good(reinitialzing the dist/vis array) and keep pushing back into a vector(answer vector) the dist(which is obviously minimum due to unweighted graph) from the vertices producing the current good.After that sort for each vertex its ans vector and sum up the first s elements and print it.

I have used same logic u said but i am getting tle don't Know why

maybe because you are using index of size n whereas it only needs to be of size k

Look here: 38736740 And what I should do with that?

Maybe use long double instead of double! Link

Try to change from double to long double.

What?!?!

Funny staff with mingw + windows. In a local machine with gcc+linux does not show this artifact.

It's not MinGW or Windows specifically, it's floating point arithmetic in general.

Basically, the compiler is free to calculate one expression with more precision than needed by

`double`

(say, in 80-bit FPU registers) and the other one with 64 bits precision. These two results will be different when compared in 80 bits, hence non-zero output.This could be an explanation

can someone give python code for binary fft

For Div2B, during the contest I came up with this approach:

For any two positive integer numbers

aandb:a^{b}=b^{a}if(a=b), elsea^{b}<b^{a}if(a= 1), elsea^{b}>b^{a}if(a<b).But got WA on pretest 5.

Today, I checked for the test I failed in and saw it was (2 4), then treated this test, but got WA on this test (2 3), then treated this and got AC.

Is really this

lastapproach is correct for allaandb, or it just passed the tests?BTW, it was easy to come up with this

lastapproach because (2 4) and (2 3) are easy to think about.38781062

.............

UPD: Sorry, I have just seen that what I wrote above had been discussed in the editorial. I apologize for my fault.I got AC with similar solution: brute forced everything where a && b is small, otherwise using your logic.

I did exactly what the tutorial says in div2C, and during the contest as well, but it gets TLE on test 11. Tried searching for similar solutions in python 2 but no one AC, can someone tell me what I got wrong?

38782761

Submit it on PyPy interpreter

You need to relearn python bro. Use xrange instead of range. And why are you wrapping map with list? That's not needed in python 2. On the other hand, there is indeed a speed limitation with python. You might need to submit in pypy if your code is already optimized and TLEs. Sometimes, the reverse situation happens as well. So take note of this.

Why are some problem names written in russian instead of english in english version of editorial ??

I would like to just note that problems B from Div2 and D from Div1 have a lot in common. Solution to Div2B basically tells you how optimal sum looks like in Div1D ;). I don't know if it is intentional, but if not then it is funny coincidence.

Yeah, I noticed it too. But it wasn't intentional :) Ddiv1 was in my problem bank for long time and easier problems were made up just for this contest.

;

See my code I used the same approach [Problem C] got AC(http://codeforces.com/contest/987/submission/38761812)

Made a simple yet fatal error ...thanks btw

Why did Div1B set a lower bound on

n?For joke to work. It is usual condition for such problems.

This is my solution for Div2 E 38777827

I am counting the number of swaps needed to obtain the permutation in O(nlogn)

how to calculate the number of cycles in permutation in O(n).

Just use a regular array instead of map, the numbers in the permutation <= 1e6.

Can someone please explain DIV2F ? I can't comprehend the editorial.

May someone please tell, why did my short solution got TLE to the problem Div1-B? Submission link. Is it because of distance function?

Since set does not have a random access iterator distance function takes O (n). Thats what makes it slow. For more see this

hello satvik007 can you please tell me what is wrong with my code in petr and permutations ?.I have solved it using mergesort...Here is the link :

Can someone explain how i get AC by usingthis solution on problem Div2-D

Do multisource bfs k times , for every color and note down the distances you get , the for each node sort the distance values and take first s values , you get the answer

why doing multisource bfs will get yime out coz time will be o(k.no_of_vertices_in_type_k.(V+E))

Can anybody tell how to solve Div2 -D in O(s*(n+m))

For div1D, 3^i always looks like 100000....000 (with i 0s) in base 3. So, why don't we just convert n (or n/2, or n/4) to base 3 (In O(LlogL)) and compare it with 3^i (in base 3)?

Can you convert it in base 3 that fast?

I was wrong. Sorry.

38793607 Getting TLE in problem D from Div. 2. Not sure what I'm doing wrong here, could anyone help?

In the bfs Don't push a node unless their cost will be updated and update the cost

Thanks! I made this change and got AC. But I don't understand one thing:

Earlier I was pushing the nodes irrespective of whether the cost would be updated; I was performing this check at the time of popping and stopping the BFS from a popped node if the cost is not to be updated. So basically in the BFS in my earlier submission, all the nodes were being operated on 2 times instead of 1 time (first time when it's pushed onto queue and second time when it's popped and I check for cost update). So this would be an optimization, right? The time complexity is the same? Please correct me if I'm wrong. Thanks again!

Nvm, I understood my mistake.

Actually a node will be pushed as many times as it's degree here ! Moreover, you might run into a cycle which will take you into an infinite loop !

How to solve Div2 E with mergesort ?

Merge sort here is used to calculate the number of inversions of a permutation to find the permutation's parity.

Suppose your permutation is 4 3 2 1.

Divide it into two parts. 4 3 and 2 1.

Now, what are the number of inversions created due to 2? (number of pairs where a[i] > a[j] and i < j and a[j] = 2)

It's the number of elements larger than 2 in 4 3.

You can easily find that in the merge process of merge sort. When you are inserting 2 during a merge, the number elements left in the other half of the array is the number of elements larger than 2, isn't it?

So, for 1, when you have divided original array into 2 parts, you are adding 2 to number of inversions.

When you further divide 2 1 into 2 halves i.e. 2 and 1, you are adding another 1 to number of inversions.

This is just a basic gist, you can find details by googling about finding number of inversions using merge sort.

I had used the logarithmic approach as discussed in the editorial but I am getting the wrong answer for x=y (where x and y are very big) but it is working fine on my local system. I am not able to figure out the exact reason why it is behaving differently on CF judge and my local system. Can someone please help me out to understand it?

Thanks in advance.

my solution

changing epsilon worked. 38796332

Edit: http://codeforces.com/blog/entry/59720?#comment-434361 for the explanation

Links to authors submissions are added.

Not available. Looks like something with the rights

If I remember correctly, you should submit by hitting an airplane submit button next to a problem in the problemset list:

Looks like the important part is "the problemset list". If I submit it in the contest, it is not visible because I'm manager of the contest. Anyway, thanks.

Well, the authors submissions links are not working properly. Please fix the problem, thanks. [UPD] I'm sorry for the unnecessary post too. I'll think before I post:)

[UPDATE]— Its working now. Sorry for the unnecessary post.Your solution link is not working.

It keeps on saying,"You are not allowed to view the requested content"?

Should be fixed now.

Last two comments are not helping.

For Div.1 E,can somebody elaborate the fenwick tree on euler tour tree or introduce some tutorial on it? How does it calculate the sum of on path to root in O(logn) time? Thanks a lot!

keep l[x] = starting time of x in euler tour, r[x] = ending time

If you want to set value of node x = val, then with fenwick you modify l[x] to val, and r[x] to -val.

Now say you have 2 nodes x, y and x is ancestor of y

The range of query will be l[x], l[y]. Every node on path from x to y will appear Exactly once on this path. And nodes that aren't on path appear twice, so they cancel out by the way we set values in fenwik tree.

So basically sum from root to x is query on (1, l[x])

Got it,thanks a lot!

Also we can do that without fenwick tree at all. Just do dfs and keep sums to the root in the global array. In this case for each query we need to calculate sum

sum_{x}+sum_{y}-sum_{lca(x, y)}-sum_{parent(lca(x, y))}. That will be solution.can anyone provide links to more problems like Div1-B,Div2-E.. Thanks :)

Can any one help me with solving Three displays problem using dynamic programming(DP)?

The states for the DP I visualised are current display label(1..N) and number of displays(1..3). For each display label I minimise and choose the best cost of one display, the best cost of two displays and the best cost of three displays.

I'm getting Wrong Answer. The case I'm missing is where I don't get the best cost of three display at any display labels. So now I'm kind of feeling stuck on how to re use the sub-problem's solution here? Can anyone help me with formulating the DP ?

My submission link contains my DP idea.

I solved it with dp. It might help if you look at my solution. I think it is easier to understand. http://codeforces.com/contest/987/submission/38737422

Hope I helped. Have a nice day!

Can you please tell the logic to solve using dp?or some material to study from these kind of problems...

rahul9707 Say i is the index of first display board, j is the index of second display board and k is the index of third display board of our answer, where i<j<k, We keep track of the best cost of two display boards cost(i)+cost(j) at dp(j) if font_size(j)>font_size(i). Then for each k we try to pair it with dp(j)[which holds the best cost of two boards already] if font_size(k)>font_size(k).

Thank you _maverick

Thanks a lot for the idea.

Now if we increase the constraint, say length of the array is 10^5.

Can we arrive at an O(N) dp for the problem ? That was what I as trying to do in my initial submissions for this problem.

Amazing.

please explain O(6^(n/2)) approach for AND graph. thanks

I am getting TLE for Div2D in Java even though I used the same algo as the editorial. Someone suggested that using ArrayList in graphs in Java is inefficient. Does anyone has links to some articles that explains the alternative well?

Read graph to ArrayList[], then copy its contents to int[][]. Use int[] for queue in bfs. It should be enough.

Hello dalex brother. I have followed your suggestion and tutorial but still getting TLE :(

https://ideone.com/fGjMWD

Dear Um_nik,

Please explain your second solution to F with complexity O(6^(n/2))

Here is my editorial to problem F. I think it's easier to understand. Let me know if you have any doubts. :)

First, Thanks for an excellent editorial, very nice I understood what I was missing , thankyou so much;)

I used the same approach but I am getting runtime error- stack overflow, I use java.

Hey, thanks a lot :) ... I don't know why it's getting RTE. Try removing the variable arr you'll reduce the amount of memory consumed that way and try again.

Thanks! This is a very good explanation :)

can someone explain me why am i getting WA in test case 14 (F. AND Graph Div 2)?

i am using dsu to solve the problem

http://codeforces.com/contest/987/submission/38892597

Didn't understand your logic ... Please explain it clearly

You can read the blog link I posted to understand my approach.

a < b and if a&b = 0

then i will change the vale of ane[b]=ane[a] it means b is connected with a so ane[b] != b

if c < b and b&c = 0 so if c is not connected with a graph but b is connected with a so i will change ane[c]=ane[b]

and in the end if(ane[c]==c) that means c is not connected with any prw graph so i will add 1 in my ans...

Ex.

4 3 1 3 4

so we see than in first loop (for i=1 and j=3,4)

1&3 != 0 (no cnange ) ane[4]=1 | ans=1

in sec loop (for i=3 and j=4)

now 3&4 == 0

but ane[4]=1 so i will change ane[3]=ane[4]

in sort i am just checking if a or b is prw. connected or not

So, does each vertex have only one edge in your approach ?

yes we can say that

i think there can be 2 edges... assume some p and q and r S.T p<q<r among many possibilities we can say that p&q=0 and q&r=0 will be true and thus there are 2edges for q.

nup in this case first pair is (p,q) and at that time ane[q]=p

now for q loop ane[r]=ane[q]=p

so in your case its is not true

if a<b<c<d

a&d=0 , b&c=0 , b&d=0 so we can say that b has two edge

Ex.

5 12 17 32

but i realize that it's O(m^2) so its will not work for this problem

it also means that the upper bound will only N/2 as each vertex has only one edge.

*******DELETED******

Damn dude how did you make such a hard problem into a piece of cake? :D. Nice solution and very easy to understand.

Thanks a lot :)

Please read my other posts to see if you like any of them too :)

Hello ghoshsai5000 First of all your editorials are superb.Thanks a lot. I have a problem with question E :Petr and permutations, i have solved it differently and it is quite short code. Can you look at it once and tell me where i am wrong? Here is the link: (https://codeforces.com/contest/987/submission/39357325)

Thank you for your compliments :)

You are not counting the number of inversions correctly. Give me some time to give a counter example to show why that way of counting is wrong. :)

ok thank you but please do help :-)

Please tell me bro....

Take 2341

So you would do

But you need three swaps !

Thank you...

Can you tell me what we have to do after counting inversions and why? I have counted inversions using mergesort but it is showing wrong answer on test case 3.....link

The idea is to count the number of swaps you need to arrive at the current permutation. Check the parity of the number of inversions.

I know how to do it with segment trees and cycle decomposition, but I don't know how to do it with merge sort. I think there's some mistake in that part.

Can you tell me the condition(or formula) to check after counting the number of inversions? Like when will the ans be Um_nik and when Petr...

The parity checking is correct. The parity of number of inversions should be equal to parity of number of operations.

What is the O(s(m + n)) solution to D ?

Inspite of the controversy surrounding this contest over the delayed queue length and the tight memory limits, I actually enjoyed this contest quite a lot ! I loved the problems and 3 of them inspired me to write blog posts.

Here is the repository of all my solutions to problems of this contest, along with explanations.

All my blog posts have some more exposition. Please read them and get back to me with any doubts or corrections. :)

I have written a blog post about B here.

I have written a blog post about E here.

I have written a blog post about F here.

(All the problems were quite beautiful :) )

Note — I have not written a blog post about C, but the repository of all my solutions also contains an O(n log n) solutions to C with segment trees. It's quite easy to understand if you know how to count the number of inversions using a segment tree. It's the same idea. First we sort elements by font size, and keep track of every elements cost and position. Then, one by one, we insert the elements in ascending order of font size into a minimum tree for cost. When we insert element x, all elements with font size smaller than x have already been inserted, so to know the smallest cost, we simply query the tree from (1, position(x) — 1).

We do the same thing for the right too, except now we insert the elements, one by one, from the largest font size. This time when we insert x, we have already put all elements with a font size greater than font size(x). So to get the minimum cost, we query the tree from (position(x) + 1, n).

Can someone explain update complexity in 1E(Prince's Problem).

(in div1B)

Why the generated tests have to be generated randomly!?

I mean hacks were disabled and we're guaranteed that input is random, even though the official solution does not rely on randomness of the input!

or else the joke wouldn't be complete

Can I get a hint for solving Div2-D in O(s(m+n))?

Hi~

Also I don't know the optimum solution, but here is my thought. Hope it help

When we run BFS for one specific town, we don't need to record all information of

`k`

goods. Once get`s`

goods, we get the answer. This is to say, no need to use the sort stuff.Author's solution for div1E contained a typo (which made it incorrect, of course). There were no tests against this bug in my testset, so even two other solutions didn't help to find it. Now author's solution should be fixed and some stronger tests added. I apologize for this mistake and want to thank Death_Scythe who found the bug.

I can't fully understand connection between the number of cycles and parity in 986B. Can somebody explain?

Can someone explain how to solve Div2D in O(s(n+m)) time?

In problem Div2 E, I don't quite get the part of parity of 3n or parity of 7n + 1. Can someone help me with this?

When n is even, 3n is even and 7n+1 is odd. When n is odd, 3n is odd and 7n+1 is even. Thus, if you know the parity of n and parity of inversions in the array, you can directly arrive at the answer.

Can Div2 C be solved in O(nlog(n)) time. If yes, how?

using segment/fenwick tree http://codeforces.com/contest/987/submission/38760023

Can you explain your approach?

Troll solution for a troll problem

Div1BLink to AC soln

(Expected number of fixed points are very different in both approaches)

Could you please elaborate on how you arrived at this, it is interesting!

I am confused why Div 1 A fair solution is not O(n2) why coz if supoose there is m element in graph having type 1 .it means there are m elemnts of type 1 so in sol of umnik we hv to do bfs for all those m elemnt so this should time out correct me pls .arnt we doing bfs m times in type 1 . so why not O(k.m.(V+E))