AhmedAshraff

Untitled

Nov 23rd, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
  3. #define ll long long
  4. #define sz(s) (int)(s).size()
  5. #define all(s) (s).begin(),(s).end()
  6. using namespace std;
  7. void File();
  8. void sol();
  9. int main() {
  10.     boAshraf
  11.     File();
  12.     int t = 1;
  13. //    cin >> t;
  14.     while (t--) {
  15.         sol();
  16.     }
  17.     return 0;
  18. }
  19.  
  20. void sol() {
  21.     int n;
  22.     cin>>n;
  23.     vector<int>v(n);
  24.     for(auto &it:v)cin>>it;
  25.     sort(all(v));
  26.     int m;
  27.     cin>>m;
  28.     auto valid=[&](int d)->bool{
  29.         int cnt=0,last=-d;
  30.         for(auto it:v){
  31.             if(it-last>=d)cnt++,last=it;
  32.         }
  33.         return cnt>=m;
  34.     };
  35.     int ans=-1;
  36.     int l=0,r=1e5,mid;
  37.     while(l<=r){
  38.         mid=l+(r-l)/2;
  39.         if(valid(mid))ans=mid,l=mid+1;
  40.         else r=mid-1;
  41.     }
  42.     cout<<ans;
  43. }
  44.  
  45. void File() {
  46. #ifndef ONLINE_JUDGE
  47.     freopen("input.txt", "r", stdin);
  48.     freopen("output.txt", "w", stdout);
  49. #endif
  50. }
Add Comment
Please, Sign In to add comment