BledDest's blog

By BledDest, 16 months ago, translation, In English

2051A - Preparing for the Olympiad

Idea: BledDest

Tutorial
Solution (Neon)

2051B - Journey

Idea: fcspartakm

Tutorial
Solution (BledDest)

2051C - Preparing for the Exam

Idea: BledDest

Tutorial
Solution (BledDest)

2051D - Counting Pairs

Idea: fcspartakm

Tutorial
Solution (BledDest)

2051E - Best Price

Idea: BledDest

Tutorial
Solution (Neon)

2051F - Joker

Idea: BledDest

Tutorial
Solution (Neon)

2051G - Snakes

Idea: BledDest

Tutorial
Solution (adedalic)
  • Vote: I like it
  • +64
  • Vote: I do not like it

| Write comment?
»
16 months ago, hide # |
 
Vote: I like it +12 Vote: I do not like it

If tutorials for some problems aren't loading, they should be up in about 3-4 minutes.

  • »
    »
    16 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    The E problem is really well designed. Initially, my understanding of the scanline algorithm was focused on processing points within a two-dimensional range. Therefore, when analyzing the E problem, I enumerated all the values of $$$~a_i~$$$ and $$$~b_i~$$$ and treated them as prices, which we denote as val. At this point, the number of buyers among those who would not leave negative reviews is the count of $$$~a_i~$$$ that are greater than or equal to val. The number of buyers who would leave negative reviews is the count of $$$~b_i~$$$ that are greater than or equal to val, under the condition that $$$~a_i~$$$ for that $$$~b_i~$$$ is greater than val. Thus, I viewed this problem as counting points within a two-dimensional range, applying a scanline approach combined with offline processing using a Fenwick tree. However, the complexity of this approach prevented me from implementing it during the competition. After the contest, I was able to see a similar application of the scanline thinking, and the profound understanding of it truly opened my eyes. This also led me to reflect on the essence of the scanline preprocessing, which is to update all necessary states in a legal time complexity according to a certain order. It made me reconsider that in my code design, I should focus on specific implementation steps rather than just thinking about which algorithm to apply. This experience has been very educational and helpful for me! I really appreciate the design of the problems you created!

»
16 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

For problem D, I felt so pathetic, if I would have realised earlier that there is no distinction between index i and j. I have solved same kind of question earlier, I would have solved it in contest as well as:(

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

E, input:

3 1
2 9 5
12 14 9

Why the answer is not 18 (2 * 9)?

1st customer: 9 <= b (negative review), 2nd customer: 9 <= a (positive review).

»
16 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

In E, a third method to optimize would be using PBDS, 297960644

»
16 months ago, hide # |
Rev. 4  
Vote: I like it 0 Vote: I do not like it

Hi is there anyone able to solve G with python? My TSP seems too slow (currently at 7000ms). Also, it there any reason for the strict contraints, making it just a library check as even some C++ solutions are TLEing?

UPD: Got it AC

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

E is simple binary search on answer right? Or am I missing something?

  • »
    »
    16 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    Binary search wont work because total earning w.r.t cost of the tree is not monotonus or unimodal.

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

      Why is it not unimodal?

      • »
        »
        »
        »
        16 months ago, hide # ^ |
        Rev. 2  
        Vote: I like it 0 Vote: I do not like it

        you can not be sure that if p is not a valid price then p+1 will also not be valid. Consider an example a = [6, 12, 12, 12] and b = [10, 20, 30, 40], and k = 0. Here the valid interval for a price could be [1, 6] but once the prices reaches 7, we will forced to buy the first tree, giving one negative review which is not allowed. The same will be the situation for 8, 9 and 10. But, once the price reaches 11 we can buy the remaining three trees with zero negative reviews. and beyond 12 we can again not buy anything. so the earnings increase in the interval [1, 6] then go to 0 in the range [7,10], peaking at 12 and again going to zero at beyond that. This behavior is not monotonic

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

          Can you help me find the bug in my approach 340085771 ? I tried finding highest possible price within the negative review limit by binary search (don't care about the profit now). Once I have that (let's call it highest_possible_price, I tried finding profit in 2 ways.

          • trying all possible a[i] and b[i] prices that are less than highest_possible_price and take the highest profit
          • instead of going over these prices linearly, I tried a ternary search as well. But I commented it out later since it's failing even on linear search so something is probably wrong at finding highest_possible_price.

          I'm really clueless even though I was pretty confident in my mind that it would work. Getting WA on 40th case of test 2 so can't check myself.

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

Why are O(nlogn) solutions getting hacked in E ?

  • »
    »
    16 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    Maybe they get TLE? My O(nlogn) c++ solution barely fits in TL. 297985458

    • »
      »
      »
      16 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it
      try this
»
16 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

why the testcases of the problems are opened?

»
16 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it
»
16 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

https://mirror.codeforces.com/contest/2051/submission/297939657

in e problem i have done binary search on binary search , can you tell me why this will not work , basically what i did is first i did bs on profit , now to check if we can get profit x or not i have done binary search on price that what price we can keep so that it can give desired profit ??

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

For D: if two pointers do not solve problem, use 3 297969402.

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

Can Anyone give me hint for problem E? just hint for from where to start thinking process

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

anyone got AC following the alternate approach in E? im following a very similar approach but it fails my approach please help me out

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

Why my brute force solution doesn't work for D (I skipped C)? 297948999. It doesn't give TLE but it gives Wrong Answer on Test 3.

  • »
    »
    16 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    You wrote "ll totalSum = accumulate(a.begin(), a.end(), 0);" Instead you should write "ll totalSum = accumulate(a.begin(), a.end(), 0LL);" so the calculation takes place in long long type

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

      What about my approach? Are there any flaws?

      • »
        »
        »
        »
        16 months ago, hide # ^ |
         
        Vote: I like it +1 Vote: I do not like it

        Time complexity is O(n^2)

      • »
        »
        »
        »
        16 months ago, hide # ^ |
         
        Vote: I like it +1 Vote: I do not like it

        if you sort the array first, u can use binary search instead of using running nested for loops.

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

          How I mean I looked at the editorial but I didn't make sense of it? We need to find the number of pairs 'i, j' such that if we remove i and j the sum of all elements is at least x or at most y. How can I use binary search? and thank you for helping me!

          • »
            »
            »
            »
            »
            »
            16 months ago, hide # ^ |
             
            Vote: I like it +1 Vote: I do not like it
            // this is code
            temp.push_back(v[0]);
            for(int i = 1; i < n; i++) {
                    int l = lower_bound(temp.begin(), temp.end(), left - v[i]) - temp.begin();
                    int r = upper_bound(temp.begin(), temp.end(), right - v[i]) - temp.begin();
                    ans += r - l;
                    temp.push_back(v[i]);
            }
            

            Text use temp vector to store the values already visited and find the range that would satisfy the req condition. ( here left = total — y, right = total — x).

        • »
          »
          »
          »
          »
          16 months ago, hide # ^ |
           
          Vote: I like it +1 Vote: I do not like it

          Yes, just use lower_bound and upper_bound functions to find the range satisfying the given condition (either on the remaining part of the array or the completed part)

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

297858654 Can anyone tell me why this solution of C got hacked.

»
16 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

The judge gave an accepted verdict for my slightly wrong submission of Problem C during the contest, but in post-contest judgements I got WA on Test Case 10. :(

Idk if it was the issue with the judging software or the initial testcases were bad.

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

Obviously the problem E is binary search. For anyone here is the solution pretty straight forward.

Solution

  1. Observation only the cost values of ai and bi matters either take a penalty with maximum hit or don't. Any other values in between don't make any sense.

  2. Binary search if the cost is C then how many people can buy it happily and how many can't buy at all.

  3. Calcuate the penalty from these 2 values which is just total people — people who can buy happy — people who cannot buy at all and then see if penalty people are in our limit

  4. If in our limit take the current cost and go on max of all such costs.

I was 7 minutes late in contest. Observation 3 got me. I thought its in my best interest if I want to sell or not but apparently I have to sell if the customer can afford because "the customer is always right".

PS: How to approach F in simple language?

  • »
    »
    16 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +3 Vote: I do not like it

    If intially u have joker at 5th positon . if a0<5 it will occupy 4th and 5th position , if a0>5 it will occupy 5th and 6th position . so doing this u will get that joker will occupy from l to r . now if ai<l l--,else if ai>r r++ else the joker will also occupy 1st and nth position . now keep doing the same thing assuming u have 3 segments 1 to l1 , l to r and r2 to n

»
16 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Here is my solution to the Problem E which i find really interesting.

Firstly i hash all the values with the index of the vector then i count the values which might leave the negative review using a difference array which is values that are a[i]+1 to b[i] after that i iterate for the values and calculate the cost for which has k or less negative review.

Submission : 297938827

»
16 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

F was smart

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

F is so hard. I can't imagine F being a question about segments.

»
16 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Is the rating updated for you guys? This is taking a bit longer than usual.

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

For F , how to get 5 in the first example?I only get [2 , 3 , 4 , 5]

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

    Just implement the required operations in a straightforward way to calculate all possible deck states, then count unique positions for joker.

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

This round really preserved the beauty of Educational Rounds as the authors are the same... sadly, I could not attend it live and had to participate virtually.

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

What would be the difficulty rating of problem E and F? clist.by says 1980 for E and 2349 for F but I don't think it's that much, no?

»
16 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

My submission 298069513 for Problem E is giving wrong answer in testcase 2. Can anyone please help out why this is happening and at what testcase it fails? bcoz according to me my logic seems correct.

Thanks

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

    Since nobody replied to my comment I figured it out myself.

    The problem was in the case of duplicates in a and b arrays. This code didn't consider duplicates.

    This is resolved in this solution 299196423

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

Here a super-easy and clean implementation of problem E using upper_bound to find number of positive and negative reviews without creating new arrays (just sorting the original ones with negative values): 297888481

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

Shouldn't the test case for D, 7 10 13 4 2 5 2 4 3 1

be giving 3 instead of 4. What am I missing here?

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

Could anyone explain why O(n^2*q + 2^n * n ^ 2) solution for problem G works for n <= 20 and q <= 2*10^5? I normally suppose that 10^8 operations will be run in 1 second, so this solution runtime should be more than 4s.

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

I find my solution to D to be much simpler than the editorial's--the only downside is that it uses PBDS: https://mirror.codeforces.com/contest/2051/submission/299348552

I think the code is self-explanatory (ignoring the indentation and other weird quirks.)

»
11 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

can anyone tell me why my code is not passed 3rd test case for problem b https://mirror.codeforces.com/contest/2051/submission/319545029

»
8 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

MY SOLUTION FOR E ->

int countLessThan(const vector<int>& arr, int x) {
    int lo = 0, hi = arr.size();
    while (lo < hi) {
        int mid = lo + (hi &mdash; lo) / 2;
        if (arr[mid] < x) lo = mid + 1;
        else hi = mid;
    }
    return lo;
}


int countGreaterEqual(const vector<int>& arr, int x) {
    int lo = 0, hi = arr.size();
    while (lo < hi) {
        int mid = lo + (hi &mdash; lo) / 2;
        if (arr[mid] < x) lo = mid + 1;
        else hi = mid;
    }
    return arr.size() &mdash; lo;
}


void solve() {

    int n, k;
    cin >> n >> k;

    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    int res = 0;


    for(int i = 0; i < n; i++) {
        int c2 = countLessThan(a, a[i]);
        int c3 = countLessThan(b, a[i]);
        if(c2 - c3 <= k) {
            int cnt = countGreaterEqual(b, a[i]);
            res = max(res, cnt * a[i]);
        }
    }


    for(int i = 0; i < n; i++) {
        int cnt = countLessThan(a, b[i]);
        int c3 = countLessThan(b, b[i]);
        int c2 = countGreaterEqual(b, b[i]);
        if(cnt - c3 <= k || c2 <= k) {
            res = max(res, b[i] * c2);
        }
    }

    cout << res << endl;

}