原题
You’re given a permutation a of length n (
1
≤
n
≤
1
0
5
1\le n\le 10^5
1≤n≤105). For each
i
∈
[
1
,
n
]
i \in [1,n]
i∈[1,n], construct a sequence
s
i
s_i
si by the following rules:
1.
s
i
[
1
]
=
i
s_i[1]=i
si[1]=i
2.The length of
s
i
s_i
si is n, and for each
j
∈
[
2
,
n
]
,
s
i
[
j
]
≤
s
i
[
j
−
1
]
j \in [2, n], s_i[j] \le s_i[j-1]
j∈[2,n],si[j]≤si[j−1];
3.First, we must choose all the possible elements of
s
i
s_i
si from permutation a. If the index of
s
i
[
j
]
s_i[j]
si[j] in permutation a is
p
o
s
[
j
]
pos[j]
pos[j], for each
j
≥
2
j \ge 2
j≥2,
∣
p
o
s
[
j
]
−
p
o
s
[
j
−
1
]
∣
≤
k
|pos[j]-pos[j-1]|\le k
∣pos[j]−pos[j−1]∣≤k(
1
≤
k
≤
1
0
5
1 \le k \le 10^5
1≤k≤105). And for each
s
i
s_i
si, every element of
s
i
s_i
si must occur in a at most once.
4.After we choose all possible elements for
s
i
s_i
si, if the length of
s
i
s_i
si is smaller than nnn, the value of every undetermined element of
s
i
s_i
si is 0;
5.For each
s
i
s_i
si, we must make its weight high enough.
Consider two sequences C=[c1,c2,…cn] and
D
=
[
d
1
,
d
2
,
.
.
.
,
d
n
]
D=[d_1, d_2, ..., d_n]
D=[d1,d2,...,dn], we say the weight of C is higher than that of D if and only if there exists an integer k such that
1
≤
k
≤
n
1 \le k \le n
1≤k≤n,
c
i
=
d
i
c_i=d_i
ci=di for all
1
≤
i
<
k
1 \le i < k
1≤i<k, and
c
k
>
d
k
c_k > d_k
ck>dk.
If for each i ∈ [ 1 , n ] i \in [1,n] i∈[1,n], c i = d i c_i=d_i ci=di, the weight of C is equal to the weight of D.
For each
i
∈
[
1
,
n
]
i \in [1,n]
i∈[1,n], print the number of non-zero elements of
s
i
s_i
si separated by a space.
It’s guaranteed that there is only one possible answer.
Input:
There are multiple test cases.
The first line contains one integer
T
(
1
≤
T
≤
20
)
T(1 \le T \le 20)
T(1≤T≤20), denoting the number of test cases.
Each test case contains two lines, the first line contains two integers n and k (
1
≤
n
,
k
≤
1
0
5
1 \le n,k \le 10^5
1≤n,k≤105), the second line contains nnn distinct integers
a
1
,
a
2
,
.
.
.
,
a
n
a_1, a_2, ..., a_n
a1,a2,...,an(
1
≤
a
i
≤
n
1 \le a_i \le n
1≤ai≤n) separated by a space, which is the permutation a.
Output:
For each test case, print one line consists of n integers
∣
s
1
∣
,
∣
s
2
∣
,
.
.
.
,
∣
s
n
∣
|s_1|, |s_2|, ..., |s_n|
∣s1∣,∣s2∣,...,∣sn∣ separated by a space.
∣
s
i
∣
|s_i|
∣si∣ is the number of non-zero elements of sequence
s
i
s_i
si.There is no space at the end of the line.
思路
分析知 ∣ s i ∣ |s_i| ∣si∣与 i i i在 a i a_i ai周围的k个元素中 ≤ a i \le a_i ≤ai的元素的最大一个有关,利用set lower_bound二分查找(STL 中的set使用红黑树实现,插入、查询、删除的时间复杂度为 O ( l o g n ) \mathcal{O}(log n) O(logn)
代码实现
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+3;
int t,n,k;
int a[MAX];
int pre[MAX],res[MAX];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
set<int> s;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
int L=0, R=min(k, n);
for(int i=L;i<=R;i++){
s.insert(a[i]);
}
for(int i=0;i<n;i++){
if(L<i-k)s.erase(a[L++]);
if(R>=i+k)s.insert(a[++R]);
set<int>::iterator it=s.lower_bound(a[i]);
if(it==s.begin())pre[a[i]]=0;
else pre[a[i]]=*(--it);
}
res[0]=0;
for(int i=1;i<=n;i++){
res[i]=res[pre[i]]+1;
}
for(int i=1;i<=n;i++){
printf("%d",res[i]);
}
printf("\n");
}
}
参考链接:
https://blog.csdn.net/weixin_43667611/article/details/100527679
https://blog.csdn.net/qq_41157137/article/details/100185548