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

 #include <bits/stdc++.h>

 using namespace std;

 inline int read()//快速读入
{
int f=,x=;
char c=getchar(); while(c<'' || c>'')
{
if(c=='-')f=-;
c=getchar();
} while(c>='' && c<='')
{
x=x*+c-'';
c=getchar();
} return f*x;
} int n,k,b,v[],s[],sum=; int main()
{
n=read(),k=read(),b=read();//输入 for(register int i=; i<=b; i++)
{
int x=read(); v[x]=;//记录损坏的灯的编号
} for(register int i=; i<=n; i++)
{
s[i]=s[i]+s[i-]+v[i];//计算从1~i损坏的灯的个数
} for(register int i=; i<=n-k+; i++)//枚举区间
{
if((s[i+k-]-s[i-])<sum)//如果这个区间损坏的灯数比当前最小值小
{
sum=s[i+k-]-s[i-];//就更新最小值
}
} printf("%d",sum);//输出最小值 return ;//结束
}

P.S. 这是本人在洛谷上AC的第400题。

题解【洛谷P3662】[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. 洛谷 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 ...

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

  5. 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III

    题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...

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

  7. [USACO17FEB]Why Did the Cow Cross the Road II S

    题目描述 The long road through Farmer John's farm has  crosswalks across it, conveniently numbered  (). ...

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

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

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

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

随机推荐

  1. 剑指offer-面试题27-二叉树的镜像-二叉树

    /* 题目:输入一个二叉树,输出该函数的镜像. */ /* 思路: 基础条件:树为空,或只有一个节点. 其它:递归交换二叉树的左右子树. */ void Mirror(TreeNode *pRoot) ...

  2. Zjnu Stadium HDU - 3047 带权并查集板子题

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...

  3. linux commands - 一次性解压多个tar.gz文件

    tar -zxvf list所有tar.gz文件,然后利用xargs将其作为参数传给tar命令.-n 1表示每次传一个参数. xargs: https://www.cnblogs.com/wangqi ...

  4. C#读取excel时提示“外部数据库驱动程序 (1) 中的意外错误”

  5. Unable to execute command or shell on remote system: Failed to Execute process

    1.问题描述 先说下我的项目环境:jenkins部署在windows下面,项目部署也是在windows下面,ssh服务器是FreeSSHd,原来是打算用Send files or execute co ...

  6. vs2008 asp.net “无法连接到ASP.NET Development server”

    这是因为该网站启动ASP.NET Development server时使用的端口被占用而导致的. 解决方法: 1.在“解决方案资源管理器”中选中网站项目名称,然后切换到“属性”窗口 2.在“属性”窗 ...

  7. PP: Meta-learning framework with applications to zero-shot time-series forecasting

    From: Yoshua Bengio Problem: time series forecasting. Supplementary knowledge: 1. what is meta-learn ...

  8. python&mysql

    第一种方法,使用 pymysql库 import pymysql.cursors # Connect to the database connection = pymysql.connect(host ...

  9. cli4适配移动端

    1.首先在项目中安装以下依赖 npm install px2rem-loader --savenpm install amfe-flexible --savenpm install postcss-p ...

  10. IDEA的Debug模式灰色无法运行

    1. 检查本地环境是否配置得当,进入项目右键看项目是否可以正常debug运行 2.  如果不可运行,那么选择File--Project Structrue 3. 修改Src文件,如图然后就可以正常使用 ...