NC25100. [USACO 2006 Dec S]River Hopscotch
描述
输入描述
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; }