1 条题解
-
1
#include <bits/stdc++.h> using namespace std; int n, m; int x[112345]; //检验间隔为 len 能不能凑出来 m 个牛舍 bool check(int len) { int cnt = 1; int lastPos = x[1]; for (int i = 2; i <= n; i++) if (x[i] - lastPos >= len) { cnt++; lastPos = x[i]; } return cnt >= m; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> x[i]; sort(x + 1, x + n + 1); int l = 1; int r = x[n] - x[1]; int ans = -1; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 652
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 2
- 标签
- 递交数
- 56
- 已通过
- 35
- 上传者