awoo's blog

By awoo, history, 11 days 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
  • +89
  • Vote: I do not like it

»
11 days 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

»
10 days 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?

  • »
    »
    10 days 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.

»
10 days 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.com/contest/1519/submission/114709341

Edit: the issue is resolved thanks

  • »
    »
    10 days 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.

»
10 days 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.

  • »
    »
    10 days 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.

»
10 days ago, # |
  Vote: I like it +27 Vote: I do not like it

Thanks for E, it was quite educational.

»
10 days 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

  • »
    »
    10 days 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.

    • »
      »
      »
      10 days 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?

      • »
        »
        »
        »
        10 days 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).

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Very interesting contest, I really enjoyed it! Thanks!

»
10 days 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

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Will the rating roll back QAQ

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

can anyone show me simple readable solution for C?

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please give me a proof for B?

  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You can also use BFS

    • »
      »
      »
      10 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sadly haven't got to covering this yet, trying to solve most of the A problems and B problems from Div2 90% before I go on to covering DP, DFS and BFS problems. If you could tell me a way to prove this without assuming the formula, that would be amazing.

      • »
        »
        »
        »
        9 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        No one directly "assumed" the formula in my opinion how I approached it was trying to convert my intuition to mathematical proof. Try reading this article. Have a good day!

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Try to draw a 2d matrix and try to compute the value at random points from taking both ways left and right then u will come up with the same formula

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

the round was amazing, can anyone help me identify the error in 114652844 to problem c, my approach is similar to the edi.

»
10 days 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

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

    They're actually the same

  • »
    »
    8 days 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??

    • »
      »
      »
      8 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

hi! can some one point out my mistake in D. I have used the same approach as in editorail, except i have used a 2-D dp. test case 27 is showing wrong answer. https://codeforces.com/contest/1519/submission/114798610

thanks

»
9 days 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.

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    when you are declaring:

    vll pre(n+1),

    i dont why but on my system it fills it with garbage value, and run time is really slow on my pc as you have stated

    but,

    when u use ll pre[n+1] ,

    pre is filled with all 0s, and run time is fast on my pc

  • »
    »
    9 days 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.

»
9 days 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.com/contest/1519/submission/114826178)

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey, I tried your approach as well, calculating answer for each value of k[1,n] , i got TLE as well, link similar to your solution(TLE): https://codeforces.com/contest/1519/submission/114824539

    Nevertheless, I got ACC , finally. Here's some tips, i can give you:

    1.) use vector<vector> instead of map<int,vector> db or use unordered_map

    2.) use a vector to store output of each value of k[1,n]

    3.) after sorting the inner vector, use the same inner vector to store cumulative sum

    3.) iterate this inner vector after sorting and increment the value of output-vector according to this inner vector

    my Accepted solution(not the most efficient): https://codeforces.com/contest/1519/submission/114825135

  • »
    »
    9 days 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.com/contest/1519/submission/114635321

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

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can Any one help me in Problem D, i used BIT to calculate the prefix and suffix value and used Brute force for calculating the interval [i,j] , but still TLE at test case 9:

my solution: https://codeforces.com/contest/1519/submission/114828602

»
9 days 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.

  • »
    »
    9 days 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.

»
8 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone Explain D a little bit in detail

»
8 days 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?

»
8 days ago, # |
  Vote: I like it 0 Vote: I do not like it

I believe I tried to solve with the same approach as explained in the editorial for C . i.e grouping the students according to university_id and then taking the prefix sum for individual university . however I kept getting TLE on test 4 . can anyone tell why ? 114624170

  • »
    »
    8 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have got the same issue .. try to sort the vector of vector in terms of size of v[i].size in greater(). Uh can check out my code too .. i can explain that

»
8 days 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?

  • »
    »
    7 days 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.

»
8 days 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

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

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

»
7 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone tell me why this code is giving me tle :( but after reversing the conditions of last for loop it got accepted......Thanks in Advance :)

#include<bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; while(t--){ int n; cin >> n; vector uni(n); vector score(n); vector<vector> combine(n,vector()); for(int i=0;i<n;i++){ cin >> uni[i]; } for(int i=0;i<n;i++){ cin >> score[i]; }

for(int i=0;i<n;i++){
            combine[uni[i]-1].push_back(score[i]);
        }

        for(int i=0;i<n;i++){
            sort(combine[i].begin(),combine[i].end(),greater<int>());
        }

        for(int i=0;i<n;i++){
            for(int j=1;j<combine[i].size();j++){
                combine[i][j] = combine[i][j]+combine[i][j-1];
            }
        }
        //Tle bcz of this
        for(int i=1;i<=n;i++){
            long long int ans = 0;
            for(int j=0;j<n;j++){
                int x = combine[j].size()/i;
                if(x>0)
                ans+= combine[j][x*i-1];
            }
            cout << ans << " ";
        }
        //But after making this change All Ok :)
       vector<long long int> ans(n,0);
                for(int i=0;i<n;i++){
                    for(int j=1;j<=combine[i].size();j++){
                        int x = combine[i].size()/j;
                        ans[j-1]+=combine[i][x*j-1];
                    }

                }

                for(int i=0;i<n;i++){
                    cout << ans[i] << " ";
                }

        cout << "\n";
    }
}
»
7 days ago, # |
  Vote: I like it 0 Vote: I do not like it

115108145 Please help! Why am I getting TLE ?

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

115150110 Any of you guys have any idea why is this code failing for Test case 11 in Question D. Thanks in advance!

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem D (Maximum Sum of Products) , Is there an algorithm with Time Complexity less than O(n^2) ? Like O(n) or O(nlogn) ?? Further thanks..

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

I can't find the problem in my solution for problem D (wrong answer test 9) 115467785, help please

»
14 hours 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))?