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