Advertisement
YeetMaster69

SPOJ-AGGRCOW

May 3rd, 2022
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. testCases=int(input())
  2. for _ in range(testCases):
  3.     N,C=[int(i) for i in input().split()]
  4.  
  5.     arr=[int(input()) for _ in range(N)]
  6.  
  7.     arr.sort()
  8.  
  9.     def ifpossible(C,mid):
  10.         cow=1
  11.         lastpos=0
  12.         for i in range(1,len(arr)):
  13.             if arr[i]-arr[lastpos]>=mid:
  14.                 cow+=1
  15.                 lastpos=i
  16.                 if cow>=C:
  17.                     return True
  18.         return False
  19.  
  20.     strt=0
  21.     end=arr[-1]
  22.     ans=-1
  23.     mid=(end+strt)//2
  24.  
  25.     while strt<=end:
  26.         if ifpossible(C, mid):
  27.             ans=mid
  28.             strt=mid+1
  29.         else:
  30.             end=mid-1
  31.         mid=(end+strt)//2
  32.     print(ans)
  33.  
  34. #**
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement