先上代码:
#include<iostream> #include<algorithm> #define ll long long using namespace
std; int a[100001]; int n,m; bool check(int x){ int sum = 0; for(int i = 1;i <=
m;i++){ if(a[i] - x <= sum)//左边界比要扫的区域小 { if(a[i] <= sum)//机器人在需要打扫的区域内 sum =
a[i] + x - 1; else sum = sum + x; } else return 0;//左边界超出所需打扫区域 } return sum >=
n; } int main() { cin >> n >> m; for(int i = 1;i <= m;i++) cin >> a[i]; sort(a
+ 1,a + 1 + m); int lb = 0, ub = n, mid, ans; while(ub >= lb) { mid=(lb + ub) /
2; if(check(mid)) { ub = mid - 1; ans = mid; } else lb=mid+1; } cout << (ans -
1) * 2 << endl; return 0; }