Topic Portal: http://acm.hdu.edu.cn/showproblem.php?pid=4004
The problem is that frogs are going to cross the river, now give you the width of the rivers, the number of stones in the river (frogs to jump across the river from the stone, these stones are perpendicular to the bank of a straight line)
And the frog can jump up the number of times, as well as each stone away from the river, asked the frog step at least to jump how many meters can cross the river "
This is a two-point greedy problem, starting from 0 to the river width of two points, two separate a number and then judge in such a minimum number of steps (the number of jumps in the distance) can cross the river
Be greedy when you judge.
The main difficulty in thinking, the key is to think of two points up, can think of two code is very good write (experimental two points of thinking is very strong)
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 //typedef long Long ll;5 intl,n,m;6 intnum[500001];7 intCheckintx)8 {9 if(x*m<l)return 1;//The number of steps that can cross the river is necessarily greater than the averageTen intI=1, j=0, step=0; One while(i<=n+1) A { -step++; - if(num[i]-num[j]>x)//cannot satisfy the jumps between the adjacent two stones apparently not. the return 1; - while(num[i]-num[j]<=x&&i<=n+1//Try to make this step jump over more stones, greed is basically this format -i++; -j=i-1; + } - if(step>m)return 1;//judge whether to exceed the specified number of times + return 0; A } at intMain () - { - intLeft,right,i,mid; - while(~SCANF (" %d%d%d",&l,&n,&m)) - { - for(i=1; i<=n;i++) inscanf"%d",&num[i]); -Sort (num+1, num+1+n); tonum[0]=0; num[n+1]=l; +left=0; right=l; - while(left<=Right )//two min. number of steps the { *Mid= (left+right)/2; $ if(check (mid))Panax NotoginsengLeft=mid+1; - Else theright=mid-1; + } Aprintf"%d\n", left); the } + return 0; -}
Hdu 4004 (two points plus greedy) frogs cross the river