fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. using ll = long long;
  8. using ld = long double;
  9.  
  10. #define all(x) x.begin(),x.end()
  11. #define v(x) vector<x>
  12. #define nl '\n'
  13. #define fxd(x) fixed << setprecision(x)
  14. template<class t> using ordered_set = tree<t, null_type, less<t>, rb_tree_tag, tree_order_statistics_node_update>;
  15. template<class t> using ordered_multiset = tree<t, null_type, less_equal<t>, rb_tree_tag, tree_order_statistics_node_update>;
  16.  
  17.  
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  21. int n, k ; cin >> n >> k;
  22. v(int) stalls(n);
  23. for (int i = 0; i < n; i++)
  24. {
  25. cin >> stalls[i];
  26. }
  27. ll l = 0, r = stalls[n-1] , mid , pos = -1;
  28. while (l <= r)
  29. {
  30. mid = (l+r) /2;
  31. int cows = k-1 , CurrentDistans = 0;
  32. for (int i = 1; i < n; i++)
  33. {
  34. CurrentDistans += stalls[i] - stalls[i-1];
  35. if(cows == 0)
  36. {
  37. break;
  38. }
  39. else if (CurrentDistans >= mid)
  40. {
  41. cows--;
  42. CurrentDistans = 0;
  43. }
  44. }
  45. if(cows <= 0)
  46. {
  47. l = mid+1;
  48. pos = mid;
  49. }
  50. else
  51. {
  52. r = mid-1;
  53. }
  54. }
  55. cout << pos;
  56. }
Success #stdin #stdout 4.28s 1725716KB
stdin
Standard input is empty
stdout
Standard output is empty