2018 ccnu summer 二分 [Cloned] The Frog's Games
C - The Frog's Games
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
Output
For each case, output a integer standing for the frog's ability at least they should have.
Sample Input
6 1 2
2
25 3 3
11
2
18
Sample Output
4
11
題意:青蛙跳越過河,河的長度為L,河裡線性排列著n塊石頭分別排列,青蛙跳m下踩著石頭過河,問 青蛙每次最少跳多遠能過河?
解題思路:1.青蛙的跳遠距離可以限制在 1 ~ L ,其跳遠的距離(這裡假設為 x )可以用二分給出來,問題就變成了跳遠距離 x 是否能在青蛙跳 小於或等與 m 下的情況下成功跳到對岸。在跳的過程中遵循這種方式:每一次跳的過程中在跳遠距離範圍內儘可能跳到更遠的石塊上。若最後能成功在小於等於 m 次下跳到對面則返回 true,超過m次返回 false,若跳不過直接返回 false。 (這種方法有跳遠距離 x 小與兩塊石頭的情況出現下面給出另種限制條件)。
2.從所有的相鄰兩塊石塊中找到分隔距離最大的兩塊 距離為Max,則把青蛙的跳遠距離限制在Max ~ L 之間,這就不存在跳不過去的情況了,最後判斷按所遵循的方法跳的次數是否會 <= m 如果是返回 true ,否則 false ;
程式碼1:(限制條件在L範圍內的)
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int L,n,m;
int s[500005];
bool tofind(int mid)
{
int ans=0;
int los=0;
for(int i=1;i<=n+1;i++)
{
if(mid>=s[i]-s[los]&&(s[i+1]-s[los])>mid)
{
ans++;
los=i;
}
if(mid<s[i]-s[los])
{
return false;
}
}
ans++;
if(ans<=m)
{
return true;
}
else
{
return false;
}
}
int main()
{
while(scanf("%d%d%d",&L,&n,&m)!=EOF)
{
s[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&s[i]);
}
s[n+1]=L;
sort(s+1,s+n+1); //-- 借用了第二種限制條件的程式碼,這裡沒有刪去。
int Max=0;
for(int i=1;i<=n+1;i++)
{
if((s[i]-s[i-1])>Max)
{
Max=s[i]-s[i-1];
}
}
//-----------------------------------------
int l=1,r=L,mid;
while(r>=l)
{
mid=(r+l)/2;
bool an=(tofind(mid));
if(tofind(mid))
{
r=mid-1;
}
else
{
l=mid+1;
}
}
printf("%d\n",l);
}
return 0;
}
程式碼2:(限制條件在 Max ~ L 的)
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int L,n,m;
int s[500005];
bool tofind(int mid)
{
int ans,los;
ans=0;
los=0;
for(int i=1;i<=n;i++)
{
if(s[i]-s[los]<=mid&&s[i+1]-s[los]>mid) //2 11 18 25
{ // 2 6
ans++;
los=i;
}
}
ans++;
if(ans<=m)
{
return true;
}
else
return false;
}
int main()
{
while(scanf("%d%d%d",&L,&n,&m)!=EOF)
{
s[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&s[i]);
}
s[n+1]=L;
sort(s+1,s+n+1);
int Max=0;
for(int j=1;j<=n+1;j++)
{
if(s[j]-s[j-1]>Max)
{
Max=s[j]-s[j-1];
}
}
int l=Max,r=L,mid,c;
while(r>=l)
{
mid=(r+l)/2;
bool an=tofind(mid);
if(tofind(mid))
{
c=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
printf("%d\n",c);
}
return 0;
}
借鑑了csdn博主的文章,吸收了前輩的思想,寫出了第一種限制範圍的程式碼。雖然用了一天時間,但是我還是好菜啊。。
。。。。。
萬木春來