• 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;
    }
    
  • 相关阅读:
    file_get_contents高級用法
    php缓存技术常用函数
    php爬虫的两种思路
    jQuery判断文本框是否为空
    Asp.net 实现图片缩放 无水印(方法二)
    C# winform关于DataGridView的一些操作
    PHP判断访问者手机移动端还是PC端的函数
    本地图片显示在网页
    如何安装 Composer
    国内开源镜像站
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12000242.html
Copyright © 2020-2023  润新知