### giorgosgiapis's blog

By giorgosgiapis, 3 years ago, ,

Fourth round of COCI 2017/2018 season will take place this Saturday at 14:00 UTC.
You can find the full schedule for this season here and register for the contest here.

Let's discuss the problems here after the contest.

• +31

 » 3 years ago, # |   0 Remainder: Contest starts in less than 20 mins.
 » 3 years ago, # |   0 How to solve the third problem (Automobil) for N,M <= 1'000'000?
•  » » 3 years ago, # ^ | ← Rev. 4 →   +13 Let's calculate the answer first without any operations. Then calculate array R and C, R i denotes the product of all query's for the i th row, similarly for columns C i denotes the product of the i th column (I created them lazily with std: : map). Then update the answer according to these, first by rows then by columns. Then update the intersection of the queried rows and columns manually (for example if in one row we multiplied by a and then in one column we multiplied by b then at their intersection we considered this value a + b times but should've considered it ab times), since K is pretty small there won't be many (atmost ), so this way we have a O(K 2) algorithm.
•  » » » 3 years ago, # ^ |   +10 Ah, I think I got too caught up thinking of how to handle the intersections, should've realised that there would only be ~ K 2 of them. Thanks!
•  » » 3 years ago, # ^ | ← Rev. 4 →   +7 I solve this problem as follows:1 Add all values that don't have modifications (blank areas on the image), this step has a time complexity of numbers of queries executed over rows multiply by numbers of queries performed over columns. 2 For each row found in the queries find their corresponding sum and exclude every element that is affected by a column query (red areas on the image). After that add to your answer, this sum multiply by every Y found on the queries that affected the current row. 3 Same as the previous step but by columns.4 Finally, add to your answer the cells marked in red on the image above. Time complexity O( K*K ). Memory complexity O( N ).I hope that this will be useful for you. If you need I can share you my code.
•  » » 3 years ago, # ^ | ← Rev. 6 →   0 My approach was a little diferent that mavd09 and mraron The problem is to solve the next summation for each column: where k i = multiplication of times row i was updated and v i is the value that is in i - th row of each column. C is the multiplication of times that column was updated. But the difference between each column for values ( v i) is 1 between consecutive columns (I mean v j i + 1 = v j + 1 i with j being the j - th column ). So we can rewrite the solution as:Now if you look that carefully, the terms and are always same for each j. So you only need to calculate them once. Call the first sumViKi and second sumKiNow our answer would beIt can be reduced a little bit. But that's enough for me. So now you only need to precalculate both terms sumViKi and sumKi and the rest is just iterate over columns.Complexity( )Code
 » 3 years ago, # |   0 How to solve D (large) and E?
•  » » 3 years ago, # ^ | ← Rev. 4 →   +3 D: dp i, j =  can i-th player win if (i - 1)-st player said number j. dp i, j = true if a i ≠ a i + 1 and dp i + 1, j + 1... j + k has at least one false. a i = a i + 1 and dp i + 1, j + 1... j + k has at least one true. Write array a many times and use some prefix/suffix sums to find that "at least one" thing in instead of .Total complexity: .Code
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 Thank you. I wanted to do the same thing but defined state a little different so I couldn't do bottom up DP.
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 I also did this, but will it definitely pass? There are NM dp states, and K processing at each state, giving us O(NMK), or about 1.25 * 1011 operations. Am I missing something?Edit: I was mistaken, should've paid more attention to the explanation. Thanks for the clarification.
•  » » » » 3 years ago, # ^ |   0 No, process each state is O(1) cause you only need to track the last winning and last losing state.
•  » » » » 3 years ago, # ^ | ← Rev. 4 →   0 if you do transitions in . Check the code.
•  » » 3 years ago, # ^ | ← Rev. 2 →   +3 I think is like dp[i][j] = is turn of i-th animal and the number the previous animal said is j. Now I need to look for a winning or losing state in (i+1)-th animal depending on what kind of animal is animal i and i+1. The first idea of course is to iterate through [j+1 — j+k].But if you see carefully, the state of [i][j] always is looking in [i+1]. So you only need to know what are the position of the last winning and losing state in [i+1]. Then check if last winning <= j+k or last losing <= j+k depending on the case if i and i+1 are same animal, or not.AC code
 » 3 years ago, # |   0 How to solve 140, 160?
•  » » 3 years ago, # ^ | ← Rev. 3 →   +13 EFix the roof i. Let X = H i, then we should find X that minimizes . Let's just simply write it as . We can see this is unimodal, and it attains it's minimum when X =  (median of Y).So, for each roof i, we have to find the median of Y, and calculate . We will do it in O(lg 2(n)) per each i.For the first part, binary search for the answer, and calculate the number of j such that H j - j ≤ X - i and 1 ≤ j ≤ i, plus H j + j ≤ X + i and i + 1 ≤ j ≤ n We can maintain fenwick tree for and and and answer the queries. Second part don't need binary search, and it's simillar to the first part. https://ideone.com/5tyUj0 FI think it uses the technique from Balkan OI 2011 Time Is Money, but I'm not sure whether it will pass TL (at least it runs very fast on random datas). Whatever, that technique is cool, so I highly encourage you to learn (solution for Balkan OI 2011)
•  » » » 3 years ago, # ^ | ← Rev. 2 →   0 How to find maximum/minimum of unimodal function if there are more x s with same value of F(x)?
•  » » » » 3 years ago, # ^ |   0 Nothing changes. It attains its minimum when X = (median of Y)
•  » » » » » 3 years ago, # ^ | ← Rev. 2 →   0 I meant for an arbitrary unimodal function (I know it is so for this one).
•  » » » » » » 3 years ago, # ^ | ← Rev. 2 →   +3 If you are saying about staircase-like functions, then it don't happen as the function is convex. (I regret "unimodal" is not a good word to explain that function)And you can't find a maximum in unimodal function. Let f(x) = (x == 12345678). Then you should find 12345678 somehow but it's impossible
•  » » » » » » » 3 years ago, # ^ |   0 Ok, thank you.
•  » » » 3 years ago, # ^ |   0 You can make lg 2(n) into lg(n) by using a binary search tree to maintain the sequence Y and extract the median for each i, right?
 » 3 years ago, # |   0 For E/140 I sort of did a 2-Dimensional Ternary Search. Not entirely sure both are convex functions. Did anyone else do something like this? Or is there another way to do it>
•  » » 3 years ago, # ^ | ← Rev. 3 →   +3 I doubt that will work. But for fixed i ternary search works.
•  » » » 3 years ago, # ^ |   +10 I generated some random tests in python to check if choosing i is convex and you're right of course, it isn't.
 » 3 years ago, # |   +37 results?
•  » » 3 years ago, # ^ |   +3 Results are out in evaluator!
 » 3 years ago, # |   0 How to solve F?
•  » » 3 years ago, # ^ |   0 My F/CESTE was Dijkstra's algorithm with vertices (v, time), that the time must be increasing and cost decreasing on each v. It was too slow for O(NM * MaxTime) vertices in the worst case, but luckily passed.
•  » » 3 years ago, # ^ |   +10 We know that there exists x (0 <= x <= 1) for which the shortest path with weights time * x + cost * (1 — x) gives the optimal answer for time * cost.Great, then let's find shortest paths with x = 0. Then, let’s find minimum x < x’ <= 1 such that there is an edge (u, v) such that with weights time * x’ + cost * (1 — x’) Path ((1->u) (optimal for x) -> (u, v)) is better than (1->v) (optimal for x).And rebuild shortest paths with x = x’.We will do this process while we can find such x’.(We can find this x’, just solve some equation a * x + b * (1 — x) < c * x + d * (1 — x) for each edge).
•  » » » 3 years ago, # ^ | ← Rev. 3 →   -8 I think I might have understood what you just said. However, I'm far from being able to prove any of the facts you used. First, let me make sure I understood it the right way: for a fixed x, you compute the optimal path from 1 to all the other vertices, where the weight of a path is defined as time * x + cost * (1 — x) (basically x is a constant that helps in defining the weight of an edge). Now, my problem is: how do you know that there exists a proper choice of x so that the optimal path that you get in that case is also optimal when considering the weight as time * cost? It makes sense intuitively but I can't think of any attempt to prove it. Then, you also count on the fact that you can somehow make leaps that take you to a better answer while increasing the x. This seems even harder to prove, so if you have any idea how to prove any of these facts, I'd be grateful to see them. Aand my last question would be: this is a really strange approach, maybe even more interesting than Alien's trick (which I heard is actually Lagrange multiplier), so I was wondering whether you just came up with it, or you saw something somewhat similar to it in some other problem. If so, could you share that problem with us? Thanks in advance!To the dowvnoters: Really?:)) It's so funny to see the -7 when my comment also explains part of the solution that some people might not have understood and definitely helped at least those. CF community doesn't cease to impress me