River Hopscotch
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 10842 |
|
Accepted: 4654 |
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 unit S away from the start (1≤ L ≤1,000,000,000). Along the between the starting and ending rocks, N (0≤ n ≤50,000) more rocks appear 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 to m Rocks (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 Rocks.
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
MRocks
Sample Input
25 5 2214112117
Sample Output
4
The main topic: Cows across the river game, timid cows only dare to jump to the nearest stone, river long l meters, there are N stone, give you each stone distance from the starting position,
Now let you remove the M block stone, so that the distance between the adjacent stones is the largest, so that you output the longest distance is how much.
Thinking analysis: In recent days have been doing two points of the topic, leading to do this problem read test instructions, looked at the data range is basically determined to use two points to write, basic
All the same, but each topic has a need to pay attention to the place, such as to prevent overflow specially used __int64 (not to create no use), the weak or contribute
Two FA wa, the main reason is in the beginning and end of the processing, the starting point and the end of the stone can not be moved! Then the end is taken out alone to deal with, the rest if not satisfied
Remove the stone on the right, and success a drops.
Code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace Std;
const int maxn=50000+100;
__int64 A[MAXN];
int n,m;
BOOL Check (__int64 x)
{
int t=0;
int i;
__int64 Len;
__int64 Now=a[0],next;
__int64 sum=0;
for (i=n;i>=0;i--)
{
if (a[n+1]-a[i]<x) t++;
else break;
}
int h=i;
for (i=1;i<=h;i++)
{
Next=a[i];
Len=next-now;
if (len<x)
{
t++;
if (t>m) return false;
}
else Now=a[i];
}
return true;
}
int main ()
{
__int64 L;
while (scanf ("%i64d%d%d", &l,&n,&m)!=eof)
{
a[0]=0;
for (int i=1;i<=n;i++)
scanf ("%i64d", &a[i]);
A[n+1]=l;
Sort (a,a+n+2);
__int64 l=0,r=l;
__int64 ans=0;
while (L<=R)
{
__int64 mid= (l+r) >>1;
cout<<mid<<endl;
if (check (mid)) ans=mid,l=mid+1;
else r=mid-1;
}
Cout<<check (4) <<endl;
printf ("%i64d\n", ans);
}
return 0;
}
The problem of maximizing the poj3258 binary minimum value