坐标排序c语言,C语言排序(5)___青蛙过河——(二分查找)

Description

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

样例:

#include

int arr[500050],len,m,n; //n个石头坐标、河的总宽、最多跳的次数、石头个数

int seeing(int x) //参数x表示跳远能力,此函数判断当前能力是否能够过河

{

int now=0; //now变量表示当前你的坐标.

int i=0; //所处的石头序号.

int times=0; //跳的次数

while(times

{

while(arr[i]-now<=x&&i<=n) //i递增.直到目标石头与当前位置的距离超过跳跃能力,

i++;

now=arr[i-1]; //青蛙跳到这个石头,把当前坐标更新到石头坐标.

times++; //跳跃次数加1

}

if(now==len) //m次跳跃后判断青蛙是否已经到了对岸

return 1; //返回1表示最小能力值可能比当前能力值小.

else

return 0; //返回0表示最小能力值一定比当前能力值大.

}

int bin(int l,int r) //此函数是用二分查找来枚举可能的能力值

{

int mid; //二分查找的中间值

while(l

{

mid=(l+r)/2; //令mid为左右界限的中间值

if(seeing(mid)!=0) //调用seeing函数判断当前能力值是否可以过河

r=mid; //如果能,把右边界调整为中间值.

else

l=mid+1; //如果不能,把左边界调整为中间值+1

} //以上调整产生差异的原因是因为seeing函数返回值的意义一个是可能,一个是一定(见上).

return r;

}

void quicksort(int left,int right) //定义的石块坐标排序的快排

{

int i,j,step;

i=left,j=right,step=arr[left];

if(i>j)

return;

while(i!=j)

{

while(i=step)

j--;

while(i

i++;

if(i

arr[i]^=arr[j]^=arr[i]^=arr[j];

}

arr[left]=arr[i],arr[i]=step;

quicksort(left,i-1);

quicksort(i+1,right);

}

int main()

{

int i;

while(scanf("%d%d%d",&len,&n,&m)!=EOF) //输入河的宽度、石头个数、最多跳的次数

{

for(i=0;i

scanf("%d",&arr[i]); //循环存储石头坐标

arr[n]=len; //因为终点可以落脚.所以终点可以看作最后一个石头.

quicksort(0,n); //石头坐标排序.

int max=arr[0]; //以下四排.目的找出相邻两个石头的最大距离,即使找出跳跃能力的下界.

for(i=0;i

if(arr[i+1]-arr[i]>max)

max=arr[i+1]-arr[i];

printf("%d\n",bin(max,len));

}

return 0;

}

题目大概意思:

对于每组输入数据:

第一排 分别是  河的宽度、石头的个数n、最多的跳跃次数.

以下n排是n个石头的坐标,

然后要求在最多跳跃次数以内,每次落地只能在石头上,跳到河的对岸.输出青蛙至少有每次跳多远的能力?

这道题目坑点:

1.题目输入数据的石头坐标可能不是按照从小到大排序,所以要定义快排排序.

本题主要是用的二分查找枚举,利用seeing函数巧妙判断.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值