### aropan's blog

By aropan, history, 2 years ago, translation,

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

• +101

 » 2 years ago, # |   -74 I am first?
•  » » 2 years ago, # ^ |   +5 yes
 » 2 years ago, # |   +5 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?
•  » » 2 years ago, # ^ |   0 you're right. The minimum distance(P1, P2)=3. Our target is to minimize the distance.
•  » » » 2 years ago, # ^ |   0 Understood, thanks for the help. Got confused by the line "the distance between the first and second point will be equal to the sum of the distances between the first and third and between the third and second.", I think it should be "less than equal to".
•  » » » » 2 years ago, # ^ |   +1 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
•  » » » » » 2 years ago, # ^ |   0 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.
•  » » » » » » 2 years ago, # ^ |   0 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 .
 » 2 years ago, # |   0 How is median of all numbers equal to the average of the sorted set ? Consider 1, 1, 1, 1000 median = 1 average = 334.3333
•  » » 2 years ago, # ^ |   +2 Hello @tmaddy, you are forgetting here that the set contains only three elements, and in the case of three elements median=mean.
•  » » » 2 years ago, # ^ |   +23 1, 3, 1000
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 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
•  » » » 23 months ago, # ^ |   0 Hey thanks for mentioning the rather important property!
•  » » 2 years ago, # ^ |   0 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
•  » » » 2 years ago, # ^ |   0 thanks @kavishlodha123 :)
•  » » » » 2 years ago, # ^ |   +3 Welcome, you can always upvote
 » 2 years ago, # | ← Rev. 2 →   0 Problem C: How the number of combination of left side will be i*(i-1)/2
•  » » 2 years ago, # ^ |   +3 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)
•  » » » 2 years ago, # ^ |   0 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.
 » 2 years ago, # |   0 Can anyone explain the persistent segment tree solution on F?
•  » » 2 years ago, # ^ |   0 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!!
 » 2 years ago, # |   +15 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
 » 2 years ago, # |   0 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?
 » 2 years ago, # |   0 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.
 » 2 years ago, # | ← Rev. 2 →   0 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
•  » » 2 years ago, # ^ |   0 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.
•  » » » 2 years ago, # ^ |   0 Ohhhh, well that sums it up. Thanks a lot!
 » 2 years ago, # |   +14 In problem F, shouldn't $k$ be $\frac{\sqrt{MaxA}}{\ln{\sqrt{MaxA}}}$ instead of $\sqrt{MaxA}\ln{\sqrt{MaxA}}$?
 » 2 years ago, # | ← Rev. 2 →   0 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!
•  » » 2 years ago, # ^ |   0 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.
•  » » » 2 years ago, # ^ |   0 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 :(
•  » » » » 2 years ago, # ^ |   0 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.
•  » » » » » 2 years ago, # ^ |   0 Thanks a lot! It is now more clear to me. Finally Got it :)
 » 2 years ago, # |   0 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
•  » » 2 years ago, # ^ |   0 Integer OverflowYour Submission
•  » » » 2 years ago, # ^ | ← Rev. 2 →   0 Why should overflow occur, I have used long datatype. My code: #include using namespace std; int main() { int t; long a,b,c; cin>>t; for (int i=1;i<=t;i++) { cin>>a>>b>>c; cout<
•  » » » » 2 years ago, # ^ | ← Rev. 2 →   0 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: cout << sizeof(int) << '\n'; cout << sizeof(long) << '\n'; 
•  » » » » 2 years ago, # ^ |   0 You should use long long (which is 64 bits) for the reason mentioned by alwyn.
 » 2 years ago, # | ← Rev. 2 →   0 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?
•  » » 2 years ago, # ^ |   0 can you explain your logic for F , i haven't understood the editorial.
•  » » » 2 years ago, # ^ |   0 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])
 » 2 years ago, # |   0 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
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 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.
 » 2 years ago, # |   0 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.
•  » » 2 years ago, # ^ |   0 If you know how to solve F , can you explain the logic?
 » 2 years ago, # | ← Rev. 2 →   0 NVM, already answered here
 » 2 years ago, # |   0 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.
 » 2 years ago, # |   0 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!
•  » » 2 years ago, # ^ |   +8 Failing test case: SpoilerInput: bbbccbbbExpected Output: 2 bb 1 b 2 bb 1 b 2 cb 1 b 0 1 b Note that bb is lexicographically smaller than bbbb (your output).
•  » » » 2 years ago, # ^ |   0 Thanks for your help, now I got accepted! ♪
 » 2 years ago, # | ← Rev. 2 →   0 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 cleanfor _ 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
 » 2 years ago, # |   0 I just think the best name for problem F is "The real Forced online Query"
•  » » 2 years ago, # ^ |   0 Because I have seen one named "Forced online Query" can be solved by offline method QAQ
 » 2 years ago, # |   0 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.
•  » » 2 years ago, # ^ |   0 That's because a+b+c-1 can overflow.
•  » » » 2 years ago, # ^ |   0 Thank you =)
 » 2 years ago, # |   0 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 :)
 » 2 years ago, # | ← Rev. 3 →   0 ....
 » 2 years ago, # |   0 Can anyone help me with the proof of problem A?
•  » » 16 months ago, # ^ |   0 If you apply triangle inequality taking a diagonal, you will get sum of 3 sides is greater than the third one, similarly, we will get sum of every three sides should be more than the other side. Now we reduce these three inequalities into a single inequality. Let d be the largest side among a,b,c,d. Certainly d < a + b + c, if this is true all there other 3 inequalities are also true, which can easily deduced. Note that as triangle inequality is necessary and sufficient for the existence of a triangle, the inequality deduced above is also necessary and sufficient for the existence of a quadrilateral.
 » 23 months ago, # |   0 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?
 » 23 months ago, # |   0 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
•  » » 23 months ago, # ^ | ← Rev. 2 →   0 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?
•  » » » 23 months ago, # ^ |   +1 Apology for not mentioning the necessary information.My idea for this problem is the calculate the contribution of all the digits in all the possible ways in which we can remove some digits except that particular element.eg:- 1213121 Let's say we can calculate the contribution of digit 3Case 1 => digits which are before 3 would not change the decimal place of 3, so we simply need to add the total number of ways in which we can remove some digits before 3 and multiply with it decimal value i.e 3*10^3.Case 2 => digits at the right of 3 will change the decimal place of 3, so here we add the contribution of 3 on the based of its decimal place. If single elements are removed from the right then its decimal place would be 10^2 and we have a total of 3 ways to do that. Total = 3*10^2 If two elements are removed from the right then the decimal place would be 10^1 and we have total of 2 ways to do it. Total 2*10 And last if we remove all the elements from the right of 3 then Total = 1*1. Total right contribution = digit*(300+20+1) = 3*321 Total contribution of 3 = Left + Right ==> 3*10^3 + 3*321 This thing we apply for all the digits. For the above example power[]=1 10 100 1000 10000 100000 1000000 sm[] = 0 1 21 321 4321 54321 654321 7654321sum_n() = calculates total number of was in which we can remove some digits i.e (i*(i+1))/2This is my Idea. If there are some bugs then please help me to detect them.And function like int fxp(int a,int b,int m) and int m_m(int a,int b,int m) are just part of my template. They do not perform any function in this code.
•  » » » » 23 months ago, # ^ |   0 I found the bug.your solution with some changes of mine: 96360406You just didn't pay attention to the operations module, normally after each operation you must modulate the operands, when you don't do that an overflow can happen (even using long long), so, I just used the module after each operation and everything worked good. (I used the module in the function int sum_n (int n) too).After that, i can only congratulate you with that brilliant solution :D
•  » » » » » 23 months ago, # ^ |   0 Thanks a lot, man!!!. I could have never check that error. Literally thanks, man.
 » 23 months ago, # | ← Rev. 3 →   0 Alternative solution for C: $\sum\limits_{r = 1}^n (f[r-1] . 10^{n-r})+(suffix[r+1].r)$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} + suffix[r+1]$My solution: 96168877
•  » » 23 months ago, # ^ |   0 Thanks for your kind help. I understood your approach but if you please help to find the bug in my code, It would be very kind of you.
 » 23 months ago, # |   0 i want to be green so bad :(
 » 15 months ago, # |   0 For problem B,Why average of the four numbers will not minimize the sum ? Why median and not average of the four numbers ?
•  » » 14 months ago, # ^ |   0
•  » » » 14 months ago, # ^ |   0 Thanks bro, this is very helpful.
 » 14 months ago, # |   0 please someone tell me where my code is going wrong???? for problem c->
•  » » 14 months ago, # ^ |   0
 » 14 months ago, # |   0 Really bad editorial, seems like the writer wrote it only for those who solved the problems.