River hopscotch
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:6697 |
|
Accepted:2893 |
Description
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. the excitement takes place on a long, straight river with a rock at the start and another rock at the end,LUnits away from the start (1 ≤L≤ 1,000,000,000). Along the river between the starting and ending rocks,N(0 ≤N≤ 50,000) More rocks appear, each at an integral distanceDiFrom 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 only 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 his cows and watches this event each year. but as time goes by, he tires of watching the timid cows of the other farmers limp compression ss the short distances between rocks placed too closely together. he plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. he knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove upMRocks (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 has to jump after removing the optimal setMRocks.
Input
Line 1: three space-separated integers:
L,
N, And
M
Lines 2 ..
N+ 1: each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing
MRocks
Sample Input
25 5 2214112117
Sample output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the START) to 2. after removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25 ).
Source
Usaco 2006 December silver
Solution:
This type of question is hard to understand... There are n + 2 stones, giving the distance from the last to the first, and inputting the distance between N stones in the middle to the first stone, requiring the removal of M stones, make the minimum value of the distance between two adjacent stones as big as possible...
The train of thought is still binary. Find the upper and lower bounds .. It's hard to understand .. In the following code
This statement is available.
For (INT I = 1; I <= N; I ++) {If (temp [I] <= mid) // The current distance is less than or equal to mid, note that mid is the current minimum value. If you do not delete the I-th stone, temp [I] becomes the expected minimum value {CNT ++; temp [I + 1] = temp [I + 1] + temp [I];}
Why is it temp [I] <= mid? Why is it true ?? If it is also deleted, does it mean that the distance between two adjacent stones is mid ?? I thought about why for a long time.
Let's assume you know why .. I am very grateful for leaving a message in the following comments ..
Code:
# Include <iostream> # include <stdio. h ># include <algorithm> using namespace STD; const int maxn = 50010; int dis [maxn]; int temp [maxn]; int main () {int L, n, m; scanf ("% d", & L, & N, & M); DIS [0] = 0; DIS [n + 1] = L; int left = 1000000000, Right =-1; for (INT I = 1; I <= N; I ++) {scanf ("% d ", & dis [I]) ;}n ++; sort (DIS, DIS + n + 1); For (INT I = N; I> = 1; I --) {dis [I] = dis [I]-Dis [I-1]; If (left> dis [I]) Left = dis [I]; if (right <dis [I]) Right = dis [I];} // dis [I] stores the distance from the I stone to the I-1 stone while (left <right) {for (INT I = 1; I <= N; I ++) temp [I] = dis [I]; int mid = (left + right)/2; int CNT = 0; For (INT I = 1; I <= N; I ++) {If (temp [I] <= mid) // The current distance is less than or equal to mid. Note that mid is the current minimum value, if I Stone is not deleted, temp [I] will become the reasonable minimum value {CNT ++; temp [I + 1] = temp [I + 1] + temp [I] ;}} if (CNT <= m) Left = Mid + 1; else right = mid ;} cout <right <Endl; return 0 ;}
[ACM] poj 3258 river hopscotch (binary, maximum minimum)