列表

详情


NC24076. [USACO 2017 Feb S]Why Did the Cow Cross the Road II

描述

The long road through Farmer John's farm has N crosswalks across it, conveniently numbered 1…N (1≤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 K working signals.

输入描述

The first line of input contains N, K, and B (1≤B,K≤N). The next B 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 K working signals somewhere along the road.

示例1

输入:

10 6 5
2
10
1
5
9

输出:

1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 1248K, 提交时间: 2020-03-14 17:47:20

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

上一题