题目描述

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;
}

[USACO17FEB]Why Did the Cow Cross the Road II S的更多相关文章

  1. 洛谷 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 ...

  2. 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P

    题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...

  3. [USACO17FEB]Why Did the Cow Cross the Road II P

    嘟嘟嘟 考虑dp. 对于ai,和他能匹配的bj只有9个,所以我们考虑从这9个状态转移. 对于ai 能匹配的一个bj,当前最大的匹配数一定是[1, j - 1]中的最大匹配数 + 1.然后用树状数组维护 ...

  4. [USACO17FEB]Why Did the Cow Cross the Road II

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4990 [算法] 首先记录b中每个数的出现位置 , 记为P 对于每个ai , 枚举(a ...

  5. BZOJ 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)

    题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边 ...

  6. 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S

    本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...

  7. 洛谷 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 ...

  8. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

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

随机推荐

  1. 网络1711-1712班 c 语言评分总表一览

    学号 姓名 作业地址 PTA实验作业5分 PTA排名2分 阅读代码2分 总结1分 代码规范扣分-2--0 总分 是否推荐博客 1 **莹 http://www.cnblogs.com/wwwwxy12 ...

  2. Beta敏捷冲刺每日报告——Day2

    1.情况简述 Beta阶段Scrum Meeting 敏捷开发起止时间 2017.11.2 00:00 -- 2017.11.3 00:00 讨论时间地点 2017.11.2 晚9:30,电话会议会议 ...

  3. Cocoapods最全完整使用教程

    什么是cocoapods cocoapods是库管理工具. cocoapods的用途 解决库之间的依赖关系.如前文所述: 一个开源的项目可能是另一个项目的基础, A依赖B, B依赖C和D, D又依赖E ...

  4. 静态关键字static用法。

    static的特点:1,static是一个修饰符,用于修饰成员.2,static修饰的成员被所有的对象所共享.3,static优先于对象存在,因为static的成员随着类的加载就已经存在了. 4,st ...

  5. Raid5两块硬盘掉线可以恢复数据吗_raid数据恢复案例分享

    本案例中发生故障的存储类型是HP P2000,虚拟化平台为vmware exsi,共有10块硬盘组成raid5(硬盘容量为1t,其中6号盘是热备盘),由于某些故障导致阵列中两块硬盘亮黄灯掉线,硬盘无法 ...

  6. 超绚丽CSS3多色彩发光立方体旋转动画

    CSS3添加了几个动画效果的属性,通过设置这些属性,可以做出一些简单的动画效果而不需要再去借助JavaScript.css3动画的属性主要分为三类:transform.transition以及anim ...

  7. Class-Based-View(CBV)

    我们都知道,Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承.封装.多态).所以Django在后来加入了Class-Based-View.可以让我们用类写V ...

  8. vue组件详解(四)——使用slot分发内容

    一.什么是slot 在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer>& ...

  9. 新概念英语(1-47)A cup of coffee

    新概念英语(1-47)A cup of coffee How does Ann like her coffee? A:Do you like coffee, Ann? B:Yes, I do. A:D ...

  10. Python学习之dict和set

    #coding=utf-8 # dict dict= {'bob': 40, 'andy': 30} print dict['bob'] # 通过dict提供的get方法,如果key不存在,可以返回N ...