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

输入输出样例

输入样例#1:
10 6 5
2
10
1
5
9
输出样例#1:
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 }

 

转载于:https://www.cnblogs.com/Y-E-T-I/p/7105269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值