[USACO17FEB]Why Did the Cow Cross the Road II S
题目描述
The long road through Farmer John's farm has crosswalks across it, conveniently numbered (). To allow cows to cross at these crosswalks, FJ installs electric crossing signals, which light up with a green cow icon when it is ok for the cow to cross, and red otherwise. Unfortunately, a large electrical storm has damaged some of his signals. Given a list of the damaged signals, please compute the minimum number of signals that FJ needs to repair in order for there to exist some contiguous block of at least working signals.
共有N个信号灯,编号为1~N,有B个信号灯损坏,给你它们的编号。
问,最少修好几个信号灯,可以有K个编号连续的信号灯。
输入输出格式
输入格式:
The first line of input contains , , and (). The next lines each describe the ID number of a broken signal
输出格式:
Please compute the minimum number of signals that need to be repaired in order for there to be a contiguous block of working signals somewhere along the road.
输入输出样例
10 6 5 2 10 1 5 9
1
非常简单的二分。
可知当a[i]-a[j]>=k时,求min(i-j-1);
二分a[i]-k即可
此题卡常数,把能减少时间的方法都用上
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<set> 6 using namespace std; 7 int ans=2e9,n,a[100001],k,b,l,sum[100001]; 8 int dinary(int x) 9 {int as; 10 int l=1,r=b; 11 while (l<=r) 12 { 13 int mid=(l+r)/2; 14 if (a[mid]<x) as=mid,l=mid+1; 15 else r=mid-1; 16 } 17 return as; 18 } 19 int get() 20 {int s=0; 21 char ch; 22 ch=getchar(); 23 while (ch<'0'||ch>'9') ch=getchar(); 24 while (ch>='0'&&ch<='9') 25 { 26 s=s*10+ch-'0'; 27 ch=getchar(); 28 } 29 return s; 30 } 31 int main() 32 { 33 cin>>n>>k>>b; 34 int last,l,i,j; 35 for (i=1;i<=b;i++) 36 { 37 a[i]=get(); 38 } 39 sort(a+1,a+b+1); 40 a[++b]=n+1; 41 for (i=1;i<=b;i++) 42 { 43 if (a[i]<=k) continue; 44 int pos=dinary(a[i]-k); 45 ans=min(ans,i-pos-1); 46 } 47 cout<<ans; 48 }