Блог пользователя awoo

Автор awoo, история, 6 лет назад, По-русски

1359A - Berland Poker

Идея: BledDest

Разбор
Решение 1 (BledDest)
Решение 2 (BledDest)

1359B - New Theatre Square

Идея: BledDest

Разбор
Решение (pikmike)

1359C - Mixing Water

Идея: adedalic

Разбор
Решение (pikmike)

1359D - Yet Another Yet Another Task

Идея: BledDest

Разбор
Решение (pikmike)

1359E - Modular Stability

Идея: BledDest

Разбор
Решение (BledDest)

1359F - RC Kaboom Show

Идея: BledDest

Разбор
Решение (pikmike)
  • Проголосовать: нравится
  • +131
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

How to solve problem D if the value of the array is much bigger ?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Refer this thread.

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +45 Проголосовать: не нравится

    You can precompute for each element of the array, the segment that he is the maximum element, this can be done with a stack, finding the rightmost element which is greater than ai and its from 1 to i-1 for each i and then do the dame for the right, then in this interval, for each you need to find the smallest prefix sum un the range from ci to i (here ci is the rightmost element in the left which is greater than ai) and the maximum prefix sum from i to di (here di is the leftmost element in the right which is greater than ai), and the optimal range which its maximum is ai will be this range, the maximum and minimum prefix sums can be computed with segment tree in nlogn. Sorry for my poor english.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +7 Проголосовать: не нравится

    Well, maybe I can try. Let's fix an index i. We need find the minimum left index L and maximum right index R such that left maximum number in the range [L, R] be a[i]. How can we do this? Use binary search on index with range maximum query. Complexity for this would be O(n (log n)^2).
    Then, at the particular index l in [L, i] and r in [i, R], such that the ans for the fixed i be maximum. Now, notice the following observation. l would be the index in [L, i] such that suffix sum till index l is maximum. Similarly, r would be in the range [i, R] such that prefix sum till r be the maximum. Now, we have to do range maximum query on prefix sum and suffix sum. Simple. Use segment tree once again. Complexity would be O(n log n).
    Now, after we find the index l and r, now construct the ans. ans would be suff[l] — suff[i] + pref[r] — pref[i], it's easy to find out. So, it's given on yourself.
    Now, we would do this for every i from 1 to n and update the answer.

    Yeah bro, that's it. Final complexity is O(n (log n)^2).

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I calculated prefix sum as well as suffix sum. Since array was immutable, so I created sparse table for array,prefix array and suffix array.

    Then for each Index i, find the maximum subarray range that can be formed by applying binary search on sparse table of given array. After that find the maximum suffix sum in left part and maximum prefix sum in right side of that index. If the sum are positive add them.

    Here is my solution for the above approach Solution

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +2 Проголосовать: не нравится

    Let's find for some position "pos" L, R that in the interval from L to R the value in position pos is the largest, then you just need to find the maximum from pos to R and the minimum from L to pos. Now the answer can be MAX (pos, R) — MIN (L, pos) — a [pos], and therefore we iterate over all positions and perform these operations at each position

    Давай найдем для какой то позиции "pos" L, R, что в интервале от L до R значение в позиции pos является наибольшим, тогда вам просто нужно найти максимум от pos до R и минимум от L до pos. Теперь ответ может быть MAX (pos, R) — MIN (L, pos) — a [pos], и поэтому мы перебираем все позиции и выполняем эти операции на каждой позиции

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Nice problems, waiting for F editorial, thanks for the contest :)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

It would have been helpful if you could post the editorial a bit faster. Not complaining, just pointing out. I was waiting all day for the solutions, and the few contests i gave had fast editorials. So this might be normal; just saying

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

For problem D, what is the purpose of the best variable? I don't see a correspondence between it and any variables in Kandane's (https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane's_algorithm)

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +10 Проголосовать: не нравится

    Best variable stores the minimum prefix sum that we have seen so far. In Kadane, at index i, the question is what is the maximum sum subarray that ends at index i. In other words, we want to find the maximum value of cur-pref where cur is the total sum of [0,i] and pref is some prefix sum of elements from [0,j] where j<i. So we want the minimum prefix sum we have seen so far in order to obtain the maximum value for cur-pref which is in our case cur-best. Then we also subtract mx as it will be removed.

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +3 Проголосовать: не нравится

      Great explanation! It's really cool how the Kadane's algo can be rephrased using the prefix sum method you described. I only knew of the max / min way described on Wikipedia before :)

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Sir, I kindly request you to explain why are we taking this infinity to replace a[i] if it is greater than max?

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        We are taking MINUS infinity. One other way of doing it is to simply consider only those intervals with all elements <= mx and ignore the ones that are greater. Then simply run Kadane on each of those intervals independently and update the answer. Setting values >mx to negative infinity has the same effect as Kadane will automatically never take those elements in a subarray (as they have minus infinity value) and this makes the code quite a bit easier to write.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +37 Проголосовать: не нравится

A note for my not so experienced companions-

In the editorial of problem E, if someone else is also having trouble understanding why the following is true,

( x mod a ) mod ( b a ) = ( x mod ( b a ) ) mod a = x mod a

Then here's why,

For ( x mod a ) mod ( b a ) = x mod a

We know that x mod a will already give us something smaller than ba so that's why mod (ba) will have no effect on it.

For ( x mod ( b a ) ) mod a = x mod a

When we do x mod (ba) we end up with something of the form x — k*(ba) (which is by definition of modulo). So we have subtracted a from x, k*b times already but the remaining x may still be big enough for more a's to be subtracted. That happens when we take its mod with a and we are ultimately left with the answer which we would have got if we had just done x mod a

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    other way to prove Other way to prove (x mod (ba)) mod a = x mod a since x mod (ba) = x - k*(ba) where k is some integer we can apply mod aon both sides

    `(x mod (ba)) mod a = (x-k*(ba) mod a`
    `(x mod (ba)) mod a = (x mod a - (k*b*a mod a)) mod a`
    `(x mod (ba)) mod a = (x mod a - 0) mod a`
    `(x mod (ba)) mod a = x mod a`
    
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

How to solve Problem C with binary search..as mentioned in above..

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

That allows us to find such k, that the value of tk is exactly t. However, such k might not be integer. (k+1)⋅h+c/2k+1 forgot to multiply k with c

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -7 Проголосовать: не нравится

Task C took soul out of me but never showed Accepted

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    Try tracing an example. let's say h=10 c=5, try taking 1,2,3 and so on. if we take 1, the average would be 10, taking 2 the average would be 7.5, taking 3 the average would be 8.333 , taking 4 the average would be 7.5, notice that if you take any even number, the average would be the same. so in this example if 7.5 is the closest to t, we would surely take 2 because it is the minimum even number. Going on taking odd numbers you will notice that the average goes down towards 7.5 but never below it. That means that when you take larger odd numbers you would converge to the average when taking any even number(all gives the same average). Now as the average is always going down as we are taking larger odd numbers we can use binary search to know which is the optimal odd number that we should take to make the average as close as possible to t. Supposing that t is less than or equal to the average when taking an even number, the answer would be 2 because when taking odd numbers, the average would never cross the even average and if we assume that the average when taking an odd large number can ever reach the even average. we would still pick 2 becuase it is smaller than that odd number. so if t<=(h+c)/2.0 "the even average" , the answer would be 2 , else we know that t is greater than the even average. we also know that as the odd number goes to infinity the odd average converges to the even average, so if t is greater than the even average , we are sure that there exists an odd number that its average is as close as possible to t, so we do a binary search considering only odd numbers and calculate the average for that mid then checking if average>t then we want a smaller number so we discard the left half else we want a larger number so we discard the right half. Going on, we would get the number that gives the closest average possible to t. if anything is still not clear, feel free to ask.``

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    I don't see a single submission from your account for problem C. Please can you explain how did it took the soul out of you?

    If i am not wrong you must be the kind of guy who creates new id just for the sake of posting comments.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    check this video for detial explanation of problem c

    https://youtu.be/Ts6to-_N-Y0

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

IT confused me why there were so few correct submissions for F — the time limit is very generous and you only have N^2/2, which is about 3e8 pairs to check if you brute force. So brute force should easily pass.

Unfortunately I didn't debug in time during the contest itself...

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone explain Problem A in a more beginner- friendly and easy way. or else tell me where i have gone wrong.
MY SUBMISSION: https://mirror.codeforces.com/contest/1359/submission/81751748 code is in C++. thank you!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

I have a challenge for E

Solve it without having a % operator!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If anyone need detail explanation for C Here

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

How to solve D in $$$O(Nlog(N))$$$? Any D&C approach?
Edit: D&C $$$\implies$$$ Divide-and-conquer

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится -7 Проголосовать: не нравится

    check this video editorials for c and d C : https://youtu.be/Ts6to-_N-Y0 D : https://youtu.be/1h1D7wMbDis

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    I will share the approach of one of my friend 81836011.

    If we need to find the answer between L and R index, then we can do the following

    Let i be the index of any maximum element in range L to R. There are three possiblities

    1. The optimal subarray lies from L to i - 1
    2. The optimal subarray lies from i + 1 to R
    3. It include element at index i and therfore maximum of this subarray will be element at index i.

    Now first two cases are recursion and for the third you need to find the maximum prefix sum for range i + 1 to R and maximum suffix sum for range L to i - 1. So these queries (maximum index, maximum suffix sum, maximum prefix sum) can be done by segment tree. The merging step will take O(logN) time and therefore even though our problem doesn't always reduce into two equal halves, we will achieve a time complexity of O(NlogN).

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      I really like the idea, can you please help me to understand what build2 and build3 stands for in the code.

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
         
        Проголосовать: нравится +1 Проголосовать: не нравится
        1. build1 is used for calculating the index of the maximum element in the range
        2. build2 is used for calculating maximum prefix sum for a range
        3. build3 is used for maximum suffix sum for a range

        For the understanding of build2, you can see that when you merge two nodes either the max prefix will be one of that of max prefix sum of the left node or it could be taking full left node and taking max prefix sum for the right node. The segment tree is storing a pair in case of build2 where first of that pair is max prefix and second is sum of that range. A similar idea could be applied to build3 as well. You can refer the following link

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      This code has greatly helped me solidify my concepts on suffix sums, thanks a lot for sharing this code

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help with my approach? I am getting Wa but couldn't find why? My submission

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

take test case 999977 17 499998 answer is 499981 but i think it should be 499979

please explain why?

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem E, should'nt it be (d-1)P(k-1) instead of (d-1)C(k-1), because the problem asks only to reorder the elements?

For example if the minimum element is 1, and the k=3, then {1,2,3} and {1,3,2} are both stable arrays but we are counting only one in the answer. What am i getting wrong?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    You haven't got the question right but don't worry.

    The question asks for how many different arrays are stable where a stable array is one that gives the same remainder irrespective of the permutation of the array.

    For instance, {1,2,3} and {1,3,2} are not two different stable arrays but two permutations of the array {1,2,3}. Notice how in the question it is mentioned that we have to find the arrays such that 1 <= a1 < a2 < a3 <...<an <= n. This is a strictly increasing array.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Am not able to completely understand Editorial of Problem D

CAN SOMEONE PLEASE WRITE AN ELABORATE EXPLANATION FOR IT.

Thanks in Advance.

Edit : No Longer needed , i got the idea from code in this video:https://www.youtube.com/watch?v=0WNladOR-XM

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

in question c for testcase 999977 17 499998 answer should be 499979 because for number of cups 499979 and 499981 the temprature of barrel are 499998.0000020001 and 499997.9999979999 respectively so as mention in question answer should be 499979.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In D can't we just use Kadane's algorithm to find the maximum sub-array and then find the maximum number in that array and just subtract if from sum of the maximum array?

TC: 11 3 0 1 -2 5 -5 -1 0 3 2 2 My ans: 3 expected: 4 y is it 4? total is 8 and max number is 5 so ans should be 8-5=3

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For their solution in problem E, is there a reason they had to write an add function? Would simply adding and then using % not work?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

1379D - New Passenger Trams

You can see that for any value between y and mx the maximum sum segment will always be that chosen one.

Can Someone explain this?

`

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

Ставь класс если придумал 7 уникальных решений задачи D (включая divide-and-conquer) и среди них нет элементарного авторского

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I understood the problem C wrongly, I don't know if I was the only one lol, I thought that after pouring a cup you wait for the temperature to stabilize and AFTER that you pour the next cup, which will change the equation.. because if we note t_k the temperature after the k-th pour then for even k we have t_k=(t_(k-1)+c)/2 and for odd k we have t_k=(t_(k-1)+h)/2...

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится -8 Проголосовать: не нравится

    I see where your confusion comes from, but the problem clearly stated that:

    "The water temperature in the barrel is an average of the temperatures of the poured cups."

    I recommend that if you're not getting the correct answer in the test cases you re-read the problem (or the notes). This should've solved your doubt.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I didn't understand the last statement in PROBLEM E

On the other hand, suppose there exists an element ai such that it is not divisible by a1. Let's take x=ai and two following reorders of the array a: [a1,a2,…,ak] and [ai,a1,a2,…,ai−1,ai+1,…,ak]. For the first array, we get xmoda1=aimoda1, which is non-zero; and for the second array, aimodai=0, so the result is zero.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

for problem D : lets say the current value of mx is 5 and we dont have any 5 in our array how can current answer for this sub task be sum of largest segment — 5 ??