codeforces Beautiful Numbers

来源:http://codeforces.com/problemset/problem/1265/B

B. Beautiful Numbers

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a permutation p=[p1,p2,…,pn]p=[p1,p2,…,pn] of integers from 11 to nn . Let‘s call the number mm (1≤m≤n1≤m≤n ) beautiful, if there exists two indices l,rl,r (1≤l≤r≤n1≤l≤r≤n ), such that the numbers [pl,pl+1,…,pr][pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m1,2,…,m .

For example, let p=[4,5,1,3,2,6]p=[4,5,1,3,2,6] . In this case, the numbers 1,3,5,61,3,5,6 are beautiful and 2,42,4 are not. It is because:

  • if l=3l=3 and r=3r=3 we will have a permutation [1][1] for m=1m=1 ;
  • if l=3l=3 and r=5r=5 we will have a permutation [1,3,2][1,3,2] for m=3m=3 ;
  • if l=1l=1 and r=5r=5 we will have a permutation [4,5,1,3,2][4,5,1,3,2] for m=5m=5 ;
  • if l=1l=1 and r=6r=6 we will have a permutation [4,5,1,3,2,6][4,5,1,3,2,6] for m=6m=6 ;
  • it is impossible to take some ll and rr , such that [pl,pl+1,…,pr][pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m1,2,…,m for m=2m=2 and for m=4m=4 .

You are given a permutation p=[p1,p2,…,pn]p=[p1,p2,…,pn] . For all mm (1≤m≤n1≤m≤n ) determine if it is a beautiful number or not.

Input

The first line contains the only integer tt (1≤t≤10001≤t≤1000 )  — the number of test cases in the input. The next lines contain the description of test cases.

The first line of a test case contains a number nn (1≤n≤2⋅1051≤n≤2⋅105 ) — the length of the given permutation pp . The next line contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n , all pipi are different) — the given permutation pp .

It is guaranteed, that the sum of nn from all test cases in the input doesn‘t exceed 2⋅1052⋅105 .

Output

Print tt lines — the answers to test cases in the order they are given in the input.

The answer to a test case is the string of length nn , there the ii -th character is equal to 11 if ii is a beautiful number and is equal to 00 if ii is not a beautiful number.

Example

Input

Copy

3
6
4 5 1 3 2 6
5
5 3 1 2 4
4
1 4 3 2

Output

Copy

101011
11111
1001

Note

The first test case is described in the problem statement.

In the second test case all numbers from 11 to 55 are beautiful:

  • if l=3l=3 and r=3r=3 we will have a permutation [1][1] for m=1m=1 ;
  • if l=3l=3 and r=4r=4 we will have a permutation [1,2][1,2] for m=2m=2 ;
  • if l=2l=2 and r=4r=4 we will have a permutation [3,1,2][3,1,2] for m=3m=3 ;
  • if l=2l=2 and r=5r=5 we will have a permutation [3,1,2,4][3,1,2,4] for m=4m=4 ;
  • if l=1l=1 and r=5r=5 we will have a permutation [5,3,1,2,4][5,3,1,2,4] for m=5m=5 .

 解题思路:记录每个值的位置,从1开始让最小的区间包围1-i,如果区间长度正好等于i就说明是一个i的排列。

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!=‘-‘&&(c<‘0‘||c>‘9‘))c=getchar();if(c==‘-‘)f=-1,c=getchar();while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar();return f*x;}
typedef unsigned long long ll;
const int maxn = 1e6+10;
int a[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int k;
        for(int i=1;i<=n;i++){
            cin>>k;
            a[k]=i;
        }
        int l,r;
        l=r=a[1];
        cout<<1;
        for(int i=2;i<=n;i++){
            l=min(l,a[i]);
            r=max(r,a[i]);
            if(r-l+1==i){
                cout<<1;
            }
            else{
                cout<<0;
            }
        }
        cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lipu123/p/12142233.html

时间: 2024-11-10 15:28:12

codeforces Beautiful Numbers的相关文章

CodeForces 55D Beautiful numbers 数位DP+数学

题意大概是,判断一个正整数区间内有多少个整数能被它自身的每一个非零的数字整除. 因为每一个位置上的整数集s = {0,1,2,3,4,5,6,7,8,9} lcm(s) = 2520 现在有一个整数t是由s中一个或者多个数字构成的,记为abcde,显然t = a*10^4+b*10^3+c*10^2+d*10^1+e 要使得t能被a,b,c,d,e整除,必然有t % lcm(a,b,c,d,e) = 0 因为a,b,c,d,e去重之后一定是s的一个子集,所以lcm(s)一定是lcm(a,b,c,

Codeforces 55D. Beautiful numbers(数位DP,离散化)

Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得只需要记录搜到当前位出现了哪些数字作为状态即可,明显是假算法...感觉这是一道数位DP好题.可以这样思考:一个数要想被其各位数字分别都整除,等价于它被那些数字的LCM整除.因此记录当前位,当前数对(1~9的LCM)取模的结果,当前出现的数字的LCM这三个值作为状态才合理,即dp[pos][sum][

Codeforces Beta Round #51---D. Beautiful numbers(数位dp, 巧妙)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

CodeForces 55D Beautiful numbers(数位dp&amp;&amp;离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers 题意 ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少. 求一个区间内的Beautiful numbers有多少个.Beautiful numbers指:一个数能整除所有组成它的非0数字. 例如15可以被1和5整除,所以15是Beautiful numbers. 思路 Beautiful numbers指:一个数能整除所有组成它的非0数字. 等同于 一个数能整除 所有组成它的

CodeForces 55D Beautiful numbers

Beautiful numbers 题目链接 Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just

CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+快速幂)

C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal

CodeForces 55D - Beautiful numbers - [数位DP+离散化]

题目链接:https://cn.vjudge.net/problem/CodeForces-55D Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with

CodeForces - 55D Beautiful numbers (数位DP)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

CodeForces - 55D - Beautiful numbers(数位DP,离散化)

链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with