cannor147's blog

By cannor147, 4 weeks ago, In English,
Tutorial is loading...

Authors: MikeMirzayanov and geranazavr555

Authors' solution
Tutorial is loading...

Author: MikeMirzayanov

Authors' solution
Tutorial is loading...

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution
Tutorial is loading...

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution
Tutorial is loading...

Author: MikeMirzayanov

Authors' solution
Tutorial is loading...

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution
Tutorial is loading...

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution
Tutorial is loading...

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution
Tutorial is loading...

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution
 
 
 
 
  • Vote: I like it
  • +53
  • Vote: I do not like it

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

very good contest!

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

How to solve C2 if 't' constraint would have been t<=10^5 (without segment tree and treap) ?

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

    Use two Priority Queues, in one keep those that pass and in the other those that fail. Before printing the result, you have to fail as students as needed to sum less than M. Then, while the greatest from the pq that passes is greater than the smallest of the pq that fails, swap them. Finally, while you can pass the smallest from the pq that fail for free, do it.

    My submission during contest: 55775417

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

      So you're pushing/popping same element multiple times. How are you sure this will work in the given time constraint? Can you please explain a bit more?

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

      I have used a similar method but couldn't get the answer ...

      What I am is doing deleting removing the greatest from the pq till the sum is less than M and then adding the smallest from the pq that fails until the sum is than M ...

      Can someone help me know why won't this method work?

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

      I solved this with a similar approach here: 55809687, but I believe this solution also depends on the low t constraint. The amount of elements that can be popped from the priority queue at the end depends on the possible difference in answers between two subsequent indices. With the current constraints, the possible difference between t values of two indices is 99, and since each t value must be at least 1, the difference in answer can be at most 99. If you were to have t go up to m, you could construct a case where you alternate t values between 1 and m. For the cases where t is 1, you only need to remove the t values that are equal to m, and thus the answer is current index / 2. For the cases where t is equal to m, you need to remove all previous values and thus the answer is the current index. Here, the amount of elements popped and pushed is O(n^2) and will not pass.

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

      In your code you have written saco.top(); inside the while loop. What is the value of saco.top() when i=0 and you haven't put anything inside it. Can you help explain the code ?

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

        when i=0 then u r correct that there will be nothing in pq but u will never go inside that while as first element will always be less than M. also this is mentioned in problem statement ** It's guaranteed that all values of ti are not greater than M. **

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

    I used two BIT arrays to find the answer, 55812830
    $$$su$$$ stores the prefix sum,
    $$$seen$$$ stores the sorted idx,

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

    https://codeforces.com/contest/1185/submission/55759936

    Top programmers usually have the best solutions. He basically puts the students in a multiset (multiset automatically sorts things in O(logN)) and if the sum is bigger than M, he subtracts the biggest students in the multiset and prints the answer. Just doing this would give TLE so he also deletes students from his multiset if the sum is above M. For example, take this case:

    5 100
    80 40 40 40 60
    

    first sum is 80, so no need to fail any students, output is 0.

    second sum is 120, so we need to fail one student, output is 1. Since the sum is now bigger than M, and it will always be bigger than M from now forward, its in our best interest to elements from the multiset until the sum is smaller than M.

    This solution would I think work regardless of the constraint of $$$t$$$

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

      No, I don't think this solution works for large $$$t$$$.
      e.g. N = 20000, t = 10000, M = 10000
      and the array are
      1...(repeat 10000 times) 10000... (repeat 10000 times)

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

        Maybe maintain an array that stores the sum after each $$$i$$$,and then when sum > M, we use binary search to find the first sum in the array that would get the current sum below M, maybe do $$$int index = lower__bound(sum_array.begin(),sum_array.end(),current_student)-sum_array.begin()$$$ ? Because sum-current_student is less than M. And then whatever index we get, we add it to every other result. This seems like a TLE as well tbh, but I'm too lazy to figure out a better solution.

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

          Can you please explain why this approach gives TLE isn't the complexity n(lgn+ n)

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

      After understanding your explanation, check for this

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

    I have used two Fenwick Trees + Binary Search. It will work for t <= 2e7. Code: 55863688

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

      Could u explain ur code a little bit for what purpose are u using Fenwick tree

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

Good Contest...Thanks for Fast Editorials

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

nice contest!!

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

Hey can someone help me in F Two Pizzas:

In the editorial it has been written: "Then, we should go through all possible masks, that could be reached by two different pizzas.

For each such mask we should keep two pizzas with the smallest total cost, which gives us that mask (i. e. their bitwise-or is equal to our current mask)."

My question is how to choose two pizzas of min-cost that generate a particular mask without going back to brute-force which would, of course, be a TLE.

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

Though G1 was a bit easier than F, it's still a really good contest for div 2. The score distribution is a bit weird since i saw someone who solved 8 problems have a higher rank than someone who solved all.

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

The author solution is n^2 for problem d how did it work. Also is there any other approach to do it??? My approach is this-> https://codeforces.com/contest/1185/submission/55853531

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

I solved G2 by using a $$$O(n^3T^2)$$$ solution. I think it's incorrect but I can't hack it.

Can someone help me hack it?

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

can somebody tell me why my code is incorrect? https://codeforces.com/contest/1185/submission/55763542 it is easy to read

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

For problem D cant we maintain a difference array after sorting the initial array of pairs . now we traverse the difference array and maintain a count of number of different elements in the difference array .

if there is only one element say 1,1,1,1... then we can output first or last index since it is in ap already , if the difference array is something like 2 2 1 1 then we output the first occurrence of the changed difference that is index 3 in the above array if the difference array is something like 1 2 3 .... then -1.

is this approach correct ?

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

    Your second case will fail on test 1 2 4 6 8 Difference array will be 1 2 2 2 and your output will be 2 instead of 1.

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

      yes that was dumb of me , thanks.

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

      Could u also explain how u solved Nick an array(CF round 569 Problem B)

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

        yes that was pretty simple one if you convert all elements to non negative integer that is either zero or positives. Now you just need to sort the array and then make elements negative pairwise so that you can get maximum product at the end. If your array length is odd then you don't need to add any condition you just need to leave the last element as it will handle itself the maximum product. For example if your array is -2 -4 -1 0 0 1 2 3 then after converting them to non negatives it becomes 1 3 0 0 0 1 2 3 now after sorting and making the elements pairwise negative you will get 0 0 0 1 1 2 3 3 --> -1 -1 -1 -2 -2 -3 -4 -4 now you can restore your array order by sorting it with index for that you can store it in pair.

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

          Thank u so much for sharing this approach. My approach was count number of positive and negative elements in array and if both positive and negative elements are even or odd make all elements positive elements negative. Else make all positive elements negative except the last one but it is giving me a wrong answer. Can u explain y my logic is wrong?

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

            Consider the following example array[]={-2 -1 2 1} according to your approach neg_count==2 and pos_count==2 here both are even hence you will perform the operation on all elements and your new array will be [3 0 -3 -2] which leads to zero product which is incorrect.

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

    Check my submission history :|

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

in solution of problem D, i don't really understand why we need to find the maximum difference between two elements in sequence maxd = max(maxd, a[i + 1].first — a[i].first) and how we to to know if we do that, it will correct ?

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

    Please, try one else. One of authors added another solution, which wasn't coincidental with editorial. I hope, it will help you.

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

Could someone elaborate more on the $$$3^9$$$ bruteforce to get the number of satisfied people for each mask?

Or as in this submission: 55810335, Why is making the outer loop as the loop over the bits, and the inner loop as the loop over the masks stops double counting. I don't seem to get what it is trying to achieve with order.

Here's the for loop, $$$B$$$ is a const int which is equal to $$$9$$$ and $$$cntHappy$$$ has the number of satisfied people for each mask and at first contains the count of the input masks.

    for (int j = 0; j < B; ++j)
    {
        for (int msk = 0; msk < (1<<B); ++msk)
        {
            if ((1<<j)&msk)
                continue;
            cntHappy[msk|(1<<j)] += cntHappy[msk];
        }
    }

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

    Can Anyone Please Explain how to solve C2 problem in given time constraints. I am not able to understand the solution.

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

      Use greedy approach,let's say u have available A marks out of M (u need to pass a student that have index i so A=M-marks[i] ).u want to fill Max amount of students in A so u check count of student having marks 1 to 100 (which, u are maintaining count). Now greedily fill A, so u will get maximum number of passing students.

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

Can anyone please explain this particular line of code of problem c2 posted by author and please also explain the reason behind this line.

k += (d + j — 1) / j;

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

    I will try to explain it. So, according to the editorial, this line will be run if we have situation, when $$$s_i + t_i - count_k \cdot k \le M$$$. And in this situation just $$$a_i + count_k$$$ may be not mimimal answer (but we want to output the minimal answer).

    So we need to free some $$$s_i$$$ minutes. We know that $$$k$$$ students' durations equal $$$count_k$$$. And for the minimal answer we need to add only $$$\lceil \frac{s_i}{k} \rceil$$$ to $$$a_i$$$. Let me rewrite this line in editorial's naming:

    $$$a_i += (s_i + k - 1) / k$$$

    Note that in programming `(A + B — 1) / B' is equivalent to $$$\lceil \frac{A}{B} \rceil$$$

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

In problem E, Does taking input as :

char a[1000][1000];

for(int i=1;i<=n;++i){
         for(int j=1;j<=m;++j){
         cin>>a[i][j];   
         }
   }

takes more time than taking input like this:

string a[1000];

for(int i=1;i<=n;++i){
     cin>>a[i];
   }

plus later processing like:

for(int i=1;i<=n;++i){
         for(int j=1;j<=m;++j){
         //doing processing on each character of the matrix     
         }
   }

as given in the tutorial.

Why is my solution giving TLE?

The approach is that I go through the a->z and fill a temporary integer matrix of the same size that of the above matrix with that (character-'a'). ie — 0 for a, 1 for b and so on only on those positions where that particular character is present.

Then while filling the matrix for a given character, I check to see if the position on which I'm writing the corresponding number already has a number lesser than the number to be filled.

If so, the answer is NO because that will mean that the earlier occurring letter snake is overwriting the current letter snake. If not then I keep filling the temporary integer matrix.

At the end, the answer is YES and I print the starting and ending position of a snake in the matrix.

My answer is giving TLE. So, that's why I'm asking if there is any difference in taking the input.

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

Is

1

2 4

a..a

.... ` a valid input for Problem E?

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

Problem E

Getting error on test case 5 :

wrong answer actual picture doesn't equal to expected [test case 35]

Can anyone help me with that?

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

In problem F, can 2 people with 3 as favorite ingredient ever get pleased by a pizza with

3 4 5

as the ingredient.

Here, 3 is occurring only once in a given pizza while there are 2 people who need it.

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

In problem F, test case 3

1 5

9 9 8 7 6 5 4 3 2 1

3 4 1 2 3 4

1 4 5 6 7 8

4 4 1 3 5 7

1 4 2 4 6 8

5 4 1 9 2 8

the output is 2 4

With 2nd and 4th pizza being chosen, none of them suffice to the liking of the customer. Can anyone explain how we get the solution for above case? I am finding this problem a bit difficult to understand although it seems easy to read.

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

    With all the combinations of pizzas, the maximum number of friends which can be satisfied is 0. So we simply choose 2 pizzas with minimum cost.

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

Can anyone please help me in PROBLEM-D

Here is my code

I am failing on Test Case 42

Thanks in advance

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

56396995

why is this solution not working?

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

Problem E: Getting WA in test case 5

Checker log: wrong answer expected r1 == r2 || c1 == c2, but r1=1, c1=1, r2=2, c2=2 [test case 136]

This is my submission : 56490336. Can anyone help?