1. 程式人生 > >洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S

洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S

信號燈 tar each back nes als 輸出格式 name imu

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

洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S