zoukankan      html  css  js  c++  java
  • 洛谷 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 N1N (1 leq N leq 100,0001N100,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 N1B,KN). 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.

     

    输入输出样例

    输入样例#1: 复制
    10 6 5
    2
    10
    1
    5
    9
    输出样例#1: 复制
    1

    说明

    感谢@ 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=1;i<=b;i++){
            int x;
            scanf("%d",&x);
            vis[x]=1;
        }
        for(int i=1;i<=n;i++)    sum[i]+=sum[i-1]+vis[i];
        for(int i=1;i<=n-k+1;i++)    ans=min(ans,sum[i+k-1]-sum[i-1]);
        printf("%d",ans);
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    VMware Workstations Pro15.1.0并解锁Unlock3.0.2 安装黑苹果
    正则表达式对字符串匹配
    Linq操作
    C#模糊查询绑定datagridview
    wpf的datepicker处理
    动态调用webservice,不需要添加Web References
    C#调用sap接口及返回数据到sap
    C#中文件管理的运用(Twelfth Day)
    C#中继承,集合(Eleventh day)
    C#中字符串的处理,对象的引用及继承(Tenth day)
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8157149.html
Copyright © 2011-2022 走看看