題目大意:
在一條河的起點和終點之間有N塊石頭,起點和終點上也有石頭。農夫可以移走M塊石頭(不包括起點和終點)使得
移走的每兩塊石頭(包括起點和終點)的最短距離最大。求這個值。
大致思路:
我們可以二分這個最短距離,然後求出如果這個是最短距離,我們需要在原來的基礎上去掉幾個石頭。
去掉石頭的個數如果比M大,說明我們枚舉的最短距離比真正的最短距離大了。
去掉的石頭個數比M小,說明我們枚舉的最短距離比真正大最短距離小了。
代碼:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define mmset(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
const int N = 5e4 + 5;
int data[N],n,m,k;
int count1(int mark);
/*
25 5 2
2
14
11
21
17
示例輸出
4
*/
int main()
{
while(~scanf("%d %d %d",&n, &m, &k))
{
m += 2;
data[1] = 1, data[m] = n;
for(int i = 2; i < m; i++)
{
scanf("%d",&data[i]);
}
sort(data + 1, data + 1 + m);
int a = 1, b = n, m ;
while(a <= b)
{
m = (a + b) / 2;
int temp = count1(m) ;
if(temp <= k)
{
a = m + 1;
}
else
{
b = m - 1;
}
}
printf("%d\n",b);
}
return 0;
}
int count1(int mark)
{
int res = 0, temp = 0;
for(int i = 2; i <= m - 1; i++)
{
if(data[i] - data[i-1] + temp < mark)
{
res++;
temp += data[i] - data[i-1];
}
else
{
temp = 0;
}
}
return res;
}