HDU-4004 The Frog's Games (divide and conquer)

http://acm.hdu.edu.cn/showproblem.php?pid=4004

 

Problem 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

 

Meaning of the questions:

Frogs Kingdom Games began, the most popular game is iron frog Eventing, one of which is the jump across the river project. The project requires the frog cross the river by jumping athletes. The width of the river is L. There are arranged linearly in the river stones of n. The frog can use these stones skipping across the river, it fell into the river if it fails. Frogs can jump the highest number is m. Now we want to know the frog jump at least need to have much distance to be able to successfully complete the game.

 

Ideas:

With half of the maximum distance to find a single jump, look at this distance can be completed. Until you find the smallest number, the dichotomy boundary is the river width L.

 

code show as below:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const  int MOD = + 1E9 . 7 ;
 16  // const Double the PI = ACOS (-1); 
. 17  #define the Bug COUT << "---------------------" endl <<
 18 is  const  int MAXN 5E5 + = 10 ;
 . 19  the using  namespace STD;
 20 is  
21 is  int a [MAXN]; // store to the beginning of each stone distance 
22 is  int B [MAXN]; // store a difference between the stones the value 
23 is  
24  int main ()
 25  {
 26 is      int L, n-, m;
 27      the while (~ Scanf ( " % D% D% D" , & L, & n-, & m))
 28      {
 29          for ( int I = . 1 ; I <= n-; I ++ )
 30              Scanf ( " % D " , & A [I]);
 31 is          A [n-+ . 1 ] = L; // Finally jump on the banks 
32          Sort (a + . 1 , a + . 1 + n-+ . 1 );
 33 is          int MAX = 0 ; // answer can not be less than the maximum difference value of a stone or stone must jump these two over the past 
34 is          for ( int I = . 1 ; I <= n-+ . 1 ; I ++)
 35          {
 36              B [I] = A [I] -a [I- . 1 ];
 37 [              IF (B [I]> MAX)
 38 is                  MAX = B [I];
 39          }
 40          int L = MAX;
 41 is          int R & lt = L;
 42 is          the while (L <= R & lt) // binary search answers 
43 is          {
 44 is              int MID = (L + R & lt) >> . 1 ; // dichotomy of values (i.e. the maximum distance of the initial jump frog) 
45              int = NUM 0 ; // number of steps recorded hop 
46 is              for (int I = . 1 ; I <= n-+ . 1 ;) // used herein are greedy, a possible multi-hop several stones 
47              {
 48                  int SUM = 0 ; // test needs to jump from the stone 
49                  for ( int J = I; J <= n-+ . 1 ; J ++ )
 50                  {
 51 is                      IF (SUM + B [J] <= MID) // can continue to jump 
52 is                      {
 53 is                          SUM + = B [J];
 54 is                          IF (J == n-+ . 1 ) // jump to the shore, and deal with what 
55                         {
 56 is                              NUM ++ ;
 57 is                              I = + n- 2 ;
 58                              BREAK ;
 59                          }
 60                      }
 61 is                      the else // not continue to jump 
62 is                      {
 63 is                          NUM ++; // number of hops is added. 1 
64                          I = J; // next jump from this stone 
65                          BREAK ;
 66                      }
 67                  }
 68              }
69             if(num>m)
70                 l=mid+1;
71             else
72                 r=mid-1;
73         }
74         printf("%d\n",l);
75     }
76     return 0;
77 }

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11707506.html