Dreamoon Likes Permutations

The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.

Dreamoon has two permutations p1 and p2 of non-zero lengths l1 and l2.

Now Dreamoon concatenates these two permutations into another sequence a of length l1+l2. First l1 elements of a is the permutation p1 and next l2 elements of a is the permutation p2.

You are given the sequence a, and you need to find two permutations p1 and p2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)

Input
The first line contains an integer t (1≤t≤10000) denoting the number of test cases in the input.

Each test case contains two lines. The first line contains one integer n (2≤n≤200000): the length of a. The second line contains n integers a1,a2,…,an (1≤ai≤n−1).

The total sum of n is less than 200000.

Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p1 and p2.

Each of the next k lines should contain two integers l1 and l2 (1≤l1,l2≤n,l1+l2=n), denoting, that it is possible to divide a into two permutations of length l1 and l2 (p1 is the first l1 elements of a, and p2 is the last l2 elements of a). You can print solutions in any order.

Example

input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
output
2
1 4
4 1
1
4 2
0
0
1
2 10
0

Note
In the first example, two possible ways to divide a into permutations are {1}+{4,3,2,1} and {1,4,3,2}+{1}.

In the second example, the only way to divide a into permutations is {2,4,1,3}+{2,1}.

In the third example, there are no possible ways.

#include <bits/stdc++.h>

#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 10;
int T, n;
int a[N];
bool v[N][2];
unordered_set<int> s1, s2;
vector<pii > ans;

int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        memset(v, false, sizeof(v));
        s1.clear(), s2.clear(), ans.clear();
        int mx1 = 0, mx2 = 0;
        for (int i = 1; i <= n; i++) {
            mx1 = max(mx1, a[i]);
            s1.insert(a[i]);
            if (s1.size() == i && mx1 == i)
                v[i][0] = true;
        }
        for (int i = n; i >= 1; i--) {
            mx2 = max(mx2, a[i]);
            s2.insert(a[i]);
            if (s2.size() == n - i + 1 && mx2 == n - i + 1)
                v[i][1] = true;
        }
        for (int i = 2; i <= n; i++)
            if (v[i - 1][0] && v[i][1])
                ans.emplace_back(i - 1, n - i + 1);
        printf("%d\n", ans.size());
        for (auto it:ans)
            printf("%d %d\n", it.fi, it.se);
    }
    return 0;
}
发布了360 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/105318838