Good mood and pure thoughts to everyone who comes here.

1422A - Fence

Author aropan

Solution 94875319

**Tutorial**

1422B - Nice Matrix

Author andrew

Solution 94875245

**Tutorial**

1422C - Bargain

Author aropan

Solution 94875502

**Tutorial**

1422D - Returning Home

Author aropan

Solution 94875536

**Tutorial**

1422E - Minlexes

Author aropan

Solution 94875558

**Tutorial**

1422F - Boring Queries

Author andrew

Solution 94875580, author's solution with persistent segment tree 94875295

**Tutorial**

I am first?

yes

Hi, I have a doubt in the tutorial of problem D(Returning home), if I take P1=(1,1), P2=(5,50), and P3=(3,2), then distance(P1, P3)+distance(P3, P2)=1+2=3 is not equal to distance(P1, P2)=4. I am not sure if I am missing anything, can someone please help?

you're right. The minimum distance(P1, P2)=3. Our target is to minimize the distance.

Understood, thanks for the help. Got confused by the line "the distance between the first and second point will be

equal tothe sum of the distances between the first and third and between the third and second.", I think it should be "less than equal to".Nope man ! see first of all the distance between first and third and third and second is equal to the first and second is right provided they are first , second and third by some parameter which here can be x co-ordinate or y co-ordinate . Refer to your example , Note that third is middle as referred in the editorial , it's not third by sort, so if we sort by x we have : first — (1,1) third- (3,2) and second — (5,50) so distance between first and third = |1-3| = 2 distance between third and second = |3-5| = 2 distance between first and second = |1-5| = 4 so LHS = 2 + 2 = 4 and RHS = 4 Hence , LHS = RHS . Similarly one can do it for y as well if if were less than or equal to than we have to connect another edge and that situation may lead to O(m^2) edges so that we want to avoid . If you still in doubt you can watch : https://www.youtube.com/watch?v=X1nmTo_Gkww&t=2s

Hi, thanks for the detailed explanation. But, referring to your explanation the distance between first and third point will be |1-2|=1 and not |1-3|=2.

Well ,you should take the distance between their x — co-ordinates here now not the y — co-ordinates even if here y ones are near but for that we have to sort them according to y first and then take the distance of 2 as you are saying ,right! .And also surely we will connect them based on their y — co-ordinates as well too . I hope you get my point now . If you still in doubt watch the video i shared earlier first .

How is median of all numbers equal to the average of the sorted set ? Consider 1, 1, 1, 1000 median = 1 average = 334.3333

Hello @tmaddy, you are forgetting here that the set contains only three elements, and in the case of three elements median=mean.

1, 3, 1000

I don't know about the tutorial, but we need to find minimum shift required from all four numbers to get a common number. This can be accomplished using median of four numbers. For detailed information, please visit: on-a-1-d-line-the-point-that-minimizes-the-sum-of-the-distances-is-the-median

Hey thanks for mentioning the rather important property!

By average of the sorted set, they want to say the middle element of the set though they didn't properly specified. Anyway the thing used to solve the problem is solving the following problem: given 4 no.s a,b,c and d and you have to find min value of |x-a|+|x-b|+|x-c|+|x-d| which is a trivial problem that you can easily solve by plotting it's graph. The function is minimum for range [b,c] considering a, b, c, d non-decreasing

thanks @kavishlodha123 :)

Welcome, you can always upvote

Problem C: How the number of combination of left side will be i*(i-1)/2

It is basically nC2 where n = i-1 since the editorial is considering 1-base indexing. So if we are at ith position and we consider removing left side substring we know the power of ith element for sure since right side is static so for every substring removal of left side it will contribute that same power. Therefore we need to check how many total substrings can be generated on left side i.e.nC2(choose two ends of substring in nth length string where n = i-1)

No, it should be (kC2 + i-1), while k = i-1. kC2(k=i-1) means we choose 2 objects from i-1 objects, and i-1 means we choose 1 object from i-1 object. Because we can move 1 or more digits from the left. And remember, kC2(k=i-1) is equal to (i-1)(i-2)/2.

Can anyone explain the persistent segment tree solution on F?

have you understood the editorial one , can you explain me the second part in the editorial (finding lcm for max prime-factors of ai)?thanks in advance!!

Problem E can be solved without hashes, as we could just calculate the first character which is not equal to the first character of each suffix. It could be easily re-calculated when we add a new character to each suffix. After that when we have an option to remove 2 equal characters we should compare those 2 characters with the first not equal character, and if it's smaller then we should remove those 2 characters. 94883205

Anyone explain how to calculate the lcm for those max prime-factor of every Ai(second part) in problem F, i haven't understood the editorial?

I have an alternate solution to the problem C. My solution : Solution . In this, I assumed dp[i] to be the required sum of the numbers if I consider first i digits of the number. Now, I have two choices either to include the last digit or not. If I include the last digit then the sum of the numbers will be equal to 10*dp[i-1]+(i*(i+1)/2)*(ith digit). Here i*(i+1)/2 indicates the number of times in which ith digit will appear in one's place. Now coming to the 2nd possibility, i removed the last digit and in the question, it is mentioned that we can remove a continuous segment so I will have to add all those numbers which will form upon removing the digits one by one from right to left. That value is val2 variable in my code. I used 0-based indexing!! dp[0]=1st digit.

It may be a bit dumb of me, but why doesn't the greedy work in question E? Let's say at a point we have a streak of characters followed by a different character like this: "...xxxxy..." where x and y stand for any arbitrary character. If x > y, then it should always be better for us to delete as much x as we can. We would leave one x if the number of x is odd and delete all of them otherwise. On the other hand, if x < y, keeping all x should always be lexicographically smaller. What am I missing here?

EDIT: Although I don't think it will contribute that much to my question, here's my submission if you want to look at it: 94749759

The y's could be deleted previously! So it's more like xxxxyyz so we should be comparing x with z or y here depending on whether the double y are deleted in your previous choices. And also for xxxyyxz we may also compare x with z or y.

Ohhhh, well that sums it up. Thanks a lot!

In problem F, shouldn't $$$k$$$ be $$$\frac{\sqrt{MaxA}}{\ln{\sqrt{MaxA}}}$$$ instead of $$$\sqrt{MaxA}\ln{\sqrt{MaxA}}$$$?

Can someone plz explain a bit more clearly (proof, with an example maybe, with better intuition) in problem D editorial.

`It turns out that for each point of the graph it will be enough to draw the edges to the points nearest along the x axis in both directions. Similarly for y.`

Thanks in advance!

consider these points (1,2) , (2,4) , (3,8) and (4,16). if you want to consider the edge from (1,2) to (3,8) with cost 2 (i.e. from (1,2) to (3,2) and from (3,2) to (3,8) using the teleportation) , instead of that edge travel from (1,2) to (2,4) with cost 1 (i.e. from (1,2) to (2,2) and then from (2,2) to (2,4) using teleportation) and then from (2,4) to (3,8) with cost 1 (i.e. from (2,4) to (3,4) and then from (3,4) to (3,8) using teleportation).

So our main observation is that if we want to consider any edge from point x to point y instead of considering that edge from x to y , add on all the edge weights of (x->x+1) , (x+1->x+2).......,(y-1->y) , beacause the cost for these both cases would be same and the no of edges that we have to consider decreases from m^2 to o(m).I don't know if you have asked this or not , if not just ignore this.

Thanks for your reply. Can you please tell how the number of egdes decrease from O(m^2) to O(m). and what optimisation u used for it. Sorry, but For me the lines you wrote in black were a bit unclear for me :(

actually for given m teleportation points , we will not consider all the possible edge pairs (m^2) , instead we will form only edges of adjacent x values and the edges of adjacent y values so total no of edges we will consider is 2*(m-1) . consider p1,p2,p3...pm are m teleportation points(sorted acc to x coordinates) and we will consider only these edges i.e. (p1<->p2) , (p2<->p3) , ..... and the reason why we will not consider these type of edges (p1<->p5) or (p2<->p7) is beacuse we can get the same cost, as we go from p2-p3 and then p4-p5 and then p5-p6 and then p6-p7 , we will use this path (because the cost would be same so no need to consider the useless edges like (p2<->p7))

(Try with pen & paper for an example).it will be more clear.Thanks a lot! It is now more clear to me. Finally Got it :)

Hi I had a doubt in the first question. In the tutorial it is written that the condition is satisfied by putting d = a+b+c-1. This is what I intuitively thought too and wrote a code outputting d as a+b+c-1. But the online judge reported WA in one of the tests. Please help

Integer Overflow

Your Submission

Why should overflow occur, I have used long datatype. My code:

Codeforces' judges use Windows operating system so

`long`

is 32-bit which is equal to`int`

.You can run the following code on custom invocation:

You should use

`long long`

(which is 64 bits) for the reason mentioned by alwyntandiono.I use IT to solve F but it wrong answer on test 4.This is my submission https://codeforces.com/contest/1422/submission/94746466 .Can someone help me,pls?

can you explain your logic for F , i haven't understood the editorial.

I think lcm(a,b,c,d)=lcm(lcm(a,b),lcm(c,d)). so I manage my IT 's node: node[i](l->r) is lcm of a sequence from a[l] to a[r] node[i]=node[i*2]*node[i*2+1]/__gcd(node[i*2],node[i*2+1])

I cant get my solution for problem C : bargain accepted in java I am using the same logic as provided in editorial GETTING WRONG ANS IN TEST CASE 6 PLEASE HELP ME My submission in java: https://codeforces.com/contest/1422/submission/94912651

`for (int i = n - 1; i >= 0; --i)`

change`i`

from`int`

to`long long`

.It seems that

`int * long long -> int`

. I'm not sure.Yeah, problem E can be solved without hashes. Here's my solution and I think it is more delicate. I used an array better[i], which means "is the lex order better after deleting character i and only i in the stack". The logic is way more simple than I thought during the contest, and the core logic is actually expressed in less than 10 lines! Refer to my code for more details.

If you know how to solve F , can you explain the logic?

NVM, already answered here

Can anyone help me the problem F. 94925909. I have tried a lot of test cases and my answer is always right. But it wa on test4.

I am wondering if anyone can take a look at my submission for problem E. It gets WA 46 and I have already accounted for the issue of removing pairs of indices which are not adjacent in the original string. I am not really sure what else is wrong, a hint or a break case would be greatly appreciated!

Failing test case:

SpoilerInput: bbbccbbb

Expected Output:

Note that bb is lexicographically smaller than bbbb (your output).

Thanks for your help, now I got accepted! ♪

I cant understand B at all from the editorial. I thought that there should be a case with pairs of twos, not just fours? for instance when you take the case of 3 rows and consider a11 and a13.

Here is a solution that looks clean

`for _ in range(int(input())): n, m = map(int, input().split());A = [list(map(int, input().split())) for kk in range(n)];ans = 0 for i in range(n): for j in range(m):arr = sorted([A[i][j],A[n - i - 1][j],A[i][m - j - 1],A[n - i - 1][m - j - 1]]);ans += (arr[1] - arr[0] + arr[3] - arr[2] + 2 * (arr[2] - arr[1])) print(ans // 4)`

Could anyone explain to me how that for loop works? I cant see the ans+= line at all

I just think the best name for problem F is "The real Forced online Query"

Because I have seen one named "Forced online Query" can be solved by offline method QAQ

It is wrong that in A task the answer is (a + b + c — 1). To tell the truth, before giving the solution that passed all the tests I tried (a + b + c — 1) as the answer and it did not pass.

That's because a+b+c-1 can overflow.

Thank you =)

The editorial of Problem E is really beautiful , i tried to give it some wings with my understanding in the following link : https://www.youtube.com/watch?v=EMCZ1bMwoS0 Hope it may help some :)

....

Can anyone help me with the proof of problem A?

About Problem E, I have understand the solution of author, but I can't find what's wrong with my way. My approach is based on 2 conclusion (which may be wrong): 1. For any suffix, the maximum continuous prefix which consists of the same letters should either not be removed once, or be removed as much as possible. 2. If the first char right side of the prefix is smaller than chars in prefix, we should remove pairs in prefix as much as possible, otherwise we should not remove any pairs in it. So I traverse the string from back to front, and follow the above strategy to remove pairs greedily. If I removed a pair, I will mark it to avoid to remove chars not adjacent afterwards. Finally I got WA on test 46, and I am very confused. Can someone help me find the bug?

Please help me on Problem C. I have written my code and it is working but fails in TC6. I have also checked with other TC's on the problem and it works fine and I am not able to understand the problem in my code.

https://ide.codingblocks.com/s/360158

What is your idea behind this code? how your solution works? is very difficult to see a code without know what are you thinking with that.

What $$$ digit[i] $$$ and $$$ sm[i] $$$ does mean in your code?

And what your functions

`int fxp(int a,int b,int m)`

and`int m_m(int a,int b,int m)`

are supposed to do?Alternative solution for C:

Where:

$$$ n $$$ = length of the number given at input.

$$$ suffix[i] $$$ = number formed by the suffix that ends at i position.

$$$ prefix[i] $$$ = number formed by the prefix that ends at i position.

$$$ f[i] = prefix[i] + f[i-1]$$$

The above formula is a faster way to calculate all of $$$ g[l][r] $$$, where $$$ g[l][r] $$$ is the number formed without the segment $$$[l,r]$$$:

$$$g[l][r] = prefix[l-1] .10^{n-r} + sufix[r+1]$$$

My solution: 96168877