1061 字
5 分钟
2289B题解

2289B Problem from Chebarkul#

题目描述#

You are given an array of integers a1,a2,…,ana_1,a_2,\dots,a_n and an integer kk.

Find the number of integers 1≤i≤k1\le i\le k such that in all subsequences of the array aa of length kk, the same number is located at the ii-th position.

A sequence bb is a subsequence of a sequence aa if bb can be obtained from aa by the deletion of several (possibly, zero or all) elements from arbitrary positions.

输入格式#

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041\le t\le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and kk (1≤k≤n≤2⋅1051\le k\le n\le 2\cdot 10^5).

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\dots,a_n (1≤ai≤n1\le a_i\le n).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

输出格式#

For each test case, output one integer — the number of positions at which the same number is located in all subsequences of length kk of the array aa.

说明/提示#

题面里的样例没有拿到,此处从略。

思路#

先把「子序列的第 ii 个元素」翻译成「原数组的哪个位置」。

长度为 kk 的子序列,是从 nn 个数里删掉 n−kn-k 个。要让第 ii 个元素落在原数组的 jj 位:

  • 它前面必须留下 i−1i-1 个,所以 j≥ij\ge i;
  • 它后面必须留下 k−ik-i 个,所以 n−j≥k−in-j\ge k-i,即 j≤i+n−kj\le i+n-k。

合起来,第 ii 个位置能对应到的原下标恰好是整段

[i, i+n−k][i,\ i+n-k]

而且这段里的每个下标都能取到(删掉 jj 前面若干个、后面若干个即可)。

于是问题变成:这段区间 [i,i+n−k][i,i+n-k] 里的数全部相同,第 ii 个位置才被固定;只要有一个不同,就能通过不同的删法让第 ii 位取到不同的值。

区间长度是 n−k+1n-k+1。ii 从 11 到 kk,这 kk 个区间正好是「一个长度 n−k+1n-k+1 的窗口从左往右滑 kk 次」——起点 ii 的右端点是 i+n−k≤k+n−k=ni+n-k\le k+n-k=n,不越界。

所以:滑窗,数每个窗口里有多少种不同的数,等于 1 就记一次。

怎么维护「不同数的个数」#

用值桶 ct[v] 记窗口里 vv 出现了几次,dis 记桶里非零的个数。窗口右移一格:

  • 删掉左端那个数,它的计数减到 0 就让 dis--;
  • 加进右端那个数,它原本计数是 0 就让 dis++。

只有计数跨过 0 的时候 dis 才变,这样每次滑动是 O(1)O(1)。

复杂度 O(n)O(n) 每组。

正确代码#

#include<bits/stdc++.h>
using namespace std;
#define int long long
int T,n,k;
int a[200060];
int ct[200060];
int ans;
signed main()
{
cin>>T;
while(T--){
ans=0;
cin>>n>>k;
for(int i=1;i<=n;i++) ct[i]=0;
for(int i=1;i<=n;i++) cin>>a[i];
int len=n-k+1;
int dis=0;
for(int i=1;i<=len;i++){
if(ct[a[i]]==0) dis++;
ct[a[i]]++;
}
if(dis==1) ans++;
for(int i=2;i<=k;i++){//i->i+len-1;
ct[a[i-1]]--;
if(ct[a[i-1]]==0) dis--;
if(ct[a[i+len-1]]==0) dis++;
ct[a[i+len-1]]++;
if(dis==1) ans++;
}
cout<<ans<<endl;
}
return 0;
}

我想了什么#

这题最容易走偏的一步是「子序列第 ii 位能取到哪些下标」。 想清楚是「一个连续的区间 [i,i+n−k][i,i+n-k]」之后,题目就变成了滑窗;如果一直想着枚举子序列或者用组合数,就出不来了。

为什么窗口长度是 n−k+1n-k+1 而不是 kk? 因为窗口是「这一位可能来自的原下标范围」,长度由「能删多少」决定(n−kn-k 个可删,加上自己就是 n−k+1n-k+1),跟 kk 不是一回事。我一开始也差点按 kk 去算。

清桶用的是 for(int i=1;i<=n;i++) ct[i]=0; 而不是 memset。 每组只清 nn 个位置,比清满 2×1052\times10^5 便宜——多组数据、nn 又都小的时候差别明显。

但这里藏着一条没写下来的假设:桶是按数值 ct[a[i]] 索引的,却按下标范围 1..n1..n 去清。这要求 ai≤na_i\le n,题面里正好保证了(1≤ai≤n1\le a_i\le n),代码本身却没把这条表达出来。

i+len-1 的上界正好卡在 nn。 循环 i<=k 而 i+len−1≤k+(n−k+1)−1=ni+len-1\le k+(n-k+1)-1=n,一个不多一个不少,所以不用额外判越界。

ans 是全局的,每组开头 ans=0。 跟清桶是同一个习惯:全局数组省内存,代价是每次自己重置。

#define int long long 之后 ct 每个元素 8 字节。 计数最大也就是 nn,用 int 就够,这里多花了一倍内存。

复杂度#

每组时间 O(n)O(n):建第一个窗口 O(n−k+1)O(n-k+1),滑动 O(k)O(k),相加就是 O(n)O(n)。空间 O(n)O(n)。

小结#

核心只有一句话:第 ii 个位置能来自的下标是连续区间 [i,i+n−k][i,i+n-k],所以「固定」等价于「这段全相同」。

剩下的就是定长滑窗 + 值桶维护不同数个数,标准写法。

第 118 篇,共 147 篇本文属于系列

2289B题解
https://www.hhluckyex.cn/posts/problen_solution/2289b/
作者
HHluckyex
发布于
2026-11-22
许可协议
CC BY-NC-SA 4.0

分享文章

生成精美分享图或复制链接,与更多人分享本文。

继续阅读

沿着主题读

基于共同的标签与分类

换条路线

从其他文章中稳定抽取