River Hopscotch POJ - 3258

The cows each year hold various special version of hopscotch games, including jump to another piece of rock from a rock in the river. This exciting activities in a long straight river, from the starting point at the start and end of each L away a rock (1 ≤ L ≤ 10 ^ 9). Between the start and end, there is a rock N (0 ≤ N ≤ 50000), from the start point of every rock are Di (0 <Di <L).

During the race, the cow turns from the starting point, try to reach the end of each step can only jump to another piece of rock from a rock. Of course, the strength of the bad cows can not reach the end, in the middle of the river to exit the game.

Farmer John is proud of his cows every year and watched the game. But over time, watching the timid cows to other farmers in the close proximity between the rocks slowly forward, he felt very tired. He plans to remove some rocks, so that from the beginning to the end of the course, the shortest distance of the longest jump, jump to the end of the distance is not included . He may be removed except at most M blocks of rock start and end points (0 ≤ M ≤ N).

Help Farmer John determine: the removal of these rocks, the shortest jump maximum distance is the number?

Entry

The first row contains three integers separated by a single space L, N, M.

2 to N + 1 lines, each an integer that represents the distance from the start point of each rock. No two rocks appear in the same position.

Export

Output an integer, i.e. maximum value of the shortest distance of the jump.

Example input

25 5 2
2
14
11
21
17

Sample output

4

prompt

After removal of the rock 2 and 14 located at two shortest hop distance reaches the maximum value 4 (from 17 to 21 or from 21 to 25).

 

Meaning of the questions: Given n rock, you should make sure to remove up to m block of rock makes a cow longest shortest possible jump how much distance? (A river length L, the intermediate N stone, up to M block is removed, the maximum value of the minimum distance between two arbitrary requirements stones)

Problem-solving ideas: half of each distance solving.

Code 1:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for(int a = b; a <= c; a++)
#define RFOR(a,b, c) for(int a = b; a >= c; a--)
#define sc(a) scanf("%d",&a)
#define off ios::sync_with_stdio(0)
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MOD = + 1E9 . 7 ;
 const  int MAXN = 2E5 + . 5 ;
 const  Double PI = ACOS (- 1.0 );
 const  Double EPS = 1E- . 8 ; 

int A [MAXN];
 int L, N, M, ANS; 

int Solve ( int MID) { 
     int CNT = 0 , POS = 0 ; 
    the FOR (I, . 1 , N) {
         IF (A [I] - A [POS] <MID) { // two stones smaller than the distance mid (answer pitch) , and remove the 
            CNT ++ ; 
        } 
        the else pos = i;
    }
    if (cnt > M) { //如果不满足要求
        return 0;
    }
    else 
        return 1;
}

int main() {
    sc(L), sc(N), sc(M);
    FOR(i, 1, N) {
        sc(a[i]);
    }
    a[0] = 0, a[N+1] = L;
    sort(a, a + 2 + N);
    int l = a[0], r = L;
    int ans = 0;
    while(L <= R & lt) {
         int MID = (L + R & lt) >> . 1 ;
         IF (Solve (MID)) { // satisfy 
            L = MID + . 1 ; // further searching to see whether there is a greater minimum distance 
            ans MID =; // record the answer 
        }
         the else R & lt mid- = . 1 ; // this distance does not satisfy the meaning of the questions, to smaller search 
    } 
    the printf ( " % D \ n- " , ANS); 
}

 

Code 2: using a l = mid r = mid and Solutions

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for(int a = b; a <= c; a++)
#define RFOR(a,b, c) for(int a = b; a >= c; a--)
#define sc(a) scanf("%d",&a)
#define off ios::sync_with_stdio(0)
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MOD = + 1E9 . 7 ;
 const  int MAXN = 2E5 + . 5 ;
 const  Double PI = ACOS (- 1.0 );
 const  Double EPS = 1E- . 8 ; 

int A [MAXN];
 int L, N, M, ANS; 

int Solve ( int MID) { 
     int CNT = 0 , POS = 0 ; 
    the FOR (I, . 1 , N + . 1 ) {
         IF (A [I] - A [POS] <MID) { // two stones smaller than the distance mid (answer pitch ), and remove 
            cnt ++ ;
        } 
        The else POS = I; 
    } 
    IF (CNT> M) { // If not required 
        return  0 ; 
    } 
    the else  
        return  . 1 ; 
} 

int main () { 
    SC (L), SC (N), SC (M); 
    the FOR (I, . 1 , N) { 
        SC (A [I]); 
    } 
    A [ 0 ] = 0 , A [N + . 1 ] = L; 
    Sort (A + . 1 , A + . 1 + N);
     int L = A [ 0 ], INF = R & lt; // note that here r must be open big, or you'll WA! ! ! ! ! 1
     int ANS = 0 ;
     the while (rl is an> . 1 ) {
         int MID = (L + R & lt) >> . 1 ;
         IF (Solve (MID)) { // satisfy 
            L = MID; 
        } 
        the else R & lt = MID; 
    } 
    the printf ( " % D \ n- " , L); 
}

 

Guess you like

Origin www.cnblogs.com/AlexLINS/p/12642395.html