Header Ad

HackerEarth Roy and Coding Contest problem solution

In this HackerEarth Roy and Coding Contest problem solution, Roy is going to organize the Finals of Coding Contest in his college's computer labs. According to the rules of the contest, problem statements will be available on each computer as a pdf file.

Now there is N number of computers and only one of them has the pdf file. Roy has collected M number of pen drives from his friends. Copying pdf from computer to pen drive or from pen drive to a computer takes exactly 1 minute. Roy has to copy pdf to all N computers in the minimum (optimal) time possible. You are to find this minimum time. Please see Sample Test Explanation for clarity.


HackerEarth Roy and Coding Contest problem solution


HackerEarth Roy and Coding Contest problem solution.

def roy_and_coding_contest():
for t in xrange(input()):
n,m = map(int,raw_input().split())
count = 1
res = 1
while count < m and count < n:
count *= 2
res += 1
if count > n:
count /= 2
res -= 1
#print count
tmp = n-count
res = res + tmp/m
if tmp%m != 0:
res += 1
if n == 1:
print '0'
else:
print res
roy_and_coding_contest()

Second solution

#include <iostream>
using namespace std;

int main()
{
int t; cin>>t;
while(t--)
{
int N,M; cin>>N>>M;
int T=0;
int Ph=0; int Pdh=M;
int Ch=1; int Cdh=N-1;
// Ph pendrive have, Pdh pendrive dont have, Ch computer have, Cdh computer dont have,
while(Pdh && Cdh)
{
// cCh temp Computer have, cPh temp Peddrive have
T++;
int cPh = Ph + min(Pdh,Ch);
int cCh = Ch + min(Cdh,Ph);
Pdh -= min(Pdh,Ch);
Cdh -= min(Cdh,Ph);
Ph=cPh;
Ch=cCh;
}
if(Cdh)
T += (1 + ((Cdh-1)/Ph));
cout<<T<<endl;
}
return 0;
}

Post a Comment

0 Comments