[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;
}
[USACO17FEB]Why Did the Cow Cross the Road II S的更多相关文章
- 洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S
P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm ...
- 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...
- [USACO17FEB]Why Did the Cow Cross the Road II P
嘟嘟嘟 考虑dp. 对于ai,和他能匹配的bj只有9个,所以我们考虑从这9个状态转移. 对于ai 能匹配的一个bj,当前最大的匹配数一定是[1, j - 1]中的最大匹配数 + 1.然后用树状数组维护 ...
- [USACO17FEB]Why Did the Cow Cross the Road II
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4990 [算法] 首先记录b中每个数的出现位置 , 记为P 对于每个ai , 枚举(a ...
- BZOJ 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)
题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边 ...
- 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S
本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...
- 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S
P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp
题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...
随机推荐
- Beta第七天
听说
- servlet线程同步问题-代码实现同步(转)
从servlet的生命周期中,我们知道,当第一次访问某个servlet后,该servlet的实例就会常驻 内存,以后再次访问该servlet就会访问同一个servlet实例,这样就带来多个用户去访问一 ...
- Hibernate之缓存
Hibernate为了解决频繁查询数据的效率问题,提供了三种级别的缓存 1.一级缓存 一级缓存 又叫 session缓存 .Session对象会缓存处于持久化状态的每个对象 ,如果下次想用数据表中同一 ...
- 织梦cms/dedecms清理冗余废弃未引用图片方法
原理描述: 在原有织梦后台菜单中增加"清理冗余图片按钮",实现清理冗余图片的功能. 操作步骤: 1. 打开后台dede\sys_sql_query.php代码 在该文件中搜索如下代 ...
- Python-Cpython解释器支持的进程与线程-Day9
Cpython解释器支持的进程与线程 阅读目录 一 python并发编程之多进程 1.1 multiprocessing模块介绍 1.2 Process类的介绍 1.3 Process类的使用 1.4 ...
- C语言学习之弹跳小球
重新回过头来看了一遍C语言,才发现我自己的无知,C语言其实好强大,我之前学的不过是一点C语法和做几个数学题.正好3月份的考试要考C语言,重新学一遍,先是在中国大学mooc上把翁恺老师的C语言刷了一遍, ...
- elk调试环境
http://10.110.22.30:9100/
- Linux进程管理:后台启动进程和任务管理命令
一.为什么要使程序在后台执行 我们的应用有时候要运行时间很长,如:几个小时甚至几个星期,我们可以让程序在后台一直跑. 让程序在后台运行的好处有: 终端关机不影响后台进程的运行.(不会终端一关机或者网络 ...
- Mac里安装配置Jdk
#下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.h ...
- 前端之CSS内容
一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). 二.CSS语法 1 ...