• 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;
    }
    
  • 相关阅读:
    384. 最长无重复字符的子串
    406. 和大于S的最小子数组
    159. 寻找旋转排序数组中的最小值
    62. 搜索旋转排序数组
    20. 骰子求和
    125. 背包问题 II
    92. 背包问题
    1295. 质因数统计
    471. 最高频的K个单词
    1339. 最大区间
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12000242.html
Copyright © 2020-2023  润新知