题目描述

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即可
此题卡常数,把能减少时间的方法都用上
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
int ans=2e9,n,a[],k,b,l,sum[];
int dinary(int x)
{int as;
int l=,r=b;
while (l<=r)
{
int mid=(l+r)/;
if (a[mid]<x) as=mid,l=mid+;
else r=mid-;
}
return as;
}
int get()
{int s=;
char ch;
ch=getchar();
while (ch<''||ch>'') ch=getchar();
while (ch>=''&&ch<='')
{
s=s*+ch-'';
ch=getchar();
}
return s;
}
int main()
{
cin>>n>>k>>b;
int last,l,i,j;
for (i=;i<=b;i++)
{
a[i]=get();
}
sort(a+,a+b+);
a[++b]=n+;
for (i=;i<=b;i++)
{
if (a[i]<=k) continue;
int pos=dinary(a[i]-k);
ans=min(ans,i-pos-);
}
cout<<ans;
}

最新文章

  1. 循环中的continue功能
  2. 在webapp上使用input:file, 指定capture属性调用默许相机,摄像,录音功能
  3. WPF QuickStart系列之数据绑定(Data Binding)
  4. ubuntu下mysql的常用命令,MySQL数据库的基本操作命令
  5. saiku之行速度优化(三)
  6. using-ef-code-first-with-an-existing-database
  7. jsp学习一
  8. Set Font Properties On Mouse Hover Of Push Button And Text Items At Run time In Oracle Forms
  9. 在RHEL5.4下安装ORACLE11G
  10. Nginx 变量漫谈(二)
  11. LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
  12. SpringBoot-MongoDB 索引冲突分析及解决
  13. Android为TV端助力 am命令以及hotkey文件的编写
  14. 《C#图解教程》一览
  15. h5屏幕旋转的时间和样式的设置
  16. crm作业知识点集合[一]
  17. es6笔记(3.1)三个点的“...”的作用
  18. HihoCoder - 1867: GCD (莫比乌斯容斥)
  19. [leetcode DP]53. Maximum Subarray
  20. Python 并行分布式框架:Celery 超详细介绍

热门文章

  1. alpha冲刺总结随笔
  2. 翻译:CREATE FUNCTION语句(已提交到MariaDB官方手册)
  3. openlayers调用瓦片地图分析
  4. JAVAEE——BOS物流项目09:业务受理需求分析、创建表、实现自动分单、数据表格编辑功能使用方法和工作单快速录入
  5. JavaScript AJAX实例
  6. C# HttpClient设置cookies的两种办法 (转发)
  7. 【问题解决】jhipster-registry-master空白页
  8. redis命令详解
  9. springboot字符集乱码
  10. python/进程线程的总结