### Queen_of_Mirzapur's blog

By Queen_of_Mirzapur, history, 4 weeks ago,

This problem appeared in the coding round of a company(cannot name) which was hiring interns at 100k stipend(India). This was the toughest problem of the round. I have seen similar problems at other contests but do not know the optimal solution.

Statement:

You have a shield whose power increases by 1 every second. You are given N time intervals when a Missile is fired at you. You can face it if your shield has enough power by then, or you can use counter-Missile to divert the incoming missile and decide not to take the damage. If you decide to take the damage, the power of the shield reduces by the power of incoming missile. The power of the shield should remain non-negative.

The shield starts generating power from time = 0.

Test Case:

N = 3 (Number of missiles fired at you)

Details of N Missiles.

Time -> Damage

20 ->20

21 ->4

22 ->3

It is optimal to use counter-missile on the first attack although you had enough power to take the attack.

So the minimum number of missiles that you should use is 1

Initial power = 20 since time = 20(You must have gained 1, 20 times)

N = 1 , Use counter-Missile : Power remaining: 20

N = 2; Take the attack : Power remaining : 17

N = 3, Take the attack : Power remaining : 15.

The task is to find the minimum number of missiles.

So, the answer for the case : 1.

Constraints:

N <= 1e5;

Damage <= 1e9 (I was surprised here).

The solution seems DP-ish but I dont know how to solve for Damage <= 1e9.

• +9

 » 4 weeks ago, # |   +11 The problem seems to be straightforward binary search since the search space for a number of counter missiles is monotonic i.e. as we increase the number of counter missiles there will be a threshold after which we will be able to always survive. Search Space will be like [False False False ... False True True True...] so we just need to find the First True for which we will apply Binary Search.Now Comming To the Check Function Part in our Binary Search: The check. function tells Whether we will survive with 'mid' number of counter missiles or not.So it can be easily Implemented in o(nlog(n)) complexity with the help of Max Heap or sorted set in cpp.(I assume that damage array and attack time array is sorted based on time if not u can sort it urself ). Implementation :bool check(int mid){ int shieldValue=0; int time=0; setpq; for(int i=0;i=0;}
•  » » 4 weeks ago, # ^ |   +1 So, what you are saying is that, we should defend only a prefix of missiles fired at us?
•  » » » 4 weeks ago, # ^ |   0 no that is not what i am saying , i am saying we have to avoid shield value to drop to zero at any instant of time
•  » » » » 4 weeks ago, # ^ |   +7 Well following this strategy, we won't need to binary search over the answer. We will count the needed defends in the while loop where you are using the priority queue. Instead of mid-- we will write count++. And return the value of count.
•  » » » » » 4 weeks ago, # ^ |   +3 oh! that seems to be correct , indeed nice observation!
•  » » » » » » 4 weeks ago, # ^ |   +7 Would be better if you could correct it in the main explanation. Also, please take a note: Use a multiset. The shieldValue can be zero. Else your approach is really nice and correct! I also wrote a Solutionint n; cin>>n; vector> a(n); for(auto &p:a) cin>>p; multiset s; int shieldValue=0; int last=0; int ans=0; for(int i=0;i
•  » » » » » » » 4 weeks ago, # ^ |   +5 Thanks, that helped. Thanks to both. Also, I think that WHILE could be replaced by IF because at most one removal is done in every iteration.
•  » » » » » » » » 4 weeks ago, # ^ |   0 Yes, that is also correct.
 » 4 weeks ago, # |   +12 Why can't you name the company? You are posting from fake id and If coding round is over then I see no point of hiding company name.
•  » » 4 weeks ago, # ^ |   0 The company name was Rippling, if that concerns you. And this was post contest and I can only promise you that. Thanks.
 » 4 weeks ago, # |   +11
•  » » 4 weeks ago, # ^ |   +3 What is the expected difficulty(rating) of this problem.
•  » » » 4 weeks ago, # ^ |   +10 1700-1800
•  » » 4 weeks ago, # ^ |   0 It was indeed stolen, because not only the main idea but most of the words match.