### 300iq's blog

By 300iq, 2 years ago,
• +124

| Write comment?
 » 2 years ago, # | ← Rev. 3 →   -30 Problem solved howI just pretend it didn't exist
•  » » 2 years ago, # ^ |   0 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
•  » » » 2 years ago, # ^ | ← Rev. 2 →   0 rep1(i,n-1){ // if(a[i] != a[i-1]+x){ // val++;a[i]=a[i-1]+x; // } } but for loop is still uncommented and it should run isn't it i just comment if part i know logic is not correct i am just asking about TLE
•  » » » » 2 years ago, # ^ |   0 compiler is not that stupid to run code that have no effect
•  » » » » » 2 years ago, # ^ |   0 compiler is not that stupid to run code that have no effect i don't think so i change the code rep1(i,n-1){ int x = 1,y=2; x = x+y; ans=0; } now the loop has to run according to you cause it is doing something inside for loop the time is still 452ms now what ????
•  » » » » » » 2 years ago, # ^ |   0 still has no effect (ok, actually has (ans = 0), but I think compile run it one time and skip after)
•  » » » » » » » 2 years ago, # ^ | ← Rev. 2 →   0 but I think compile run it one time and skip afterare you sure int x = 1,z=2; rep1(i,n-1){ x = x+z; ans=x; } same result
•  » » » » » » » » 2 years ago, # ^ |   0 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
•  » » » » » » » » » 2 years ago, # ^ | ← Rev. 2 →   -25 .
•  » » » » » » » 2 years ago, # ^ | ← Rev. 2 →   0 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...
•  » » » » » » 2 years ago, # ^ |   0 compiler is not that stupid to run code that have no effectthis 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.
•  » » » » » » » 2 years ago, # ^ |   0 sorry, that Pictures not clear enoughthis is the code so you can try it yourself. Code:#include using namespace std;int a[10000005];int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long res=0; for(int i=0;i<10000000;i++)a[i]=i; for(int j=0;j<100;j++){ res=0; for(int i=0;i<10000000;i++)res+=a[i]; cout<
•  » » » » » » » 2 years ago, # ^ |   0 thanks for reply clearly but my question is still same int x = 1,z=2; rep1(i,n-1){ x = x+z; ans=x; } 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 rep1(i,n-1){ if(a[i] != a[i-1]+x){ val++;a[i]=a[i-1]+x; } } ans = min(ans,val); the time cross all bound 3452ms which is around +3second how and why??
•  » » » » » » » » 2 years ago, # ^ |   0 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.
•  » » 2 years ago, # ^ | ← Rev. 2 →   0 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
•  » » » 2 years ago, # ^ |   0 actully even if i run the 3 loop , time is still 452ms please read above comment https://codeforces.com/blog/entry/98501?#comment-872991
•  » » » » 2 years ago, # ^ |   0 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;
•  » » » » » 2 years ago, # ^ |   0 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)
•  » » » » » » 2 years ago, # ^ |   0 yes sorry i was wrong i guess this may be due to the fact that floating point arithmetic is slower.
•  » » » » » » » 2 years ago, # ^ |   0 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
•  » » 2 years ago, # ^ | ← Rev. 2 →   +3 Opinion: Try solving without using float or any division.As far as what you are asking no idea man really
 » 2 years ago, # | ← Rev. 2 →   -21 .
•  » » 2 years ago, # ^ |   +2 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.
•  » » » 2 years ago, # ^ |   0 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
•  » » » » 2 years ago, # ^ |   0 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.
•  » » » » » 2 years ago, # ^ |   0 same here also
•  » » » » 2 years ago, # ^ |   0 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 shanks said and This is my new code
•  » » » » » 2 years ago, # ^ |   0 Can you please explain this code  int d=v[j]-v[i],r=j-i; int g=gcd(d,r); g=abs(g); d/=g; r/=g; in your code.What it means and why you used?Thanks in advance
•  » » » » » » 2 years ago, # ^ |   0 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-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)
•  » » » » » » » 2 years ago, # ^ | ← Rev. 2 →   0 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.5 But we already know that array only has integers and to skip those real values and 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 5 4 5 6 100 50 9 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 10 and I should change 3 elements to get my required sequence. I hope you understand now and if you have any doubts please comment down.
•  » » » » » » » » 2 years ago, # ^ |   0 Thanks a lot got your approach
•  » » » 2 years ago, # ^ |   0 What is the time complexity of that?
•  » » » » 2 years ago, # ^ |   0 I believe it's O(n^3)
•  » » » » » 2 years ago, # ^ |   0 I think that is O((n^3)*t) i.e O(34300000)>10^7 but i dont understand why that works?
•  » » » » » » 2 years ago, # ^ |   0 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.
•  » » 2 years ago, # ^ | ← Rev. 2 →   +1 There is no detail. Two observations.The formula given in the problem statement corresponds to an AP. (you can prove it easily) You can choose 1 element and make the rest equal to it. n — 1 operations. You choose any two elements to remain the same and now that u have done that your upper bound to answer is n — 2. From 2. 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.
 » 2 years ago, # | ← Rev. 2 →   +12 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?
•  » » 2 years ago, # ^ |   0 Yes, unless n == 1
•  » » » 2 years ago, # ^ |   +5 Yeah, you're right, I didn't think about that :/
 » 2 years ago, # |   +5 could someone explain D in more detail.
•  » » 2 years ago, # ^ |   +10 Here's my solution based on the editorial, hoping it helps. for (auto &it : a) it -= x; vector marked(n, true); for (int i = 1; i <= n - 1; ++i) { if (a[i] + a[i - 1] < 0 and marked[i - 1] == true) marked[i] = false; if (i >= 2 and a[i] + a[i - 1] + a[i - 2] < 0 and marked[i - 1] == true and marked[i - 2] == true) marked[i] = false; } cout << count(all(marked), true) << endl;
•  » » » 2 years ago, # ^ |   +1 Thanks bro. Nice code
•  » » » 2 years ago, # ^ |   +2 Can you please explain the editorial for D. I understood until it substract x from all elements after that i am blank
•  » » » » 2 years ago, # ^ |   +3 https://codeforces.com/contest/1616/submission/141221733 see the comments in the beginning
•  » » » 2 years ago, # ^ |   +1 Nanadaime hokage
•  » » » 2 years ago, # ^ |   0 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
•  » » 2 years ago, # ^ |   +3 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
 » 2 years ago, # |   0 can anybody help me pls in "B"problem I 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.
•  » » 2 years ago, # ^ |   +6 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
•  » » 2 years ago, # ^ |   0 You can also use pushback function for that. It is much faster
 » 2 years ago, # |   0 what's wrong with my code ? please help my submission : problem C thanks in advance
•  » » 2 years ago, # ^ |   0 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
•  » » » 2 years ago, # ^ |   0 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...
•  » » » » 2 years ago, # ^ |   0 Go for decimal places>=5
 » 2 years ago, # |   0 Could someone explain 1616F - Tricolor Triangles in a little bit more detail?
•  » » 2 years ago, # ^ |   +5 I found these resources helpful:SecondThread streamProof for $m\sqrt{m}$ bound
•  » » 2 years ago, # ^ |   0 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.
 » 2 years ago, # |   +28 Brute force with random can passed...there are many limitations, and we just choose 1000 of them randomly and it got accepted...141155530
•  » » 2 years ago, # ^ |   +20 well there are only 36 tests in system test and most of the code got TL on 37...
 » 2 years ago, # |   0 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 .
•  » » 2 years ago, # ^ |   +3 Ratings haven't been updated yet.
•  » » » 2 years ago, # ^ |   +3 Oh thanks sir but could be help me more as I am new to platform that when will ratings be updated most probably?
•  » » » » 2 years ago, # ^ |   +13 Probably sometime today or in the worst case tomorrow.
 » 2 years ago, # | ← Rev. 7 →   +40 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. We want to make $s_1 = s_2 = \ldots = s_{y - 1} = t_1$ and $s_y < t_1$ for some $y \leq x$. For each possible $1 \leq y \leq x$, we could fix the first $y - 1$ occurrences of $t_1$ and then move the first occurrence of a character less than $t_1$ to $y$, and take the min over these subcases. However since we need to remove the first $y$ occurrences of $t_1$ from $s$ so we can find the actual distance of the closest character less than $t_1$ after moving them, we use upto $O(n)$ time for each $y$, this could become $O(n ^ 2)$ overall for a large block, so this wont work. If you write out and compare the equations for various $y$, we can notice that there is always an optimal $y = z$. This is just the smallest $z \leq x$ such that $s_z \ne t_1$ before we perform any moves. (or $x$ if there is no such position) So with this optimal $y = z$, $ans = min(ans, cost + (\text{first_smaller} - z))$, where $cost$ is the cost for previous blocks and $\text{first_smaller}$ is the first index such that $s_{\text{first_smaller}} \lt t_1$ The smaller element will be in a later block. We want to make $s_1 = s_2 = \ldots = s_x = t_1$. Let the first $x$ positions of $t_1$ in $s$ be $pos_1, pos_2, \ldots, pos_x$, then the min cost of making it equal is $\sum_{i = 1}^{x} {pos_i - i}$. Since the first $x$ characters of $s$ and $t$ will be equal after this operation, we can note this is identical to the initial problem but comparing $s$ with the first $x$ occurances of $t_1$ removed to $t[x + 1, n]$ instead of comparing $s$ to $t$. So we set $cost = cost + \sum_{i = 1}^{x} {pos_i - i}$ and repeat the process for the first block of the newly obtained strings. 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)
 » 2 years ago, # | ← Rev. 2 →   +83 Can someone explain how do you use bitsets in F? Isn't it mod 3?
•  » » 2 years ago, # ^ |   +36 Same question here...
•  » » » 2 years ago, # ^ | ← Rev. 2 →   +72 This is a simple way to implement vectors in $\mathbb Z/3\mathbb Z$ using bitsets (not sure about the efficiency though): Spoilerstruct bitset3 { bitset<256> vals[3]; int get(int id) const { if (vals[0][id]) return 0; if (vals[1][id]) return 1; if (vals[2][id]) return 2; } void set(int id, int val) { vals[0][id] = vals[1][id] = vals[2][id] = 0; vals[val][id] = 1; } }; bitset3 add(const bitset3& A, const bitset3& B) { bitset3 C; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { C.vals[(i+j)%3] |= A.vals[i] & B.vals[j]; } return C; } If 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].
•  » » » » 2 years ago, # ^ |   +13 Thanks. Btw, I think you meant vals[0][id] = vals[1][id] = vals[2][id] = 0;
•  » » » » 2 years ago, # ^ | ← Rev. 2 →   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: 141818760EDIT: If someone has better/faster implementation I could use, please post it here :)
 » 2 years ago, # |   +8 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 .  int n; cin>>n; vi a(n); for(int i=0;i>a[i]; } int ans=1e9; for(int i=0;i mp; int mx=0; for(int j=0;jj)diff=a[i]-a[j]; else diff=a[j]-a[i]; int sign=1; if(diff<0)sign=-1; diff=abs(diff); int g=__gcd(diff,tot); diff/=g; tot/=g; diff*=sign; mp[{diff,tot}]++; mx=max(mx,mp[{diff,tot}]); } int change=n-1-mx; ans=min(ans,change); } cout<
 » 2 years ago, # |   0 problem C why it is failing on testcase 7141145372 this is my submission id
•  » » 2 years ago, # ^ |   +1 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.
 » 2 years ago, # |   +5 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 ai, 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 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]
•  » » 2 years ago, # ^ |   0 In D what are we checking by using 2 pointers ?
•  » » » 2 years ago, # ^ |   0 I think he means to say that when ever your sum becomes negative than that j won't be selected.
•  » » » 2 years ago, # ^ |   0 if you are interested, i just implemented and got this two pointer method accepted. you can see it here: 141273634
•  » » 2 years ago, # ^ |   0 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 please
•  » » » 2 years ago, # ^ |   0 Actually 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.
•  » » » » 2 years ago, # ^ |   0 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.
•  » » » » » 2 years ago, # ^ |   0 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
 » 2 years ago, # |   0 Problem E:How can Binary Indexed Tree be useful? I'd be grateful if someone explained.
•  » » 2 years ago, # ^ |   +6 let's say we have this array 1 2 3 4 5 6 a b c d e f 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 1 2 3 4 5 6 1 1 1 1 1 1 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 1 2 3 4 5 6 1 1 1 0 1 1 prefix_sum 1 2 3 3 4 5 now if you noticed the prefix sum of 5 and 6 will reflect the new index 4 and 5 hope this helps.
•  » » » 2 years ago, # ^ |   0 Thanks.What if we want to insert some character somewhere in the middle?
•  » » » » 2 years ago, # ^ | ← Rev. 2 →   0 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.
•  » » 2 years ago, # ^ |   +1 https://codeforces.com/contest/1616/submission/141260604 see the code i have added comments to explain the approach
 » 2 years ago, # |   +8 The editorial for problem G seems to have "a -> b" in the reversed/wrong order.
 » 2 years ago, # |   0 can somebody explain d in detail after subtracting x what is to be done?
•  » » 2 years ago, # ^ |   0 I solved this by brute force in a window with a size of 5 elements (2 fixed + 3 bruteforced) 141174401
•  » » 2 years ago, # ^ |   0 https://codeforces.com/contest/1616/submission/141221733 see the comments in the beginning
 » 2 years ago, # |   +3 Hello. I can't understand why my solution of D problem 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 prefix i isn'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 for i + 1. In that situation we know that for every possible combination of l and r < i problems statement is correct, so we should just check the case there i equal to r and compute the minimum avg_number for all possible l. If that minimum avg_number < x we don't mark x. 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.
•  » » 2 years ago, # ^ |   +8 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 testcase 1 3 2 1 -4 1 You 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.
•  » » » 2 years ago, # ^ |   0 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.
 » 2 years ago, # |   0 Can anybody please give a counter testcase for my solution for problem C (WA on test 3). 141135074And 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?
•  » » 2 years ago, # ^ |   +1 It can happen that none of the original elements share the destination AP's common difference. For example, 1 6 0 1 -1 0 -1 0 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 as 4.
 » 2 years ago, # |   0 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?
•  » » 2 years ago, # ^ |   0 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.
 » 2 years ago, # |   +9 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: 141085542Im currently in a war to let the geometry tag be on C rn
 » 2 years ago, # |   +3 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...
•  » » 2 years ago, # ^ |   +3 The precision of your double variable should
•  » » » 2 years ago, # ^ |   +3 YES,I have solved it,thank you!
 » 2 years ago, # |   +21 Anyone solve problem F by local search ?
 » 2 years ago, # |   0 I did C using Geometry...141250782
 » 2 years ago, # |   0 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.
•  » » 2 years ago, # ^ |   0 1 2 100 100 increase size of count
 » 2 years ago, # |   0 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.
•  » » 2 years ago, # ^ |   0 https://codeforces.com/contest/1616/submission/141221733 see the comments in the beginning
 » 2 years ago, # | ← Rev. 2 →   0 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
•  » » 2 years ago, # ^ |   0 After mirroring, a becomes aa.
 » 2 years ago, # |   +68 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.
 » 2 years ago, # |   -13 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.
•  » » 2 years ago, # ^ |   +3 Yes, but the value of n is very small(1<=n<=70).
 » 2 years ago, # |   0 in problem D ,can someone prove why we check only negative sum segments with lengths 2 ,3 only??
•  » » 2 years ago, # ^ | ← Rev. 2 →   +3 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 a[i] + a[i — 1] < 0 (a[i — 1] >= 0, a[i] < 0 such that a[i] + a[i — 1] < 0 vice versa) or a[i] + a[i — 1] + a[i — 2] < 0 (a[i] < 0, a[i — 1] >= 0, a[i — 2] < 0 vice versa) 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)).
 » 2 years ago, # |   0 Thanks for contest! Really enjoyed problem E :)
 » 20 months ago, # |   0 Good!But H I can't understand
 » 15 months ago, # | ← Rev. 2 →   0 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
 » 8 months ago, # |   0 Problem D is one of the most annoying problem. Nevetheless,authors done a fine job of making annoying mindless machine code implementation problems