可能是今天狀態不好,一個很簡單的二分,挺早之前就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;
}
/*
*/