题目链接
题目:
二分答案,因为答案是具有单调性且有区间限制的,所以我们可以用二分来枚举答案解决,现在重要的如何去判断一个答案的有效性。我们只需要看看满足该要求的答案需要移掉多少石头就可以了
AC代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 5e4 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
ll d,n,m,arr[N];
bool judge(ll Mid)//判断答案的有效性函数
{
ll sum = 0,now = 0;
ll l = 0,r = n+1;
while(l < r)
{
l++;
if(arr[l] - arr[now] < Mid)
sum++;//移去的石头加一
else
now = l;//该石头不用移去
}
if(sum > m)
return false;
else
return true;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> d >> n >> m;
for(ll i = 1;i <= n;i++) cin >> arr[i];
arr[n+1] = d;
ll l = 1,r = d,ans = 0;
while(l <= r)
{
ll Mid = (l+r)/2;
if(judge(Mid))//判断
ans = max(ans,Mid),l = Mid + 1;
else
r = Mid-1;
}
cout << ans << endl;
}