Untitled
unknown
c_cpp
3 years ago
786 B
7
Indexable
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll pos[100005]; ll N, M; bool Condition(ll x){ ll car_pos = 1; ll last_pos = pos[0]; for(ll i = 1; i < N; i++){ if(pos[i] - last_pos >= x){ last_pos = pos[i]; car_pos++; if(car_pos == M){ return 1; } } } return 0; } ll binarySearch(){ ll start = 0; ll end = pos[N - 1]; ll ans = -1; while(start < end){ int mid = start + (end - start)/2; if(Condition(mid) == 1){ if(mid > ans){ ans = mid; } start = mid + 1; } else{ end = mid; } } return ans; } int main() { cin >> N >> M; for(int i = 0;i<N;i++){ cin >> pos[i]; } sort(pos, pos + N); cout << binarySearch(); return 0; }
Editor is loading...