awoo's blog

By awoo, history, 4 years ago, translation, In English

1519A - Red and Blue Beans

Idea: adedalic

Tutorial
Solution (adedalic)

1519B - The Cake Is a Lie

Idea: adedalic

Tutorial
Solution (adedalic)

1519C - Berland Regional

Idea: BledDest

Tutorial
Solution (awoo)

1519D - Maximum Sum of Products

Idea: vovuh

Tutorial
Solution (Neon)

1519E - Off by One

Idea: BledDest

Tutorial
Solution (awoo)

1519F - Chests and Keys

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +92
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

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

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    At least in my case the predictor was using my rating from 2 contests prior. I think it must have scraped the ratings at a time when ratings changes were rolled back.

»
4 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

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.net/contest/1519/submission/114709341

Edit: the issue is resolved thanks

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +14 Vote: I do not like it

    Can't comment about F but problems like E are repeat or too obvious for Div1 contenders.

»
4 years ago, # |
  Vote: I like it +27 Vote: I do not like it

Thanks for E, it was quite educational.

»
4 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Can anyone explain how the time complexity of C is nlogn

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The time complexity is nlogn because you must to sort the elements of each university before the calculation of the prefixes.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      But for each k from 1..n we calculate the result, which requires to go through all schools from 1..n. Doesn't it give n^2?

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        On each university we iterate between the values 1 and the number of students in that university, because for higher values the university can not make a team. This has a complexity of O(n).

»
4 years ago, # |
Rev. 3   Vote: I like it +3 Vote: I do not like it

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

upd: got it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Will the rating roll back QAQ

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can anyone show me simple readable solution for C?

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    They're actually the same

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    why did you take max(dp[i][j], dp[i — 1][j + 1] + A[i] * B[j] + A[j] * B[i]), what's the use of taking maximum here??

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yeah, you're right. There's no use of doing that here.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
    Rev. 6   Vote: I like it 0 Vote: I do not like it

    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.net/contest/1519/submission/114635321

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

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yes it would TLE as (5000)^3 = 125*10^9=1.25*10^11 As this is much greater than the recommended 10^9 it would not fit within the time limit and give a TLE error.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    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.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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$$$.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

$$$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$$$.

  • »
    »
    17 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is the only rigorous proof about B I've ever seen!!!

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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))?

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

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.net/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!

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
22 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it