可能是今天状态不好,一个很简单的二分,挺早之前就ac过了,但是今天牛客遇到了,发现写不出来,说明当时还没搞懂。
题意不说了,这里就说一下几个点:
1.在什么情况下才可以搬掉石头?
应该是石头2到石头1的距离小于二分出的答案mid
的时候。为什么呢?因为mid
是二分出来的答案,也就是最短跳跃距离的最大值,既然他是最短跳跃距离的最大值,那么必然不能存在两个石头的距离小于mid,否则与mid矛盾。
2.ans的更新
二分算法的几个点,其中一个就是ans的更新,这题ans的更新条件就是搬掉的石头 要小于等于 可搬掉的石头数,听上去很简(sha)单(bi),实际上也很简单
溜了溜了 补作业去
/*
* @Author: hesorchen
* @Date: 2020-04-14 10:33:26
* @LastEditTime: 2020-06-09 20:14:35
* @Link: https://hesorchen.github.io/
*/
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI cos(-1)
#define PB push_back
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define lowbit(abcd) (abcd & (-abcd))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define FRE \
{ \
freopen("in.txt", "r", stdin); \
freopen("out.txt", "w", stdout); \
}
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
//==============================================================================
ll a[100010];
int main()
{
IOS;
ll l, n, m;
cin >> l >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
a[++n] = l;
ll L = 1, R = INF, ans;
while (L <= R)
{
ll mid = L + R >> 1;
ll res = 0, la = 0;
for (int i = 1; i <= n; i++)
if (a[i] - a[la] < mid)
//如果当前石头到前一个石头的距离小于最短距离,那么需要把这块石头搬掉
res++;
else
la = i;
if (res <= m) //如果需要搬掉的石头小于m,那么mid满足条件
{
ans = mid;
L = mid + 1;
}
else
R = mid - 1;
}
cout << ans << endl;
return 0;
}
/*
*/