https://vjudge.net/problem/POJ-3258
二分最小值,判断需要删去的点的个数,如果大于给定,则直接return 0,则说明该数需要再小。
最后注意,起点是0终点是l,起点可以不加进数组,终点必须加进去!!
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<map> 8 #define lson l, m, rt<<1 9 #define rson m+1, r, rt<<1|1 10 #define INF 0x3f3f3f3f 11 typedef unsigned long long ll; 12 using namespace std; 13 int l, n, m; 14 int a[50010]; 15 int C(int x) 16 { 17 int pre=0, ans=0; 18 for(int i = 0; i <= n; i++){//真正的末尾是l!!! 19 if(a[i]-pre<x){//距离<x,应该消去 20 ans++; 21 } 22 else{ 23 pre = a[i]; 24 } 25 if(ans > m){ 26 return 0; 27 } 28 } 29 return 1; 30 } 31 int main() 32 { 33 cin >> l >> n >> m; 34 for(int i = 0; i < n; i++){ 35 cin >> a[i]; 36 } 37 a[n] = l; 38 sort(a, a+n+1); 39 int lb = 0, ub = INF; 40 while(ub-lb>1){ 41 int mid = (lb+ub)>>1; 42 if(C(mid)){ 43 lb = mid; 44 } 45 else{ 46 ub = mid;//如果不满足就说明该数还要再小 47 } 48 } 49 cout << lb << endl; 50 return 0; 51 }