Блог пользователя aquiem

Автор aquiem, 3 года назад, По-английски

Count Freshers

As Srajan can spend at most X on any freshers at a time, you just have to print the count of freshers which costs less than X.

You can find the code here

The Greatest Battle

Here, you have to choose the members of "Association A" optimally so that they win the most rounds, to do this we will first sort both the arrays of powers then as we know that "Association B" always sends the strongest one, we can simply iterate of the second array starting from the back and that will give us the power of the person "Association B" chooses in each round. Now, we need to pick the person from "Association A" optimally so, what we will do is if the strongest person at present from "Association A" can't beat the strongest person from "Association B", then we will simply send the weakest person from "Association A", this will help us to minimize our loss as now, we can use the higher power person from "Association A" in some other round. If "Association A" has the higher power then just choose that person because as "Association B" sends their people in sorted order, so it is guaranteed that the next person from "Association B" will have a lower or equal power to the previous person.

You can find the code for this logic here

Confused Students

The brute-force approach to this question would be to simply iterate over the total number of days as the days <= $$$10^{5}$$$ and for each day again iterate over the number of intervals given and find how many students skip their classes. But this would result in an O($$$q^2$$$) complexity which is don't ideal. (Although in the given question you could submit this approach as the test cases are not that strict)

A better approach would the to use something like a "difference array" and the maximum value of given days is <= 10^5 we could create an array denoting the number of students present at $$$i^{th}$$$ day. Now how do we populate this array according to the given ranges? If we simply were to iterate over the given range and change the values, it would again result in an O($$$q^2$$$) complexity, So we have to somehow change all the values in the given in range in O(1) for this approach to have an effect. We will do this by simply changing 2 values, we will add/subtract 1 to the start day and subtract/add 1 to (end day + 1) after this if we were to find the prefix-sum array we would have our desired array. So, using this we are going to only change 2 values for each range and finally, we will find the prefix sum of our array and which would denote the number of students present/absent on the $$$i^{th}$$$ day.

You can find the code for this approach here

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится