D. Rating Compression - 单调栈 前缀和

Problem - D - Codeforces

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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值