Advertisement
newb_ie

Untitled

Sep 15th, 2021
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. const int maxN = 100010;
  5. long long dist[maxN];
  6. long long n,c;
  7.  
  8. bool yes (long long d) {
  9. long long cow = c;
  10. long long prev_loc = dist[1];
  11. --cow;
  12. vector<int> loc;
  13. loc.push_back(dist[1]);
  14. for (int i = 2; i <= n; ++i) {
  15. if (dist[i] - prev_loc >= d) {
  16. loc.push_back(dist[i]);
  17. --cow;
  18. prev_loc = dist[i];
  19. }
  20. if (cow == 0) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26.  
  27. int main () {
  28. ios::sync_with_stdio(false);
  29. cin.tie(nullptr);
  30. cout.tie(nullptr);
  31. int T;
  32. cin >> T;
  33. for (int test_case = 1; test_case <= T; ++test_case) {
  34. cin >> n >> c;
  35. for (int i = 1; i <= n; ++i) cin >> dist[i];
  36. sort (dist + 1,dist + n + 1);
  37. long long res = 0;
  38. long long l = 1,r = dist[n];
  39. while (l <= r) {
  40. long long mid = l + (r - l) / 2;
  41. if (yes(mid)) {
  42. l = mid + 1;
  43. res = mid;
  44. } else {
  45. r = mid - 1;
  46. }
  47. }
  48. cout << res << '\n';
  49. //~ cout << dist[1] << ' ';
  50. //~ --c;
  51. //~ long long prev_loc = dist[1];
  52. //~ for (int i = 1; i <= n; ++i) {
  53. //~ if (dist[i] - prev_loc >= res) {
  54. //~ cout << dist[i] << ' ';
  55. //~ --c;
  56. //~ }
  57. //~ if (c == 0) break;
  58. //~ }
  59. }
  60. //~ cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  61. }
  62.  
  63. Problem Link : https://www.spoj.com/problems/AGGRCOW/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement