标签:
给n个石头, 可以移除其中的m个, 求移除之后它们之间距离的最大值。
看到最大值很容易想到二分, 我们二分距离x, 判断两个石头之间的距离是否小于x, 如果小于, 那么就移除当前的石头。
#include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue> #include <stack> #include <bitset> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(a)) #define rson m+1, r, rt<<1|1 #define mem1(a) memset(a, -1, sizeof(a)) #define mem2(a) memset(a, 0x3f, sizeof(a)) #define rep(i, n, a) for(int i = a; i<n; i++) #define fi first #define se second typedef pair<int, int> pll; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; int a[50006], n, m; int check(int x) { int pre = 0, sum = 0; for(int i = 1; i<=n; i++) { if(a[i]-pre<x) { sum++; } else { pre = a[i]; } } return sum<=m; } int main() { int L; cin>>L>>n>>m; for(int i = 1; i<=n; i++) { scanf("%d", &a[i]); } a[++n] = L; ll l = 0, r = L, ans = r; while(l<=r) { int mid = (l+r)>>1LL; if(check(mid)) { l = mid+1; ans = mid; } else { r = mid-1; } } cout<<ans<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/yohaha/p/5204495.html