-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathC.cpp
executable file
·77 lines (77 loc) · 1.63 KB
/
C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=500000+10;
int l,n,m,maxm;
int a[maxn];
int idx(int dis)//to find the closest stone before dis
//or to say,the most far stone the frog could jump to
{
auto ptr=upper_bound(a,a+n+2,dis);
int id=ptr-a-1;
//printf("dis=%lld id=%d\n",dis,id);
return id;
}
bool isok(int x)//to testify if our ability is x,could we cross the river within m times
{
if(x<maxm)
return false;
//printf("We are testing %lld\n",x);
int i,id=0,cnt=0;//we start at 0, cnt means the minimum times we jump
while(id!=n+1)
{
for(i=id+1;i<=n+1;i++)
if(a[i]-a[id]>x)
{
id=i-1;
cnt++;
break;
}
if(i==n+2)
{
id=n+1;
cnt++;
}
}
return cnt<=m;
}
int main(void)
{
while(cin>>l>>n>>m)
{
maxm=-1;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=0LL;
a[n+1]=l;
sort(a,a+n+2);
for(int i=1;i<=n+1;i++)
maxm=max(maxm,a[i]-a[i-1]);
if(n==0)
{
cout<<l<<endl;
continue;
}
if(m==n+1)
{
cout<<maxm<<endl;
continue;
}
int ll=maxm-1,rr=l;//(ll,rr]
//cout<<"MARK1"<<endl;
while(rr-ll>1)
{
int mid=(ll+rr)/2;
if(isok(mid))
rr=mid;
else
ll=mid;
}
//cout<<"MARK2"<<endl;
cout<<rr<<endl;
}
return 0;
}