B.Beautiful Numbers

题意:你被给予了一个序列 p = [p1, p2, ..., pn](1 ~ n的整数),如果存在l, r左右端点(1 <= l <= r <= n),使得[pl, pl+1,..., pr]是一个1到m的序列,我们就称它为漂亮的。

分析:意思是说如果存在两个端点,里面的数字可以构成1到m且是连续的,那么就称m为漂亮的,标记为1,否则标记为0。
比如[4, 5, 1, 3, 2, 6]的漂亮数字序列是[1, 0, 1, 0, 1, 1]

分析:这个数字m是漂亮的,等价于Posmax - Posmin + 1 = m,Posmax和Posmin意思是说1...m的序列中某个数字所处的最大位置和最小位置且它们之间的距离必须是等于m且连续的
对于每个数字m,我们会采取一种方式更新Posmax, Posmin,假设Posi是一个数字在序列p中的位置,当i == 1时,我们有Posmax = Posmin = Pos1,当m > 1时,Posmax和Posmin可以被如下的公式更新
new_posmax = max(old_posmax, posm);
new_posmin = min(old_posmin, posm);

代码如下:

#include <bits/stdc++.h>

using namespace std;

const int M = 2e5 + 239;

int n, p[M], x;

void solve()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        p[x - 1] = i;
    }
    int l = n;
    int r = 0;
    string ans = "";
    for (int i = 0; i < n; i++)
    {
        l = min(l, p[i]);
        r = max(r, p[i]);
        if (r - l == i)
            ans += '1';
        else
            ans += '0';
    }
    cout << ans << "\n";
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/pixel-Teee/p/12043824.html