### pikmike's blog

By pikmike, history, 18 months ago, translation,

1155A - Разверни подстроку

Tutorial
Solution (Vovuh)

1155B - Игра с телефонным номером

Tutorial
Solution (Roms)

1155C - Будильники повсюду

Tutorial
Solution (Vovuh)

1155D - Красивый массив

Tutorial
Solution (PikMike)

1155E - Угадай корень

Tutorial

1155F - Олигополия на доставку

Tutorial
Solution (BledDest)

• +78

 » 18 months ago, # |   +105 My approach for problem D is quite different. I hope it would be easy to understand.At first, I build 2 arrays Left(i) and Right(j) that return the maximum sum of a subarray ending at i with Left(i) and starting at j with Right(j). I also have array Sum(i) is sum of all elements from 1 to i.When we multiply the subarray (l..r), the result in current case is Left(l-1) + x * (Sum(r) - Sum(l-1)) + Right(r+1). We can rewrite it by (Right(r+1) + x * Sum(r)) + (Left(l-1) - x * Sum(l-1)).With this formula, my solution is a loop for i from 1 to n, take i as the right most of the multiply subarray, get the best value of the left most to update the answer.See my submission for more details: 53150702
•  » » 18 months ago, # ^ |   0 i prefer your solution to this editorial's solution. Your solution is more interesting and easier for me to understand. Love it
•  » » » 18 months ago, # ^ |   0 Thank you.
•  » » 18 months ago, # ^ |   0 Can u please explain the formula part in detail I am not able to understand it?
•  » » » 18 months ago, # ^ | ← Rev. 2 →   +1 Let 's take subarray (l..r) to multiply.The sum of this segment is Sum(r) — Sum(l-1) and after multiply is x * (Sum(r) — Sum(l-1)).But we don't stop at this with x is negative number. You can see the first test case, we would try to increase the answer by openning its border.What is best way to do that? My solution is using arrays Left and Right.
•  » » 18 months ago, # ^ |   0 Loved your approach
•  » » 18 months ago, # ^ |   0 What a great solution！！
•  » » 17 months ago, # ^ |   0 So good
•  » » 17 months ago, # ^ |   0 Admire !
•  » » 17 months ago, # ^ |   0 I am unable to understand why did you do this res = max (res, Left[i]); when you had to maximize just these two terms in the last loop Right(r+1) + x * Sum(r)) + (Left(l-1) — x * Sum(l-1))
•  » » » 17 months ago, # ^ |   0 We can choose " at most one consecutive subarray of a " but my math expression is just correct when I choose a non-empty subarray from l to r.
•  » » 17 months ago, # ^ |   0 thank you , your solution is more easier than tutorial
•  » » 13 months ago, # ^ |   0 Beautiful Solution!!!
•  » » 8 months ago, # ^ |   0 This approach really helped a lot man ..thanx :)
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   0 UPD : Resolved
•  » » 3 days ago, # ^ | ← Rev. 3 →   0 [user:l0s3r]Can you explain why you did res = max (res, Left[i]); in your code?
•  » » » 3 days ago, # ^ |   0
•  » » » » 3 days ago, # ^ |   0 Read this but couldn't get you. Could you elaborate more?
 » 18 months ago, # |   -65 why is E so easy
•  » » 18 months ago, # ^ |   0 Humblebrag
 » 18 months ago, # |   +7 I can't get my head around as to why greedy won't work for D. If x is non positive, why can't we just find the maximum negative sum and multiply it by x? My code doesn't pass, so it is definitely wrong, but I don't know why.
•  » » 18 months ago, # ^ | ← Rev. 2 →   +9 Edit: my bad, I gave a wrong example. See the responses.
•  » » » 18 months ago, # ^ | ← Rev. 2 →   +5 I think your example is wrong cause multiplying x with -2 also gives 2+10-1+10 = 21.One example can be : 7 -1-20 21 -15 10 -10 20 10.Here -20 is the largest non-positive segment but it gives 20+21-15+10-10+20+10 = 56.But multiplying -15 by -1 gives 21+15+10-10+20+10 = 66
•  » » » 18 months ago, # ^ |   +5 In your example, you'd get 21 in either case. What you wanted to say was: 4 -1 -5 10 -3 10 
•  » » 18 months ago, # ^ | ← Rev. 2 →   0 5 0 10 -1 10 10 -2 Output:30
 » 18 months ago, # |   +5 Is there another way to get the polynomial on problem E? ( Not using Gaussian elimination )
•  » » 18 months ago, # ^ |   +21 You can use lagrange interpolation
 » 18 months ago, # |   +14 I have an approach for D that is more in the 'style' of Kadane's algorithm. The beautiful part of the array consists of 3 "segments", the 1 part, the X part, and the 1 part again.Like Kadane's, we loop through each x in the array, maintaining bestK = the largest value of K segments ending in the current x value. def solve(N, X, A): ans = best1 = best2 = best3 = 0 for a in A: best3 = max(0, a, best1 + a, best2 + a, best3 + a) best2 = max(0, X*a, best1 + X*a, best2 + X*a) best1 = max(0, a, best1 + a) ans = max(ans, best1, best2, best3) return ans 
•  » » 18 months ago, # ^ |   0 Can you explicate the best3?
•  » » » 18 months ago, # ^ | ← Rev. 8 →   0 Let A[i:j] denote the sum A[i] + ... + A[j] For a given j, best1 = largest possible A[i:j] best2 = largest possible A[h:i-1] + A[i:j] * X best3 = largest possible A[g:h-1] + A[h:i-1] * X + A[i:j] 
•  » » » » 18 months ago, # ^ |   0 I understand it. thank you
•  » » » » 4 months ago, # ^ |   0 Wow I learned the essence of kadanes algorithm from your solution
•  » » 18 months ago, # ^ |   0 damn, I like this solution much more than the editorial, this should be upvoted
•  » » 18 months ago, # ^ |   0 Thank You!
•  » » 6 months ago, # ^ |   0 Can you explain the process a bit more?
 » 18 months ago, # |   +10 For problem B, wasn't it enough to simply ignore the last 10 characters of the string and check whether more than half of the remaining characters are equal to '8'?
•  » » 18 months ago, # ^ | ← Rev. 3 →   +5 Yes. If the number of 8's is at most (n - 11) / 2 or the number of moves each person gets, Petya can remove all the 8's. The number of digits that are not 8's is n - 10 - numberOf8s, therefore if the number of 8's is greater than (n - 11) / 2 the number of digits that are not 8 will be less than n - 10 - (n - 11) / 2 = (n - 9) / 2, which is at most (n - 11) / 2. So Vasya would be able to remove all the non-8's.
 » 18 months ago, # |   0 Can anyone explain editorial's tutorial for problem D in simple and easy way.
•  » » 18 months ago, # ^ |   0 i want this too :/
 » 18 months ago, # |   0 I honestly don't get why my solution isn't working. I get WA on the 11th test case, but the output seems fine to me. Am I missing something?https://codeforces.com/contest/1155/submission/53204105
•  » » 18 months ago, # ^ |   +24 for ( int i = 1; i < n-1; ++i ) { gcd = __gcd(dif[i], dif[i+1]); } Try to find here.
•  » » » 18 months ago, # ^ |   0 Ah, turns out I was careless. Thank you!
 » 18 months ago, # |   +6 In problem E, how can we be sure that the value of f(xi) that we use to interpolate the polynomial is actually the value of the polynomial or less than the actual value because of MOD 1e6 + 3For example, if the polynomial is 1 + (1e6+1)x + x^2, f(0) = 1, f1(1) = 1e6+2, f(2) = 2e6 + 7 MOD 1e6 + 3 = 1. Now the polynomial we interpret will be wrong.
•  » » 18 months ago, # ^ |   0 Isn't f(1) = 1e6+3 = 0?
•  » » » 18 months ago, # ^ |   0 my bad. But the point stands? The function could be changed.
•  » » » » 18 months ago, # ^ |   0 If you do the interpolation with the correct values you'll end up with the correct polynomial and I believe that it will always be correct because of the way modular arithmetic works.
•  » » » » » 18 months ago, # ^ |   0 So assume that the question said instead of 11 degrees the polynomial is 2 degrees only. Then the polynomial interpolated from above info is 1 — 2x + x^2, again obviously wrong. Maybe, it will give the correct value of zero when taken MOD 1e6+3 but I don't find in intuitively clear.
•  » » » » » » 18 months ago, # ^ |   +3 -2 is congruent to 1e6 + 1 mod 1e6 + 3, so 1 — 2x + x^2 is congruent to 1 + (1e6 + 1)x + x^2 mod 1e6 + 3 and the interpolated polynomial is correct.
•  » » » » » » » 18 months ago, # ^ |   0 Got it. Thanks a lot, that helped!
 » 18 months ago, # |   0 Another approach for problem DWe find the Best subarray ending at ith index and at every index maintain three possible sums 1) x has not yet been used previously nor being used by this element 2) x is being used by this element 3) x was used by some prev segment and hence this element cannot use xTransitions for calculating ith index results1) is simple to extend using prev index's result2) either extend prev index result 1) or 2) should multiply a[i] by x3) can extend 2) or 3) but should not multiply a[i] by xhttps://codeforces.com/contest/1155/submission/53223589
•  » » 5 months ago, # ^ |   0 Thanks a lot. It helped me to solve the problem!
•  » » » 5 months ago, # ^ |   0 Glad to know :)
•  » » 4 months ago, # ^ |   0 wow very impressive!! and your solution is very similar to ecnerwala's
 » 18 months ago, # |   +5 My "solution" for F: first use dp to calculate for each subset of vertices and each pair of vertices if there is a path between those vertices passing through exactly gives subset of vertices, exactly once. From this we can get for each subset if there is a cycle passing through each of them exactly once. Iterate over all subsets and identify those, which have a Hamiltonian cycle, but none of their proper supersets does. Now while we have time left pick a random of those subsets, put all edges from the cycle to the graph, random_shuffle all other edges, add them in order and if this edge is not redundant (i.e their endpoints aren't already in the same biconnected componend) add it. After each iteration check if current solution is better, than best found so far. If it is store it. I saw some other people getting accepted with similar approach. Any idea how to defeat it? 53200092
 » 18 months ago, # | ← Rev. 3 →   +15 I've solved problem F with a random way. We can delete some edges in E, and then run Tarjan algorithm to find all strongly connected components. If the graph is bi-connected, the all nodes are supposed to form a single strongly connected component. The number of deletion strategy is 2^|E|, it's too large to try all possible strategies. But we can turn to a greedy way, iterate over each edge, if it's redundant, just delete it. You need run the process several times to get a satisfying result, before each process, shuffle the E at first.
•  » » 18 months ago, # ^ |   0 That's quite unfortunate.I think if the edges were weighted the model solution would look the same, but any randomized shit would be in trouble because e.g. for random weights it's very likely that optimal solution is unique, or the assertion I used that if for some set of vertices with Hamiltonian cycle there is its superset which also has a Hamiltonian cycle is no longer true.
•  » » 16 months ago, # ^ | ← Rev. 2 →   0 Can you share your submission?...@dalt
•  » » » 16 months ago, # ^ |   0 My submission is written on java, hope it will help 53384647
 » 18 months ago, # | ← Rev. 2 →   0 I find a way using greedy and segment tree to solve D You can see my submission 53466247.Pikmike
•  » » 18 months ago, # ^ |   +2 Can you explain your solution?
•  » » » 18 months ago, # ^ |   0 Let we make l the start of the range we multiple x.And then we need to find best r to be the end of the range we multiple x.The answer of [l,r] to multiple x is left[l]+sum(l,r)*x+right[r].Because the left[l] is fixed, we only need to consider sum(l,r)*x+right[r].Then we can use a segment tree to find the best r quickly.
 » 18 months ago, # |   0 Problem E: What would be the problem if the limit was not a prime?? Can anyone explain??
•  » » 18 months ago, # ^ |   +1 In this problem, we use Gaussian Elimination to solve a modular SLAE (System of Linear Algebraic Equations). In Gaussian Elimination, some steps involve division operations. So, the division steps, in this case, involve finding out the multiplicative inverse of the denominator and multiplying the numerator by that. The multiplicative inverses may not exist if the module is not prime and in that case, we will not be able to use Gaussian Elimination to solve the problem.
•  » » » 18 months ago, # ^ |   0 Got it.Thanks a lot
 » 17 months ago, # |   +3 Where can I read about Gaussian elimination method?
•  » » 4 months ago, # ^ |   0 change ur profile pic i thought there is something on my screen so tried to rub it off
 » 17 months ago, # |   0 For problem D can someone explain me what ecnerwala did to solve it. His code looks quite simple but I am unable to understand it :( . Here's the link to his solution 53141642
•  » » 11 months ago, # ^ |   0 Okay, so this is what I understood:A number can be in these 5 states: Not included in the range, and is to the left of the included range Included in the range, but not multiplied by x, before the range that is multiplied by x Included in the range, and multiplied by x Included in the range, but not multiplied by x, after the range that is multiplied by x Not included in the range, and is to the right of the included range. This gives you good information about the first loop.The second loop is about saying "the maximum sum, doing whatever, before the current element".So for (int z = 0; z+1 < 5; z++) dp[z+1] = max(dp[z+1], dp[z]); makes dp useful for the next for (int z = 0; z < 5; z++) dp[z] += weights[z] * v; Where, we calculate: dp[i] = dp[i](The max until i, by including, not including, multiplying etc) + weights[i]*v(the weight according to state) ;
•  » » 4 months ago, # ^ |   0 that is damn cool
»
17 months ago, # |
Rev. 2   0

what is wrong in this code 1155D

# include<bits/stdc++.h>

using namespace std;

# define pb push_back

ll max(ll a,ll b) { if(a>b) return a; return b;

}

int main() { ll n,x; cin>>n>>x; ll a[10000007]={0}; for(ll i=0;i<n;i++) { cin>>a[i];

}
ll dp[100005][3];
dp[0][0]=max(a[0],0);
dp[0][1]=max(a[0]*x,0);
dp[0][2]=max(a[0],0);
for(ll i=1;i<n;i++)
{
dp[i][0]=max(0,dp[i-1][0]+a[i]);
dp[i][0]=max(dp[i][0],dp[i][2]+a[i]);
dp[i][2]=max(dp[i-1][1]+a[i],dp[i][0]);
dp[i][2]=max(dp[i-1][2]+a[i],dp[i][2]);
dp[i][2]=max(0,dp[i][2]);

dp[i][1]=max(dp[i-1][0]+a[i]*x,dp[i-1][1]+a[i]*x);
dp[i][1]=max(0,dp[i][1]);
}
ll maxi=0;
for(ll i=0;i<n;i++)
{
for(ll j=0;j<3;j++)
{
// cout<<dp[i][j]<<" ";
if(maxi<dp[i][j])
maxi=dp[i][j];
}
//  cout<<endl;
}

cout<<maxi;


}

•  » » 16 months ago, # ^ | ← Rev. 2 →   0 The size of dp array is 100000 and should be 300000
•  » » » 16 months ago, # ^ |   0 thanks for that
 » 16 months ago, # | ← Rev. 2 →   +3 1155D - Beautiful Array — My solution is similar to alexwice and arajatchauhan813, you just have a dp table of n * 3 in which you have:dp[i][1]:the maximum sum without multiplying until idp[i][2]:the maximum sum up to i having already multiplied and without multiplying from idp[i][3]:the maximum sum up i multiplying a piece that ends in iThen :  for(int i=0;i
 » 2 months ago, # |   0 Can anyone please point out why is this submission wrong for problem D 90055454
•  » » 2 months ago, # ^ |   0 What is test case 66 ?