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

输入输出样例

输入样例#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);
}

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8157149.html

时间: 2024-11-05 12:34:21

洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S的相关文章

洛谷 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 John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morn

bzoj 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)

题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边数,容易得到方程: 不连边: 连边: 实际是这样的,每个位置如果想连边,就要从能连边的位置之前找最大值,即 直接转移不可取,由于最多只从9个位置转移,我们可以缩减一维,用记录b序列进行到位置 j 的最大连边数,再用树状数组维护的最大前缀和方便转移 1 #include <bits/stdc++.h>

[USACO17FEB] Why Did the Cow Cross the Road I P (树状数组求逆序对 易错题)

题目大意:给你两个序列,可以序列进行若干次旋转操作(两个都可以转),对两个序列相同权值的地方连边,求最少的交点数 记录某个值在第一个序列的位置,再记录第二个序列中某个值 在第一个序列出现的位置 ,求逆序对数量即可 本以为是一道逆序对水题,结果被卡了20分.看了题解才恍然大悟,实际上,序列可以旋转 ≠ 序列成环,由于逆序对的特殊性(并不适用于环),故不能把一个序列单独旋转看成它们的相对移动,正着旋转一个序列≠反着旋转另一个序列(更详细证明可以看洛谷) 所以我们要对两个序列再反着进行一次同样的操作

[USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)

题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置,$y$表示在第二列的位置,$z$表示权值 两条线交叉,就是$x<x'$且$y>y'$,又要满足差值的绝对值小于等于$k$,就是$|z-z'|<=k$ 于是就转化为了一个三维偏序问题,直接上CDQ 具体细节请看代码 1 // luogu-judger-enable-o2 2 //minamot

Why Did the Cow Cross the Road III(树状数组)

Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during

洛谷P2879 [USACO07JAN]区间统计Tallest Cow

To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with th

洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The cows move from any of the N (1 <= N <= 250)

洛谷 P2879 [USACO07JAN]区间统计Tallest Cow 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2879 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told on

洛谷 2966 [USACO09DEC]牛收费路径Cow Toll Paths

https://www.luogu.org/problem/show?pid=2966 题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The c