2020牛客暑期多校训练营(第六场)——K K-Bag

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[500005],T,n,k,l;
bool f[500005],flag;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
    while(T--)
    {
    	cin>>n>>k;
		flag=1;
    	for(int i=1;i<=n;i++)
		{
			cin>>num[i];
			val[i]=num[i];
		}
    	for(int i=1;i<=n;i++)
    	{
    		if(num[i]>k)
    		{
				flag=0;
				break;
    		}
    	}
    	if(!flag)
    	{
			puts("NO");
			continue;
    	}
    	sort(val+1,val+n+1);
    	for(int i=1;i<=n;i++)
			num[i]=lower_bound(val+1,val+n+1,num[i])-val;
    	for(int i=1;i<=n;i++)
			cnt[i]=0;
		l=1;
		for(int i=1;i<=n;i++)
		{
			cnt[num[i]]++;
			while(cnt[num[i]]>1)
			{
				cnt[num[l]]--;
				l++;
			}
			lpos[i]=l;
		}
		flag=0;
		for(int i=1;i<=n;i++)
		{
			if(i<=k)
			{
				if(lpos[i]==1)
					f[i]=1;
				else
					f[i]=0;
			}
			else
			{
				if((lpos[i]<=i-k+1)&f[i-k])
					f[i]=1;
				else
					f[i]=0;
			}
			if(f[i]&&lpos[n]<=i+1)
			{
				flag=1;
				break;
			}
		}
		if(flag)
			puts("YES");
		else
			puts("NO");
    }
}
另附欧皇代码一份
#include<bits/stdc++.h>
int main()
{
    srand((unsigned)time(NULL));
    int T;
    scanf("%d",&T);
    while (T--) if (rand()%2) printf("YES\n");else printf("NO\n");
}

猜你喜欢

转载自blog.csdn.net/cxkdad/article/details/107620330