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

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

1519A - Красные и синие мармеладки

Идея: adedalic

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

1519B - Торт - это ложь

Идея: adedalic

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

1519C - Берляндский отбор

Идея: BledDest

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

1519D - Максимальная сумма произведений

Идея: vovuh

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

1519E - Сдвиг на один

Идея: BledDest

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

1519F - Сундуки и ключи

Идея: BledDest

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

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

Good contest, I really enjoyed it! Thanks.

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

Here is a problem which has the same idea with D. http://acm.hdu.edu.cn/showproblem.php?pid=6103

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

Anyone knows why there's a major difference between predicted and actual rating changes?

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

Can anyone help me in identifying the mistake in Problem D, the approach is similar to the longest palindromic substring. It is failing in the 11th test case.

Link to the submission: https://codeforces.me/contest/1519/submission/114709341

Edit: the issue is resolved thanks

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

    Use vector<ll> for a and b, because the when you multiply two int's, the compiler doesn't know to convert it to a long long unless you explicitly tell it to do so, or have the types originally as long long's.

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

Aren't problems like E and F suitable better for div1? Why not use them in div1 then as creating div1 problems is harder.

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

Thanks for E, it was quite educational.

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

Can anyone explain how the time complexity of C is nlogn

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

Спасибо за очередной интересный контест! С нетерпением жду ещё!

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

can anyone explain how forward edges are handled in last paragraph of editorial of E?

upd: got it

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

Will the rating roll back QAQ

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

can anyone show me simple readable solution for C?

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

I was learning FFT recently (only out of curiosity) and been wondering if D can be solved by it? I don't need the solution, I just want to know if it's possible.

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

Short Video Editorial For Problems A — D

I have a different solution for problem D.
We can just calculate the sum if we flip a subarray $$$[j, i]$$$ for all subarrays.

$$$dp[i][j] = dp[i - 1][j + 1] + A[i] * B[j] + A[j] * B[i]$$$

We are basically swapping $$$A[i]$$$ and $$$A[j]$$$ assuming that the subarray to be reversed is $$$[j, i]$$$.

After this we can loop over all subarrays and add the prefix and suffix profit using prefix sums. We take the max value.

See my code for clarity: 114583311

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

Short Video Editorial For Problems A — D

I have a different solution for problem D (Maximum Sum of Products):

$$$sum[i][j]$$$ stores the new sum on reversing the subarray $$$[j, i]$$$
$$$sum[i][j] = sum[i - 1][j + 1] + A[i] * B[j] + A[j] * B[i]$$$

We calculate the sum of elements we get on reversing every subarray $$$[j, i]$$$.

To account for the rest of the array, loop over all subarrays and use prefix sums to add the remaining part. Take the best value over all subarrays.

Time complexity: $$$O(N^2)$$$

See my code for clarity: 114583311

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

Hi, for problem C, can somebody explain why 114796728 gives TLE and 114799663 doesn't. The only difference is the usage of an array instead of a vector.

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

    Your code is fundamentally O($$$n^2$$$) because of

    f(i,1,n+1) {
      vll pre(n+1);
    

    As far as I can tell, the only reason why you didn't TLE on your 2nd submission is because the compiler was nice and effectively optimized your code to

    vll pre(n+1);
    f(i,1,n+1) {
    

    Some other things I noticed about your code:

    1. Remove cout.tie(NULL) from your code. Unlike cin.tie(NULL); the cout version does nothing and should never be used.

    2. Submit under C++17(64 bit) instead of C++11. You will see much better running times. You are basically handicapping yourself by using C++11.

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

Can anyone help me figure out why I am getting TLE (https://codeforces.me/contest/1519/submission/114826178)

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

    Your code failed only because you used iterator as the second loop. The same reason my code failed and changing it will surely give AC. I also changed map to vector first and all the other optimizations and still it failed but this one small change gave AC. Reason why this happens is because when n >= 10^5, iterator being nested will be called in range of 10^5 times(depending on implementation). At this range it becomes really slow compared to normal for loop with [] operator. In my tests above 10^5 it was almost 1.2 — 2 times slower in this question(Can be more/less as I did these tests on my own PC but you can get the idea).

    Check the running time in both these codes where the only difference is for loop instead of for-each(uses iterator)

    Failed Code — https://codeforces.me/contest/1519/submission/114635321

    Accepted Code — https://codeforces.me/contest/1519/submission/114687960

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

for problem D, if I try to implement the brute force approach which would take O(n^3) time then will it be TLE ? As the constraint is n <= 5000.

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

What does if(nw.need == vector<int>(a, a + n)) in F's solution mean?

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

Please explain the proof for problem B little more briefly.. Why doesn't the cost depend on path taken?

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

    Consider the two paths shown in the picture below: one which takes the blue route and one which takes the red route. The black parts of the two paths are the same in both paths.

    Either way, the contribution from the red section or the blue section is $$$+(i+j)$$$, so both paths have equal cost.

    You can change from any path to any other path via a sequence of paths which each differ only by one square, like in the picture above.

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

Can anyone help me out in c question i had also used prefix sum but getting tle on 4 th case but it should not come 115057482

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

I believe there is a typo in the editorial for F: instead of $$$\sum_{i=1}^{a_i} - mincut$$$ it should be $$$\big(\sum_{i=1}^n a_i\big) -mincut$$$.

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

$$$2$$$ more proofs for problem $$$B$$$:

Mathematical proof:

Assume that the sequence of movement is as follows: $$$x_1$$$ units down, then $$$y_1$$$ units right, then $$$x_2$$$ units down, then $$$y_2$$$ units right, ..., then $$$x_c$$$ units down, then finally $$$y_c$$$ units right, where $$$x_i, y_i \geq 0$$$, $$$1+\sum_{i=1}^{c}x_i=n$$$, and $$$1+\sum_{i=1}^{c}y_i=m$$$. The total cost will be:

$$$x_1*1+y_1*(1+x_1)+x_2*(1+y_1)+y_2*(1+x_1+x_2)+...+(1+\sum_{i=1}^{c-1}y_i)*x_c+(1+\sum_{i=1}^{c}x_i)*y_c=$$$

$$$\sum_{i=1}^{c}y_i+\sum_{i=1}^{c}x_i*(1+\sum_{i=1}^{c}y_i)=m-1+(n-1)*m=n*m-1$$$

Ad hoc proof:

A step from $$$(i,j)$$$ to $$$(i+1,j)$$$ spans all the cells from $$$(i+1,1)$$$ to $$$(i+1,j)$$$, and a step from $$$(i,j)$$$ to $$$(i,j+1)$$$ spans all the cells from $$$(1,j+1)$$$ to $$$(i,j+1)$$$. Which means that for every step down to a cell $$$x$$$, all the cells from $$$x$$$ to the left will be counted, and for every step right to a cell $$$y$$$, all the cells from $$$y$$$ to the top will be counted. At the end, all the cells in the grid will be counted except the first cell $$$(1,1)$$$, that is $$$n*m-1$$$.

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

Can we do D with two segment trees s1 and s2, where s1 stores the sum of a[i]*b[i] over [l,r) and the s2 stores the similar sum but for the reversed array a over ranges [l,r), Afterward the ans can be brute forced by taking each possible segment that can be reversed and taking the maximum. The runtime will be O(n*n*log(n))?

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

Can anynone help me with my A.cpp code ? Why is it wrong :(( ?? https://codeforces.me/contest/1519/submission/115942467

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

can anyone help me with my A.cpp code ? Why is it wrong :(( https://codeforces.me/contest/1519/submission/115942467

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

Hi awoo

For problem F, using the idea of saturating all out going edges from source to chests, I used this dynamic programming state : dp[i][j][a][b][c][d][e][f] = the minimal cost to saturate the first i out going flow edges from source, where the i-th out going edge currently has j units of residual remaining, and the 1st incoming edge to sink has a units of residual left , second incoming edge to sink has b units left , 3rd has c units left, 4th, 5th, 6th have d , e , f units left respectively.

base case: dp[0][0][a][b][c][d][e][f] = 0 , for all a,b,c,d,e,f <= 6
transition: dp[i][j][a][b][c][d][e][f] = min{
dp[i][j - min(j , a)][a - min(j , a)][b][c][d][e][f] + C[i][1],

dp[i][j - min(j , b)][a][b - min(j , b)][c][d][e][f] + C[i][2],

dp[i][j - min(j , c)][a][b][c - min(j , c)][d][e][f] + C[i][3],

dp[i][j - min(j , d)][a][b][c][d - min(j , d)][e][f] + C[i][4],

dp[i][j - min(j , e)][a][b][c][d][e - min(j , e)][f] + C[i][5],

dp[i][j - min(j , f)][a][b][c][d][e][f - min(j , f)] + C[i][6],

}

Here is my submission https://codeforces.me/contest/1519/submission/116235067 I got wrong answer on test case 93, I have been trying to find the bug for a day and could not resolve it.

You will be my life saver if you hint me on why my solution did not get AC!

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

This is my solution.

This is an accepted solution.

Can someone, please tell me why my solution is not being accepted? The time complexity of both the solutions seems same to me.

Help me out ;( here.

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

Even though I have used the exact same approach as given in editorial my solution is showing TLE for some cases. Pls can anyone help me out. I have used std :: unordered_map for the universities. My solution is 131142379

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

Even though I have used the exact same approach as given in editorial for Problem : 1519C — Berland Regional, my solution is giving TLE for some cases. I have used unordered_map for the universities. Pls can anyone help me out. My solution is 131142379

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

Can anyone tell me why this java implementation code with hashmap will get TLE for Problem C?

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

code In Problem D why it is Accepted i thought i will get TLE

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

366086518 For people getting TLE, the approach over looping first over all $$$k$$$ 's and then the prefix array inside does not work, due to caching(from AI and other sources), this has to do with accessing the entire prefix array in each iteration v/s the given approach where you loop over array in the prefix array only once, which results in much faster code. Adding as this problem is in TLE Eliminators sheet.