列表

详情


NC25100. [USACO 2006 Dec S]River Hopscotch

描述

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, L units 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 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 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 across 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 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 has to jump after removing the optimal set of M rocks.

输入描述

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.

输出描述

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

示例1

输入:

25 5 2
2
14
11
21
17

输出:

4

说明:

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).

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Pascal(fpc 3.0.2) 解法, 执行用时: 33ms, 内存消耗: 620K, 提交时间: 2019-07-04 11:45:36

var
 n,m,k,l,r,max,mid,sum,ans:int64;
 i:longint;
 a:array[0..100001] of int64;
procedure kuaipai(l,r:longint);
var
 i,j,k,s:longint;
begin
 i:=l;
 j:=r;
 k:=a[(i+j) div 2];
 repeat
  while a[i]<k do
   i:=i+1;
  while a[j]>k do
   j:=j-1;
  if i<=j then begin
                s:=a[i];
                a[i]:=a[j];
                a[j]:=s;
                i:=i+1;
                j:=j-1;
               end;
 until i>j;
 if i<r then kuaipai(i,r);
 if l<j then kuaipai(l,j);
end;
function check(x:longint):boolean;
var
 j,cnt:int64;
 i:longint;
begin
 j:=0;
 cnt:=0;
 for i:=1 to n do
  begin
   if(j+x>a[i])then cnt:=cnt+1
               else j:=a[i];
  end;
 if cnt>m then exit(false)
          else exit(true);
end;
begin
  readln(k,n,m);
  for i:=1 to n do
    begin
      readln(a[i]);
    end;
  n:=n+1;
  a[n]:=k;
  kuaipai(1,n);
  l:=0;
  r:=k;
  while l<=r do
    begin
      mid:=(l+r) div 2;
      if check(mid) then begin
                          l:=mid+1;
                          if mid>ans then ans:=mid;
                         end
                    else r:=mid-1;
    end;
  writeln(ans);
end.

C++11(clang++ 3.9) 解法, 执行用时: 41ms, 内存消耗: 536K, 提交时间: 2020-01-31 19:14:42

#include<bits/stdc++.h>
using namespace std;
int n,m,w[50005];
bool ok(int s)
{
	int tm=m,now=1,temp=2;
	while(now<=n+1&&tm>=0)
	{
		if(w[temp]-w[now]<s)tm--;
		else now=temp;
		temp++;
	}
	if(tm<0)return 0;
	return 1;
}
int main()
{
	int d;cin>>d>>n>>m;w[n+2]=d;
	for(int i=2;i<=n+1;i++)cin>>w[i];
    sort(w+1,w+n+3);
	int l=1,r=d;
	while(l<r-1)
	{
		int mid=(l+r)>>1;
		if(ok(mid))l=mid;
		else r=mid-1;
	}
	if(ok(r))cout<<r;
	else cout<<l;
    return 0;
}

上一题