Poj3258-river hopscotch-method to find the largest Mimmun

Source: Internet
Author: User
Tags cmath

Description

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to R Ock in a river. The excitement takes place in a long, straight river with a rock at the start and another rock at the end, L units away fr Om the start (1≤l≤1,000,000,000). Along the between the starting and ending Rocks, N (0≤n≤50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping O Nly from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of he cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rock s placed too closely together. He plans to remove several rocks on order to increase the shortest distance a cow would have the to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he had enough resources to remove up ToM R Ocks (0≤m≤n).

FJ wants to know exactly how much he can increase the shortest distance *before* He starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow have to jump after removing the optimal set of M R Ocks.

Input line 1:three space-separated integers:l, N, and M
Lines 2.. N+1:each line contains a single integer indicating what far some rock was away from the starting rock. No Rocks share the same position.

Output Line 1: A single integer which is the maximum of the shortest distance A cow have to jump after removing M rocks

Sample Input

5 2
2
17

Sample Output

4

Hint before removing any rocks, the shortest jump is a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and, the shortest required jump was a jump of 4 (from-to-or from-25).

Test instructions:: Give a group of the number of n size between 0~l, when the M is removed, leaving the minimum value of the difference. (The main point, to include 0 points, and L points).

Idea:: First to sort the number of n+2, the minimum value must be subtracted from the adjacent two numbers.

(1) Assume that the minimum difference is mid,if (a[cur]-a[last] < mid) to remove the cur point, if the number of removed more than m to indicate that the mid is too large, otherwise it is too small.

(2) Remove L is left n+2-m (including 0 and L) and then through the n+2-m-1 cycle (because if there are k points, it is inevitable to produce k-1 difference), if the use of L can not be satisfied, it is too large, or too small.

For mid, we define L (left), R (right) by dichotomy, make mid = (l+r)/2, and update L, or R by comparing.

Update principle (1):: If (point >m removed) {r = Mid;} else {L = mid;} in the case of (r>l+1) loop

Update principle (2):: If (with L still cannot satisfy the loop ) {r = Mid;} else {L = mid;} in the case of (r>l+1) loop

Code (1)::

#include <iostream> #include <sstream> #include <ios> #include <iomanip> #include < functional> #include <algorithm> #include <vector> #include <string> #include <list> # Include <queue> #include <deque> #include <stack> #include <set> #include <map> #include &l t;cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <climits> #include
<cctype> using namespace std; #define Xinf Int_max #define INF 0x3fffffff #define MP (x, y) make_pair #define PB (x) push_back (×) #define REP (X,n) for (int x=0; x<n; x + +) #define REP2 (X,L,R) for (int x=l; x<=r; x + +) #define DEP (x,r,l) for (int x=r; x>=l;
x--) #define CLR (a,x) memset (a,x,sizeof (A)) #define IT iterator typedef long LL;
typedef pair<int,int> PII;
typedef vector<pii> VII;
typedef vector<int> VI;
const int MAXN = 10010;
#define INF 0X3FFFFFFF int a[50005];
int l,n,m; BOOL OK (int mid)
{int last = 0,cur,tot = 0;//tot is used to record the number of delete points while (a[last]<l) {cur = last + 1;    
            while (tot<=m+1) {if (A[cur]-a[last] < mid) {//To delete cur tot++;cur++ at this time;
        } else break;
		} last = cur;
if (tot>m) return false;//Delete point extra M returns false, R = Mid} return true;
		} int main () {while (cin>>l>>n>>m) {a[0] = 0;
		REP2 (i,1,n) cin>>a[i];
		A[N+1] = L;
		int L = 0,r = l;
		int mid;
		Sort (a,a+n+2);
			while (l<r-1) {mid = (l+r)/2;
			if (OK (mid)) L = mid;
		else R = Mid;
		} if (L = = L-1) l++;

	cout<<l<<endl;
 }
}
Code (2)::

#include <iostream> #include <sstream> #include <ios> #include <iomanip> #include < functional> #include <algorithm> #include <vector> #include <string> #include <list> # Include <queue> #include <deque> #include <stack> #include <set> #include <map> #include &l t;cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <climits> #include
<cctype> using namespace std; #define Xinf Int_max #define INF 0x3fffffff #define MP (x, y) make_pair #define PB (x) push_back (×) #define REP (X,n) for (int x=0; x<n; x + +) #define REP2 (X,L,R) for (int x=l; x<=r; x + +) #define DEP (x,r,l) for (int x=r; x>=l;
x--) #define CLR (a,x) memset (a,x,sizeof (A)) #define IT iterator typedef long LL;
typedef pair<int,int> PII;
typedef vector<pii> VII;
typedef vector<int> VI;
const int MAXN = 10010; #define INF 0X3FFFFFFF/*****************************************Header file ********************** ************************************************/int a[
50005];
int l,n,m;
    BOOL OK (int mid) {int last = 0;
        for (int i = 1; i < n+2-m; ++i) {//loop n+m-1 int cur = last + 1; while (A[cur]-a[last] < mid && cur < n+2) cur ++;//here cur will gradually accumulate if (cur = = n+2) return false;//appear in a
    The last element, which does not satisfy the loop, is cur;
		} return true;//has not seen the last element} int main () {while (cin>>l>>n>>m) {a[0] = 0;
		REP2 (i,1,n) cin>>a[i];
		A[N+1] = L;
		int L = 0,r = l;
		int mid;
			Sort (a,a+n+2);//Sort while (l<r-1) {mid = (l+r)/2;
			if (OK (mid)) L = mid;
		else R = Mid;
		} if (L = = L-1) l++;
	cout<<l<<endl;
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.