2020牛客暑期多校训练营(第六场)——K K-Bag
题目描述
A sequence is called kk-bag, if and only if it is put in order by some (maybe one) permutations of 1 to k. For example, 1,2,3,2,1,3,3,2,1 is a valid 33-bag sequence.
Roundgod is not satisfied with k-bag, so she put forward part-k-bag, which is a contiguous subsequence of k-bag.
Wcy wants to know if the sequence of length nn is a part-k-bag sequence.
输入描述
The first line contains one integer T (1≤T≤20), denoting the number of test cases. Then T test cases follow.
The first line of each test case contains two integers n,k (1≤n≤5*105,1≤k≤109).
The second line of each test case contains nn integers indicate the sequence.
It is guaranteed that ∑n≤2⋅106, the values of the sequence are between 11 and 109.
输出描述
One line of each test case, if the sequence is a part-k-bag sequence, print “YES”, otherwise print “NO”.
样例输入
1
8 3
2 3 2 1 3 3 2 1
样例输出
YES
题意
当一个数列可以表示为若干个1到k的排列依次组成时,这个数列被称为k-bag。例如1,2,3,2,1,3,3,2,1是一个3-bag。
如果一个序列是一个k-bag的连续子串,则其称为part-k-bag。
求一个长度为n的序列是否是一个part-k-bag。
第一行包含一个整数T(1≤T≤20),表示测试用例的数量。
然后是T个样例。每个测试案例的第一行包含两个整数n,k(1≤n≤5⋅105,1≤k≤109)。
每个测试案例的第二行包含n个整数表示序列。保证∑n≤2⋅106,序列的值在1到109之间。
如果一个序列是部分k-bag序列,则打印“YES”,否则打印“NO”。
题解
AC代码
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int num[500005],val[500005],cnt[500005],lpos[5