Problem Author: | Imran AKKI |
Prepared by: | Imran AKKI |
Edited by: | Hasan Saleh (MathModel) |
Solution The problem is equivalent to get the total distance travelled by each contestant and compute how many contestants who travelled strictly more than Bob did.
Let's simulate the total travelled distance for some speed and decrement of speed , at each step you'll continue to decrease original by a multiple of as long as it's positive , Formally the total distance will be
Here k=\lfloor \frac{s-1}{t} \rfloor , which is the maximum positive speed we can reach.
To calculate the distance , notice that appears k+1 times and we've (1+2+3+...+k)t as of in the negative part and we know 1+2+3+...+k=\frac{k(k+1)}{2} then the total distance is computed as
Now according to this , compute distance for i_{th} contestant as d_i sort values and count how many contestants ran more than Bob.
for _ in range(int(input())):
n,b=map(int,input().split())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
a=[]
want=-1
for i in range(n):
x=s[i]
r=(s[i]-1)//t[i]
x+=s[i]*r
v=(r*(r+1))//2
x-=v*t[i]
a.append(x)
if(i+1==b):
want=x
a.sort()
ans=1
for i in range(n-1):
if(a[i]==want and a[i+1]>want):
ans=n-i
break
print(ans)Complexity \mathcal{O}(n \log n).