洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S
题目描述
The long road through Farmer John's farm has NN crosswalks across it, conveniently numbered 1 \ldots N1…N (1 \leq N \leq 100,0001≤N≤100,000). 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 KK working signals.
共有N个信号灯,编号为1~N,有B个信号灯损坏,给你它们的编号。
问,最少修好几个信号灯,可以有K个编号连续的信号灯。
输入输出格式
输入格式:
The first line of input contains NN, KK, and BB (1 \leq B, K \leq N1≤B,K≤N). The next BB 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 KKworking signals somewhere along the road.
输入输出样例
说明
感谢@ jlyzxxm1 提供题意简述
思路:前缀和维护一下即可。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n,k,b;
int ans=0x7f7f7f7f;
int vis[MAXN],sum[MAXN];
int main(){
scanf("%d%d%d",&n,&k,&b);
for(int i=;i<=b;i++){
int x;
scanf("%d",&x);
vis[x]=;
}
for(int i=;i<=n;i++) sum[i]+=sum[i-]+vis[i];
for(int i=;i<=n-k+;i++) ans=min(ans,sum[i+k-]-sum[i-]);
printf("%d",ans);
}
洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S的更多相关文章
- 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...
- 洛谷 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 ...
- 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G
//神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...
- 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...
- 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III
题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...
- 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S
本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...
- [USACO17FEB]Why Did the Cow Cross the Road II S
题目描述 The long road through Farmer John's farm has crosswalks across it, conveniently numbered (). ...
- [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 ...
随机推荐
- Uboot优美代码赏析1:目录结构和malkefile分析
Uboot优美代码赏析1:目录结构和malkefile分析 关于Uboot自己选的版本是目前最新的2011.06,官方网址为:http://www.denx.de/wiki/U-Boot/WebHom ...
- javascript-知识点集合
第三课.JavaScript的语法与关键字 1.JavaScript的语法 字符串.数字.布尔.数组.对象.Null.Undefined 1.js的变量区分大小写 username userName ...
- PostgreSQL hstore 列性能提升一例
PostgreSQL 支持hstore 来存放KEY->VALUE这类数据, 事实上也相似于ARRAY或者JSON类型. 要高效的使用这类数据,当然离不开高效的索引.我们今天就来看看两类不同的 ...
- HDU2188 悼念512汶川大地震遇难同胞——选拔志愿者
悼念512汶川大地震遇难同胞--选拔志愿者 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- redis的javaclientJedis简单封装
经过我们团队的一番讨论,终于决定使用redis来进行我们的业务缓存.redis会将数据缓存到内存中,执行效率会非常快.同一时候异步将数据写入到磁盘中.进行持久化. 且redis支持主从同步,支持分布式 ...
- bzoj1103: [POI2007]大都市meg(树链剖分)
1103: [POI2007]大都市meg 题目:传送门 简要题意: 给你一棵树,给出每条边的权值,两个操作:1.询问根到编号x的最短路径的权值和 2.修改一条边的边权 题解: 很明显啊,看懂了题基 ...
- tensorfllow MNIST机器学习入门
MNIST机器学习入门 这个教程的目标读者是对机器学习和TensorFlow都不太了解的新手.如果你已经了解MNIST和softmax回归(softmax regression)的相关知识,你可以阅读 ...
- [JZOJ 5909] [NOIP2018模拟10.16] 跑商(paoshang) 解题报告 (圆方树)
题目链接: https://jzoj.net/senior/#contest/show/2529/2 题目: 题目背景:尊者神高达很穷,所以他需要跑商来赚钱题目描述:基三的地图可以看做 n 个城市,m ...
- js中es5 使用call方法继承实现 1.0
function Parent(name){ this.name = name; this.showMess = function(){ return this.name; } } Parent.pr ...
- STM8S103之独立看门狗和窗口看门狗
独立看门狗时钟来源为LSI:窗口看门狗时钟来源为CPU: 窗口看门狗窗口的含义是:喂狗必须在一定的窗口期内完成,不能过早也不能过晚. 总结:防止程序复位,用独立看门狗. 独立看门狗使用的流程:参见库函 ...