Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

# | User | Rating |
---|---|---|

1 | jiangly | 3677 |

2 | Benq | 3601 |

3 | ecnerwala | 3542 |

4 | maroonrk | 3540 |

5 | cnnfls_csy | 3539 |

6 | orzdevinwang | 3493 |

7 | inaFSTream | 3478 |

8 | Um_nik | 3430 |

9 | Rebelz | 3409 |

10 | Geothermal | 3408 |

# | User | Contrib. |
---|---|---|

1 | maomao90 | 174 |

2 | adamant | 165 |

3 | awoo | 162 |

4 | TheScrasse | 161 |

5 | nor | 159 |

6 | maroonrk | 158 |

7 | SecondThread | 156 |

8 | Um_nik | 153 |

9 | Geothermal | 145 |

9 | BledDest | 145 |

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial of Good Bye 2021: 2022 is NEAR

↑

↓

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Feb/27/2024 13:33:39 (j1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Problem solved

howI just pretend it didn't exist

When you commented if statement, you allowed compiler to not execute for loop completely. Btw, your solution is incorrect anyway, where did you get 0.5f? Step can be any number represented in x/y where -100 <= x,y <= 100

but for loop is still uncommented and it should run isn't it i just comment

`if`

parti know logic is not correct i am just asking about TLE

compiler is not that stupid to run code that have no effect

`compiler is not that stupid to run code that have no effect`

i don't think so i change the code

now the loop has to run according to you cause it is doing something inside for loop the time is still

`452ms`

now what ????still has no effect (ok, actually has (ans = 0), but I think compile run it one time and skip after)

`but I think compile run it one time and skip after`

are you sure

same result

Loop body doest effected by

`i`

value and there is no loop dependencies so it will be executed only 1 time, not n times. Compilers are very smart to see that.

`still has no effect (ok, actually has (ans = 0), but I think compile run it one time and skip after)`

you think so, the compiler also thinks... wah mauj kar di bhai...`compiler is not that stupid to run code that have no effect`

this a true statement the reason you don't see the difference that you don't use res in anything ,so the compiler still skip the loop.

for example see this two pictures.

Picture 1:Picture 2:As you see the only difference between two code that I commented the cout so that res not used in any thing so the compiler just skipped the two loops.

the cout used only 100 times so it's impossible to make this huge difference the reason is that compiler skipped all the two loops in first case so it's not running even once.

sorry, that Pictures not clear enough

this is the code so you can try it yourself.

Code:#include <bits/stdc++.h>

using namespace std;

int a[10000005];

int main()

{

}

thanks for reply clearly but my question is still same

i am updating

`ans`

in every loop so it must run but still the time is`468ms`

why ???but when i just change like this

the time cross all bound

`3452ms`

which is around`+3second`

how and why??oh, I see I forget to say something.

First, you updating ans but still don't use it whatever the value ans contain it still not matter.

Second, the compiler don't optimize array accessing so that's is why I used an array contain a[i]=i rather than just use i to show you the difference.

your if condition contain array accessing so the compiler can't optimize it.

from my point of view it is because when u commented the if statement it avoided 3*420*420*70 computations, 3 -> there are 3 lines that u commented, 420 ,420 -> because for the outer two for loops ,70-> because of n of inside for loop

actully even if i run the 3 loop , time is still

`452ms`

please read above commenthttps://codeforces.com/blog/entry/98501?#comment-872991

yes i got it i am saying when u are running with commenting the if condition the if number of operations is x, then after u UNcomment the if condition then the operations increase to x+3*420*420*70;

NOTE:

3452ms (with if)

452ms (without if)

does removing or applying the

`if`

condition make this much change in time if`if condition`

is o(1)yes sorry i was wrong i guess this may be due to the fact that floating point arithmetic is slower.

hey no need to say sorry we all are learning but the question is still same is arithmetic operation in floating is that much slow

Opinion: Try solving without using float or any division.

As far as what you are asking no idea man really

.

The equation in the problem statement is an Arithmetic progression. So, all the elements of the array should be in a series.

To go about it, we iterate over every

`i`

and`j`

, and find the common difference of the series fixing the values in those two positions, which is`a[j]-a[i]/j-i`

and fill the array with this progression (like`..,a[i]-d,a[i],a[i]+d,a[i]+2d,....,a[j],..`

)and check the number of elements that do not match with the original array.I did in the same approach but I am getting wrong answer on test case 7, Please tell me what's wrong with my code.This is my code

The WA was because comparing double values using == doesn't always give the right output due to the precision error in rounding up those decimals.

So, what i did was storing the difference in a variable and checking if it is lesser than a small value epsilon for comparision.

I've used code you've put, with this modification and it got AC. here.

same here also

I think I shouldn't use double or may be the precision while converting it into integer causing me trouble(I still don't know). But I used a new approach same as

shankssaid and This is my new codeCan you please explain this code

in your code.

What it means and why you used?

Thanks in advance

It is to get the minimum integer difference between the minimum range of elements. To explain it clearly->

For every pair of elements i and j (where 1<=i<j<=n) I am setting d(difference) as d=a[j]-a[i]; To get difference between consecutive elements we should divide it with difference of their range=> j-i; so d becomes d=(a[j]-a[i])/(j-i); since this value gives us real number and definitely know that the array contains only integers, I used gcd

g=gcd(d,j-i)so that when I divide both a[j]-a[i] and j-i with its gcd I will get integers. Finally we can say that for every (j-i)/g the difference between the A.P(arthematic progression) elements is (a[j]-a[i])/g. I hope you understand this, if you have any doubts please comment down below and (Prerequesite Arthematic progression is must to understand what i said above)I am just adding an example:

One of the test cases:

1 1 2 2the difference between 1st(i=1) and 3rd(j=3) element is 1(a[i]=1) and 2(a[j]=2). d=(a[j]-a[i])/(j-i) => d=(2-1)/(3-1)==>0.5; and the sequence becomes

1, 1.5, 2, 2.5But we already know thatarray only has integersand toskip those real valuesand dividing both numerator and denominator with their gcd: i.e gcd of 1,2 is 1(in this case)=> for every 2 elements the difference is 1(numerator is difference d and denominator is number of elements)===> my sequence is 1 — 2 — where (- is a real value) therefore I need to change 2 elements to get the required sequence.

Let's look at another example: 2

54 5 6100 509 10 and i=1, j=3 , a[i]=2, a[j]=4:: a[j]-a[i]=2 j-i=2 gcd g=2; ==> 2/2==> (2/2)/(2/2)==> 1/1 for every element the difference is 1: and therefore my required sequence is:::2 3 4 5 6 7 8 9 10and I should change 3 elements to get my required sequence. I hope you understand now and if you have any doubts please comment down.Thanks a lot got your approach

What is the time complexity of that?

I believe it's O(n^3)

I think that is O((n^3)*t) i.e O(34300000)>10^7 but i dont understand why that works?

In c++, 10^8 simple operation can be performed in less than 1s. Unless you don't have written some hard algo in each loop it will work. And the same is here.

There is no detail. Two observations.

The formula given in the problem statement corresponds to an AP. (you can prove it easily)

Since you took the effort of fixing two elements in the sequence i.e you can form your AP out of it and check if other elements match with the corresponding AP.

(Yes very magical observation since it is necessary and sufficient for our needs)

So just pick any two elements and keep forming APs take minimum over all of them.

P.S :- Some people have their solutions passed using doubles which isn't technically correct so when you fix your difference for the AP use methods which avoid doubles.

For problem C, isn't the upper bound of the answer always n — 2 and not n — 1, since we can always pick two elements to make an AP and then check how many elements we have to change?

Yes, unless n == 1

Yeah, you're right, I didn't think about that :/

could someone explain D in more detail.

Here's my solution based on the editorial, hoping it helps.

Thanks bro. Nice code

Can you please explain the editorial for D. I understood until it substract x from all elements after that i am blank

https://codeforces.com/contest/1616/submission/141221733 see the comments in the beginning

Nanadaime hokage

I don't understand the idea in problem D..why is there always subsegment of length 2 and 3 which have negative sum when some subarray has negative sum? Also what kind of proof have they given in editorial like x < 0, y < 0 so x + y < 0, i can't relate it

https://codeforces.com/contest/1616/submission/141221733 here is my solution for d with comments please correct me if i am wrong,i have proved why we need to check only subsegmens of size 2 or 3 in comments

can anybody help me pls in "B"problemI have two codes for the problem out of them one is accepted and another is getting TLE . can anybody let me know why this is happening.link1-TLE: https://codeforces.com/contest/1616/submission/141148880 Link2-AC: https://codeforces.com/contest/1616/submission/141149035

ans=ans+s[i] is O(n) while ans+=s[i] is O(1) https://stackoverflow.com/questions/54239654/difference-between-concat-of-strings-s-s0-and-s-0-in-c

You can also use pushback function for that. It is much faster

what's wrong with my code ? please help my submission : problem C thanks in advance

Precision errors bro. Do not directly compare numbers in double format. use a range like instead of if x==y, write if (x-y>=-0.000001&&x-y<=0.0000001) my submission https://codeforces.com/contest/1616/submission/141124140

hey, how should I decide up to how many decimal precision should I set the range for the check, like why have you chosen 0.000001 and not 0.00001 or 0.0000001?

Can u please explain in detail, coz I'm pretty bad with floats, doubles and precisions...

Go for decimal places>=5

Could someone explain 1616F - Tricolor Triangles in a little bit more detail?

I found these resources helpful:

SecondThread stream

Proof for $$$m\sqrt{m}$$$ bound

What the triangle condition actually means is that for every triangle $$$(u, v, w)$$$, $$$a_u + a_v + a_w \equiv 0 \pmod 3$$$. Notice this is a system of linear equations, which is easily solved by Gaussian elimination.

Brute force with random can passed...

there are many limitations, and we just choose 1000 of them randomly and it got accepted...

141155530

well there are only 36 tests in system test and most of the code got TL on 37...

Can someone tell me is this contest was unrated as I participated and its showing this contest name under unrated and still my rating didn't updated too kindly guide me .

Ratings haven't been updated yet.

Oh thanks sir but could be help me more as I am new to platform that when will ratings be updated most probably?

Probably sometime today or in the worst case tomorrow.

In problem E, for some reason during the contest I thought it would be very complex to maintain the shifts using a BIT. So here is another way I used to solve this problem without any data structures (albeit with trickier implementation).

Similar to the editorial we realize that the optimal answer will be of the form $$$s_i = t_i$$$ for all $$$i \lt k$$$ and $$$s_k < t_k$$$ for some $$$1 \leq k \leq n$$$.

Let us suppose $$$t_1 = t_2 = \ldots = t_x$$$ and $$$t_x \ne t_{x + 1}$$$. We have two cases for this block:

The smaller position $$$k$$$ will be in this block.

The smaller element will be in a later block.

So we effectively are repeating the process of case 2 for each block in order, and take the min of case 1 for each block.

However this still runs in $$$O(blocks * n)$$$, and the number of blocks can be upto $$$n$$$ (all characters different), so this is still $$$O(n ^ 2)$$$ in the worst case.

Suppose there exists some $$$t_i \gt t_{i + 1}$$$. Lets say we have processed the blocks upto index $$$i$$$ and made $$$s[1, i] = t[1, i]$$$ at some cost $$$c$$$. Now to proceed with this process we will have to make $$$s_{i + 1} \leq t_{i + 1}$$$. Let the cost of the moves for this be $$$d$$$. But it will take only one more move than this to make $$$s_{i} \lt t_{i}$$$ since $$$t_{i} \gt t_{i + 1}$$$.

So we know the answer is $$$\textbf{AT MOST}$$$ $$$c + d + 1$$$, and that the answer for any further block is $$$\textbf{AT LEAST}$$$ $$$c + d$$$. This $$$c + d$$$ is only achievable when $$$d = 0$$$ and $$$s[i + 1, n] < t[i + 1, n]$$$ which can be checked for in $$$O(n)$$$ time.

If a block has a smaller character than its preceding block we can immediately check if there is an optimal answer from a further block and don't need to check further blocks one by one. So we only care about at most the first 26 blocks. So our solution now runs in $$$O(min(blocks, 26) * n)$$$ which is clearly good enough for the given constraints.

Submission — 141162437 (with comments), 141162629 (without comments)

Can someone explain how do you use bitsets in F? Isn't it mod 3?

Same question here...

This is a simple way to implement vectors in $$$\mathbb Z/3\mathbb Z$$$ using bitsets (not sure about the efficiency though):

SpoilerIf necessary, it can be optimized by keeping only

`val[0]`

and`val[1]`

, since`val[2]`

can be recovered as`val[2] = ~val[0] & ~val[1]`

.Thanks. Btw, I think you meant

`vals[0][id] = vals[1][id] = vals[2][id] = 0;`

Thanks to this I implemented the same gauss function from cp-algo, and it barely squeezed into time limit ~950ms, after optimizations turning bitset3 * 2 and bitset3 * (-1) into swap(bit[1],bit[2]): 141828486.

Without bitset optimizations I passed with ~150ms with same gauss function but with array, and obvious calculation fix: if you can color all edges into one color do so and don't call gauss: 141818760

EDIT: If someone has better/faster implementation I could use, please post it here :)

Problem C can also be solved in O(n^2 log(n)) by just selecting one element at a time(ith element) and then we will calculate the value of d for every other element (d is the common difference by which we can reach jth element without altering the jth element) and we will store it in a map. The maximum value of d(maxd) which has occurred is the number of element which we do not have to change plus the ith element. So the ans will be n-1-maxd .

problem Cwhy it is failing on testcase 7141145372 this is my submission id

Could be due to the precision of your float "delta". You can avoid the float arithmetic by checking

"(arr[k]-arr[i])*(j-i) != (arr[j]-arr[i])*(k-i)",

which is essentially checking if the deltas of the k-i sequence and the j-i sequence are the same:

(arr[k]-arr[i])/(k-i) != (arr[j]-arr[i])/(j-i)

141178264 for reference.

Here are alternative ideas for D and E using two pointers.

D: the first thing to notice is that we can simplify the condition. In this case if we subtract x from all a_i the condition simplifies to having the sum of values in all subsegments be greater than 0. Now all we have to do is use two pointers and greedy. Start the two pointers at the start of the array. Increase the right pointer while this sum is negative. If the current segment length is greater than 1 unselect the last value and set the left and right pointers to after this value. Repeat until you get to the end of the array. Additionally if the sum ever gets greater than or equal to 0 and the segment size is greater than 1 then we should increase the left pointer until it becomes negative or the segment size becomes equal to 1.

E: First notice that there must be a prefix of values where a_i=b_i followed by one value where a_i>b_j. In order to make a<b it is necessary to change one of the values in this prefix or single value. Otherwise a will still be greater than b. It's pretty obvious that we only need to swap to values in a. The answer is the distance between these two values. Let's call these two positions i and j. Without loss of generality the final result of this swap will be a[i] = a[j]. We also know that for this result to work we need it to be the case that a[i]<b[i] after the swap. Therefore we simplify the problem to finding the minimum value of j-i where a[j]<b[i], j>i, and i is a position in the prefix before and including the first different value between a and b (which we will now notate as i<=P). Now we use a common greedy trick where we notice that many values of i and j are unoptimal. In particular we will only keep values so that when i<j, b[i] > b[j] and a[i] > a[j]. This means that we have two monotonic sets for a and b which we can choose from. From here we simply apply a two pointers technique in which i=P and j=N. We decrease j as much as we can while a[j]<b[i]. Then we decrease i. In total this gives us the answer in O(N).

In D what are we checking by using 2 pointers ?

I think he means to say that when ever your sum becomes negative than that j won't be selected.

if you are interested, i just implemented and got this two pointer method accepted. you can see it here: 141273634

It's pretty obvious that we only need to swap to values in a.Bro do u mean atmost a single swap will do the job? Could u elaborate it pleaseActually now that I look at it more I think my e solve is wrong. I forgot about cases like a=373, b=339. Where it is optimal to swap stuff to a place further down the string. Maybe there is some easy way to fix this though that I am missing.

I would really appreciate if you would implement and submit your solution. I want to see if it gets accepted. And let me know your final approch on E.

ok ive done more research and i see now my e solve is completely wrong, sorry. i assumed that only one value had to be swapped into some other position when that is actually not the case

Problem E:

How can Binary Indexed Tree be useful? I'd be grateful if someone explained.

let's say we have this array

if we wanna remove d, now e index becomes 4, and f index becomes 5.

so using Fenwick tree we can maintain the new index using prefix sum

let's first initialize the the BIT array with ones

if you noticed the prefix sum for each index is the index it self

now let's remove d again (index 4) and add -1 so that it will be 0

prefix_sum

now if you noticed the prefix sum of 5 and 6 will reflect the new index 4 and 5

hope this helps.

Thanks.

What if we want to insert some character somewhere in the middle?

I was wondering about the same. Rather than viewing it as the updated index, it's helpful to view prefix_sum($$$pos$$$) as "the number of unprocessed characters in the range $$$s[1..pos]$$$ (1-based indexing)". Here, unprocessed characters are those that have not been used so far in the process of equating the prefixes (and are thus coming in the way of moving our desired character to the end of the prefix). With this view, the "inserting in the middle" question doesn't arise.

In the above example, say that 'd' is moved to position 1, so that now both $$$s$$$ and $$$t$$$ match up to position 1. Now for position 2, suppose we want to bring in 'c'. We have prefix_sum(2)=2 so the cost is 2 steps. Instead, if we want to bring in 'e', the cost is prefix_sum(4)=3 steps.

https://codeforces.com/contest/1616/submission/141260604 see the code i have added comments to explain the approach

The editorial for problem G seems to have "a -> b" in the reversed/wrong order.

can somebody explain d in detail after subtracting x what is to be done?

I solved this by brute force in a window with a size of 5 elements (2 fixed + 3 bruteforced) 141174401

https://codeforces.com/contest/1616/submission/141221733 see the comments in the beginning

Hello. I can't understand why my solution of

Dproblem isn't working. I told that we can use some kind of greedy algorithm. Let's choose every number from prefix in our array. So if our prefixiisn't available(it's organize not possible situation), so we just don't mark this, in other case we just mark that number and go fori + 1. In that situation we know that for every possible combination oflandr< i problems statement is correct, so we should just check the case thereiequal torand compute the minimum avg_number for all possiblel. If that minimum avg_number <xwe don't markx. I barely can understand why this isn't a solution for this problem. Can someone explain why this isn't working and probably give me not such a big sample of this? 141117167 — my submission. I understand that it's working with square n time, but problem with Wrong Answer and it's not very hard to organize nlog(n) time complexity.Let's suppose $$$x = 1$$$, so that the second condition is simplified to:

The subsegment sum should be greater than the subsegment length. If you consider this simple testcaseYou can see that $$$[2, 1]$$$ is a valid subsegment, because $$$\Sigma [2, 1] \geq 2$$$ and $$$[1, -4] \Longrightarrow -4$$$ is not a part of the selection, and $$$[2, 1, -4] \Longrightarrow -4 $$$ is not a part of the selection.

Hence, the maximum number of candidates is 2, while your code produces 1.

Thank you!

141191656 — my submission with this approach. It's based on above conservation that not selected segment should have length 2 or 3.

But we also can build a segment tree and do not care about that notion.

Can anybody please give a counter testcase for my solution for problem C (WA on test 3). 141135074

And I would also like to hear some tips about how to debug the solution if my approach is right? Since last 4-5 contests I am getting the approach and key observations right but not able to code it correctly. How to improve upon this?

It can happen that none of the original elements share the destination AP's common difference. For example,

The optimal way is to convert every element to 0, resulting in a cost of

3, while your current logic would try to guess the difference as $$$(0 - 1)$$$ or $$$(1 - (-1))$$$ or $$$(0 - (-1))$$$ and produce the answer as4.Got it. Thanks. And apologies if you had bad time understanding the code. I actually forgot to add comments.

For problem E's solution: "In general, we can easily see that the optimal strategy is to move characters one by one, every time choosing the closest character".

How can we prove that this is true?

The simplest way to prove this is the fact that we will never swap 2 same characters, so, the relative order of all the same characters(all a's for example) remains the same.

For C, i thought about each element v[i] as being a point in the plane (v[i],i). If a point k is in the same arithmetic progression as points i and j, then (v[i],i), (v[j],j) and (v[k],k) are colinear. That way you just do shoelace theorem to see if the area of the polygon of those 3 points is 0. Code: 141085542

Im currently in a war to let the geometry tag be on C rn

Regarding question C, why is my co``de wrong in the seventh sample? Have a good brother help me to see it, thank you，

Your text to link here...

The precision of your double variable should

YES,I have solved it,thank you!

Anyone solve problem F by local search ?

I did C using Geometry...

141250782

I got a RUNTIME_ERROR verdict on this solution: https://codeforces.com/contest/1616/submission/141075698. Can someone point out the problem? Unfortunately I can't view the full test case because it is very long.

1 2 100 100 increase size of count

Can someone please explain Note that there should be a negative-sum subsegment of length 2 or 3, if there is a negative-sum subsegment. It is easy to see as x<0,y<0⇒x+y<0.

https://codeforces.com/contest/1616/submission/141221733 see the comments in the beginning

in the 2nd question can anyone tell me, a or aa which appears earlier in dictionary. i guess a if so then why is there a test case that has aa as output

After mirroring, a becomes aa.

I’ve used a weird method accepting F in the contest.

Specifically, for each test case, I repeat several times doing the following things: 1. giving edges random value. 2. adjust it to minimize the ternary rings which is not same and not {1,2,3}. If the number of illegal ternary rings haven't changed for several adjusting operation, then I just do it again.

Many C solutions i saw had 3 loops inside each other, doesn't that make its time complexity O(n^3) ? If yes then how it gets accepted.

Yes, but the value of n is very small(1<=n<=70).

in problem D ,can someone prove why we check only negative sum segments with lengths 2 ,3 only??

Assume that all sub segments of length >= 2 before i have positive sum, and we want to know whether there is sub segment ending at i such that sum(sub segment ending at i) < 0 then there are only two ways

Note that we don't look at any other case because of the assumption we made in the starting(any sub segment we choose to combine will just increase the sum(sub segment ending at i)).

Thanks for contest! Really enjoyed problem E :)

Good!But H I can't understand

In D for those, who didn't come up with idea of segments sized 2 or 3, you can use segment tree over prefix sum of a[i] — x to find if the segment beggining in i is less then 0. In that case just exclude i and continue. 187614580

Problem D is one of the most annoying problem. Nevetheless,authors done a fine job of making annoying mindless machine code implementation problems