Codeforces Round #604 (Div. 2) B. Beautiful Numbers

链接:

https://codeforces.com/contest/1265/problem/B

题意:

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

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

if l=3 and r=3 we will have a permutation [1] for m=1;
if l=3 and r=5 we will have a permutation [1,3,2] for m=3;
if l=1 and r=5 we will have a permutation [4,5,1,3,2] for m=5;
if l=1 and r=6 we will have a permutation [4,5,1,3,2,6] for m=6;
it is impossible to take some l and r, such that [pl,pl+1,…,pr] is a permutation of numbers 1,2,…,m for m=2 and for m=4.
You are given a permutation p=[p1,p2,…,pn]. For all m (1≤m≤n) determine if it is a beautiful number or not.

思路:

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

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
 
int p[MAXN];
int n;
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        int a;
        for (int i = 1;i <= n;i++)
            cin >> a, p[a] = i;
        int l, r;
        l = r = p[1];
        cout << 1;
        for (int i = 2;i <= n;i++)
        {
            l = min(l, p[i]);
            r = max(r, p[i]);
            if (r-l+1 == i)
                cout << 1;
            else
                cout << 0;
        }
        cout << endl;
    }
 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/12000242.html