D. Rating Compression
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers aa of length nn. You are now updating the infrastructure, so you've created a program to compress these graphs.
The program works as follows. Given an integer parameter kk, the program takes the minimum of each contiguous subarray of length kk in aa.
More formally, for an array aa of length nn and an integer kk, define the kk-compression array of aa as an array bb of length n−k+1n−k+1, such that
bj=minj≤i≤j+k−1aibj=minj≤i≤j+k−1ai
For example, the 33-compression array of [1,3,4,5,2][1,3,4,5,2] is [min{1,3,4},min{3,4,5},min{4,5,2}]=[1,3,2].[min{1,3,4},min{3,4,5},min{4,5,2}]=[1,3,2].
A permutation of length mm is an array consisting of mm distinct integers from 11 to mm in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (m=3m=3 but there is 44 in the array).
A kk-compression array will make CodeCook users happy if it will be a permutation. Given an array aa, determine for all 1≤k≤n1≤k≤n if CodeCook users will be happy after a kk-compression of this array or not.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of the description of each test case contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the length of the array.
The second line of the description of each test case contains nn integers a1,…,ana1,…,an (1≤ai≤n1≤ai≤n) — the elements of the array.
It is guaranteed, that the sum of nn for all test cases does not exceed 3⋅1053⋅105.
Output
For each test case, print a binary string of length nn.
The kk-th character of the string should be 11 if CodeCook users will be happy after a kk-compression of the array aa, and 00 otherwise.
Example
input
Copy
5 5 1 5 3 4 2 4 1 3 2 1 5 1 3 3 3 2 10 1 2 3 4 5 6 7 8 9 10 3 3 3 2
output
Copy
10111 0001 00111 1111111111 000
Note
In the first test case, a=[1,5,3,4,2]a=[1,5,3,4,2].
- The 11-compression of aa is [1,5,3,4,2][1,5,3,4,2] and it is a permutation.
- The 22-compression of aa is [1,3,3,2][1,3,3,2] and it is not a permutation, since 33 appears twice.
- The 33-compression of aa is [1,3,2][1,3,2] and it is a permutation.
- The 44-compression of aa is [1,2][1,2] and it is a permutation.
- The 55-compression of aa is [1][1] and it is a permutation.
=========================================================================
首先如果要出现 k的长度所构成的是一个排列,那么势必包含1- n-k+1,这些数字被包含的条件就是,1 --- n-k+1 的数字都在k长度区间里面做了一次最小值, 所以如果我们要求长度为k区间构成的排列,应该先预处理出来1 -- n-k+1的数字,看看是不是都满足情况。这样我们先利用单调栈来维护每个数字所支配的区间,对这个数字维护的区间长度取最大值。
再对全部数字维护区间的长度取最小值,这样对于1--pos, 如果其前缀最小值大于等于n-pos+1,那么k= n-pos+1这个长度是可以输出1的
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
stack<int>st;
int a[300000+10],l[300000+10],r[300000+10],pre[300000+10];
vector<int>ans;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
pre[i]=0;
}
while(!st.empty())
st.pop();
for(int i=1;i<=n;i++)
{
while(!st.empty()&&a[st.top()]>=a[i])
st.pop();
if(!st.empty())
l[i]=st.top();
else
l[i]=0;
st.push(i);
}
while(!st.empty())
st.pop();
for(int i=n;i>=1;i--)
{
while(!st.empty()&&a[st.top()]>=a[i])
st.pop();
if(!st.empty())
r[i]=st.top();
else
r[i]=n+1;
st.push(i);
}
for(int i=1;i<=n;i++)
{
pre[a[i]]=max(pre[a[i]],r[i]-l[i]-1);
}
pre[0]=1e9;
ans.clear();
for(int i=1;i<=n;i++)
{
pre[i]=min(pre[i-1],pre[i]);
if(pre[i]>=n-i+1)
ans.push_back(1);
else
ans.push_back(0);
}
for(int i=ans.size()-1;i>=0;i--)
cout<<ans[i];
cout<<endl;
}
return 0;
}