# PEWDSVTS - Editorial

Author: Udit Sanghi
Tester: Teja Vardhan Reddy
Editorialist: Oleksandr Kulkov

DIFFICULTY:
SIMPLE
PREREQUISITES:
Priority queue
PROBLEM:
Pied Piper has A users and gain X users everyday. Meanwhile Hooli has B users and gain Y users everyday. Whichever company reaches Z users first takes over Pipernet.

Pied Piper has N supporters with contribution values C_1, \dots, C_N. When i-th supporter contributes, Pied Piper get C_i users instantly. After this C_i is halved, thus it’s going to be equal to \lfloor C_i / 2 \rfloor. What is the minimum number of times supporters must contribute?
EXPLANATION:
It’s always profitable to make all contributions on the first day and contribute only largest possible numbers at every moment of time. To do this you may, keep a current set C_1, \dots, C_N in priority queue and pick the largest one all the time returning half of it to the queue.

How to check if Pied Piper loses to Hooli? You have to find minimum integer T_1 such that A+XT_1 \geq Z and minimum integer T_2 such that B+YT_2 \geq Z and compare them. It holds that:

T_1 = \left\lceil \dfrac{Z-A}{X}\right\rceil, T_2 = \left\lceil \dfrac{Z-B}{Y}\right\rceil

Thus this check is done in the following code:

bool lose(int A, int X, int B, int Y, int Z) {
int t1 = (Z - A + X - 1) / X;
int t2 = (Z - B + Y - 1) / Y;
return t1 >= t2;
}


Work with priority queue is quite straight-forward and looks like this afterwards:

priority_queue<int> C = {C_1, ..., C_N};
while(C.top() != 0 && lose(A, X, B, Y, Z)) {
ans++;
A += C.top();
C.push(C.top() / 2);
C.pop();
}
if(lose(A, X, B, Y, Z)) {
cout << "RIP" << endl;
} else {
cout << ans << endl;
}


This solution works in O(N \log N \log C). Note that multiset wouldn’t do here due to higher constant, as well as full sorting of all possible values. But it’s possible to solve the problem in O(N \log C) using radix sort, which is a bit more complicated.
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.
Tester’s solution can be found here.
Editorialist’s solution can be found here.

3 Likes

https://www.codechef.com/viewsolution/24074877

@mathecodician @teja349 @melfice
Authors solution is O(T*n*30*log(n)) = O(5.10 * 10^8) (0.45 secs)
My first approach was O(T*n*30*log(n*30))= O(6.6* 10^8)
https://www.codechef.com/viewsolution/24055261
Mine is TLE
My second approach was with same time complexity as authors which passes in 0.42 secs
Too strict time limit… I hate author for keeping it that strict and that too in a short contest…
Also the test cases were weak in other way… because O(T*n*30*log(n) + T*(z-b)/y) = O(105.10 * 10^8)
also passes…
Please correct me if I am wrong…

1 Like

Could you help me to find where I went wrong in my solution?
https://www.codechef.com/viewsolution/24058564

@kprogrammer you should run loop until ‘fina’ becomes ‘z’ not 100 and after this your code is also not optimised it will give tle , try to reduce the number of sort operation or use priority queue as stated in editorial.

I am such a dumb idiot.
Since I am a noob, could you say how could I optimize this without the use of priority queue?

you are sorting array after each contribution , you can reduce it to 30 times in worst case.
see here

you can also go through this thread for further explanation.

Thanks.
I cannot understand a lot of c++ but i will try

I have done using multiset ,and its running properly.
https://www.codechef.com/viewsolution/24079558.

hii,
firstly line
21
what if (y*k)==(z-b)
second
after giving the contribution the last value will become half so you need to sort your array again . you don’t know if the half of the last value is still greater than 2nd last value(always take last value of the sorted array (largest))
and
third you should check whether is it possible for the given contribution to overcome the remaining difference.
try it now.

why we can not use a multiset ?
can you explain plz ?

@anmol1point0
Thanks…
I understand

1 Like