codeforces B. Dreamoon Likes Permutations

在这里插入图片描述

题目

题意:

你有一个序列 a a ,现在你可以把 a a 分成两部分,但是要保证两个部分都有 1 1 ~ x x 中的每个整数都存在,且 l e n = x 1 + 1 len=x-1+1 也就是说每一部分数字唯一。

思路:

我们可以设置两个数组 q l , q r ql,qr ,从头开始扫如果这个点满足上述条件,那么就将 q l ql 置成 t r u e true ,然后 q r qr 从后往前扫,最后从前往后遍历一遍,如果出现了 q l i = t r u e q r i + 1 = t r u e ql_i=true且qr_{i+1}=true 的情况的那么就说明这个点满足前面符合条件和后面符合条件,然后存进 v e c t o r vector 最后输出就行了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 2e5 + 10;
int a[maxn], cnt[maxn], cntt[maxn];
bool ql[maxn], qr[maxn];
int main() {
    int t, n, x;
    read(t);
    while (t--) {
        bool book = false;
        memset(cnt, 0, sizeof(cnt));
        memset(ql, false, sizeof(ql));
        memset(qr, false, sizeof(qr));
        read(n);
        for (int i = 1; i <= n; i++) read(a[i]);
        int x = 1;
        for (int i = 1; i <= n; i++) {
            if (cnt[a[i]] == 0) cnt[a[i]]++;
            else break;
            while (cnt[x] == 1) x++;
            if (x - 1 == i) ql[i] = true;
        }
        memset(cnt, 0, sizeof(cnt));
        x = 1;
        for (int i = n; i >= 1; i--) {
            if (cnt[a[i]] == 0) cnt[a[i]]++;
            else break;
            while (cnt[x] == 1) x++;
            if (x - 1 == n - i + 1) qr[i] = true;
        }
        veci v;
        for (int i = 1; i <= n - 1; i++) {
            if (ql[i] && qr[i + 1]) v.push_back(i);
        }
        printf("%d\n", v.size());
        for (int i = 0; i < v.size(); i++) {
            printf("%d %d\n", v[i], n - v[i]);
        }
    }
    return 0;
}

发布了463 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_45031646/article/details/105309776