BledDest's blog

By BledDest, 4 weeks 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
  • +56
  • Vote: I do not like it

»
4 weeks ago, # |
  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.

  • »
    »
    4 weeks ago, # ^ |
      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!

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

      I think E problem has some ambiguity because if the price advances bi ,that someone may dont buy it and also dont leave a negative review

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

        all right, i am wrong sorry

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

          It seems to be just a translation problem. There is ambiguity. It means neither buying nor leaving negative comments.

»
4 weeks ago, # |
  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:(

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

    Does that mean they are always consecutive?

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

      $$$i < j$$$ essentially just means that they are different and that you should divide the result you get by not enforcing $$$i < j$$$ by $$$2$$$.

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

    Bro I'm in your exact same position, I solved a similar problem before where we calculated for each index, we subtract 1 for the index itself if counted, and then we finally divide our answer by 2, I solved a problem that looks exactly like this. I felt sorrow when I realized it.

    Also add to that problem E, it was easy to solve if you knew that the price would not be anything outside of arrays A and B, but genius me was trying to do binary search on the price :)

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

    During contest I also didn't realize it, so this was my submission. 297901632

»
4 weeks ago, # |
  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).

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

    The thrid customer will give a negative review as well (9 <= 9)

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

    3rd customer (ai=5) gives negativer review right . so neg_rev=2 not possible one

»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

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

»
4 weeks ago, # |
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

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

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

  • »
    »
    4 weeks ago, # ^ |
      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.

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

      Why is it not unimodal?

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

        Earning = cost of tree * no. of customer who buys.

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

          it will increase till next person starts not buy tree and then suddenly again decrease

          lets say a starts to not buy it at 10 then from one to 9 your earning will inc. but suddenly your earning will decrease at 11 because 1 person but it may also depend on people who buying it if they can compensate it .....

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

            Instead of prices, i kept track of negative reviews, if negative reviews is more than k, i changed my high to mid-1 else low = mid+1, whats wrong with this approach.298136423

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

              For doing binary search you need to have the negative rev array as follows for prices first k , but this is not how neg rev look for the prices , try with some test cases may be with the second pretest 1 test case where you never reach cost of 12 if you do as you mentioned

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

              there is nothing wrong but it is incomplete you can say....

              you will reach a price where you will get k negative reviews at best but have you considered how people will buy trees

              every price less then low will be valid price but how much will you will depend on how many people will buy you trees may be at some price less people buy your product but cost is high enough that you earn more or at some price less price more people buy tress that you earn more

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

                you can iterate over all the price less than your final low ....

                or you can check for all the prices given in question which is less than equal to your final low

      • »
        »
        »
        »
        4 weeks ago, # ^ |
        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

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

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

  • »
    »
    4 weeks ago, # ^ |
    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

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it +1 Vote: I do not like it
      try this
»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

why the testcases of the problems are opened?

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

    yeah the idea is similar , there are multiple problems that follows the inequality pattern.

»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

https://codeforces.net/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 ??

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

    I don't think you can do BS on price as it would not follow regular structure. Consider price x and y | x < y, if y is valid then x may or may not be valid because it may happen that x is in > k customer range. Correct me if I am wrong.

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

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

»
4 weeks ago, # |
  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

»
4 weeks ago, # |
  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

»
4 weeks ago, # |
  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.

  • »
    »
    4 weeks ago, # ^ |
      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

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

      Yeah, my bad and thanks for pointing that out.

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

      What about my approach? Are there any flaws?

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

        Time complexity is O(n^2)

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

          Yeah, but what about cases after test 3? Would it get TLE or WA or AC? Currently, I can't submit because of system testing.

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

            After you change (m — k == 0) to (n — k == 0) you will get TLE for your code

      • »
        »
        »
        »
        4 weeks ago, # ^ |
          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.

        • »
          »
          »
          »
          »
          4 weeks ago, # ^ |
            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!

          • »
            »
            »
            »
            »
            »
            4 weeks ago, # ^ |
              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).

        • »
          »
          »
          »
          »
          4 weeks ago, # ^ |
            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)

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

            Oh, thank you for helping me this far. I didn't think of binary search because I just brute forced the solution.

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

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

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

    in the else if you wrote m-k==0 instead of n-k==0

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

      Ahh. My bad. Thanks for pointing it out. I had written m-k>=2 too but changed it before submission for the if() part. I forgot to change the else part.

»
4 weeks ago, # |
  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.

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

    I guess pretests contained only n = m cases, at least my solution that swaped n and m in some places passed the pretests.

»
4 weeks ago, # |
  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?

  • »
    »
    4 weeks ago, # ^ |
    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

»
4 weeks ago, # |
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

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

    this sir is exactly how i thought as well. anyway had a rough contest.

    for reference , here's my submission: 298046917

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

F was smart

»
4 weeks ago, # |
  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.

»
4 weeks ago, # |
  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.

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

Hi, for problem E, can someone explain me why this submission 297935421 in contest got AC but for later tests got TLE? The total complexity isn't $$$O(n \log n)$$$?

I other problem before, I used the same technique and got TLE too.

Thanks!

EDIT: TLE was for setting a bad comparator to sort() function. 298263208

»
4 weeks ago, # |
  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]

  • »
    »
    4 weeks ago, # ^ |
      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.

»
4 weeks ago, # |
  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.

»
4 weeks ago, # |
  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?

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

    E in my opinion should be between 1400 and 1600. Couldn't really solve F so can't give my opinion on it.

»
4 weeks ago, # |
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

  • »
    »
    3 weeks ago, # ^ |
      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

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

if someone solved d using binary search pls share your solution

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

    Yes, I was able to implement it for search within a range. Check this Python 3 solution: https://codeforces.net/contest/2051/submission/297906734

    Try to read through the implementation, instead of conventional check on a value, it checks for the value being $$$<$$$ lower bound ( = $$$\Sigma - a[i] - y$$$ given max. sum ), $$$>$$$ upper bound ( = $$$\Sigma - a[i] - x$$$ given min. sum ), or being in the range with >= lower bound and <= upper bound.

    In case you wonder about the bounds, $$$\Sigma - a[i] - a[j] < x \implies \Sigma - a[i] - x < a[j]$$$ holds as long as the values are non-negative.

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

Ok if you want to understand Problem E...

watch this super easy code.... https://codeforces.net/contest/2051/submission/298088585

What i did was to use sweep line algorithm to find efficiently the number of negative reviews for a given price (which will be someone among a's or b's) and uses binary search to find the customers who can afford the tree and if negative reviews are below the constraint(k) booommm we got a possible answer and i update my final answer.

»
4 weeks ago, # |
  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

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

For Problem E, simply use line sweep:

keep track of total a[i] and b[i] at each point. Then line sweep them.

»
4 weeks ago, # |
  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?

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

Did anyone solve E using ternary search? Also I would love to know whether or not it can actually be solved using ternary search on price p of trees.

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

E was nice sweepline, 298190107

»
4 weeks ago, # |
  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.

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

For E i am getting tle in sweep line solution can anyone help

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

My submssion for problem C is giving TLE for test case 3. I don't know why. Can someone tell me why ? It seems efficient. Below is my solution:

def findMissingNum(q,k,n):
    sum_arr= 0
    for ind in range(k):
        sum_arr+=q[ind]
    total = (n*(n+1))//2
    return (total - sum_arr)

def resultDeterminer(n: int,m: int,k: int,a: list,q: list):
    if k < (n-1):    
        return "0"*m
    elif k == n:
        return "1"*m
    else:
        ans = ""
        missing_num = findMissingNum(q,k,n)
        for ind in range(m):
            if a[ind]!=missing_num:
                ans+="0"
            else:
                ans+="1"
        return ans
    
if __name__ == "__main__":
    t = int(input())
    while(t):
        n,m,k = map(int, input().split())
        a = list(map(int, input().split()))
        q = list(map(int, input().split()))
        print(resultDeterminer(n,m,k,a,q))
        t-=1
»
3 weeks ago, # |
  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://codeforces.net/contest/2051/submission/299348552

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