Codeforces Round #631 (Div. 2) B. Dreamoon Likes Permutations(排列组合)

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

Dreamoon has two permutations p1p1 and p2p2 of non-zero lengths l1l1 and l2l2 .

Now Dreamoon concatenates these two permutations into another sequence aa of length l1+l2l1+l2 . First l1l1 elements of aa is the permutation p1p1 and next l2l2 elements of aa is the permutation p2p2 .

You are given the sequence aa , and you need to find two permutations p1p1 and p2p2 . 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 tt (1≤t≤100001≤t≤10000 ) denoting the number of test cases in the input.

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

The total sum of nn is less than 200000200000 .

Output

For each test case, the first line of output should contain one integer kk : the number of ways to divide aa into permutations p1p1 and p2p2 .

Each of the next kk lines should contain two integers l1l1 and l2l2 (1≤l1,l2≤n,l1+l2=n1≤l1,l2≤n,l1+l2=n ), denoting, that it is possible to divide aa into two permutations of length l1l1 and l2l2 (p1p1 is the first l1l1 elements of aa , and p2p2 is the last l2l2 elements of aa ). You can print solutions in any order.

Example

Input

Copy

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

Copy

2
1 4
4 1
1
4 2
0
0
1
2 10
0wtcl,被细节坑到死==思路比较好想,因为是全排列,所以这个数列最大的数肯定是较长的那个全排列的一端,所以两个全排列的长度分别为mmax和n-mmax,然后分别看1~mmax和mmax+1~n能否组成两个全排列,1~n-mmax和n-mmax+1~n能否组成两个全排列即可。坑点1:有可能两个全排列完全一样,只需要输出一个即可。坑点2:(主要是我脑残)0的阶乘是1,是合法的!所以输入5  1 2 3 4 5会输出:20 55 0
#include <bits/stdc++.h>
using namespace std;
int n;
int ori[200005];
bool ok(bool vis[],int n)
{
    int cnt=0,i;
    for(i=1;i<=n;i++)if(!vis[i])return 0;
    return 1;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int i;
        int mmax=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&ori[i]);
            mmax=max(mmax,ori[i]);
        }
        bool vis1[200005]={0};
        bool vis2[200005]={0};
        int ans=0,l1=0,l2=0,l3=0,l4=0;
        for(i=1;i<=n-mmax;i++)vis1[ori[i]]=1;
        for(i=n-mmax+1;i<=n;i++)vis2[ori[i]]=1;
        if(ok(vis1,n-mmax)&&ok(vis2,mmax))
        {
            ans++;
            l1=n-mmax,l2=mmax;
        }
        bool vis3[200005]={0};
        bool vis4[200005]={0};
        for(i=1;i<=mmax;i++)vis3[ori[i]]=1;
        for(i=mmax+1;i<=n;i++)vis4[ori[i]]=1;
        if(ok(vis3,mmax)&&ok(vis4,n-mmax)&&mmax*2!=n)//如果两个是一摸一样的全排列 只输出一个就行
        {
            ans++;
            l3=mmax,l4=n-mmax;
        }

        cout<<ans<<endl;
        if(l1||l2)cout<<l1<<‘ ‘<<l2<<endl;//不能用&& 因为0!=1 有可能有0 5和5 0这种情况
        if(l3||l4)cout<<l3<<‘ ‘<<l4<<endl;

    }
    return 0;
}


原文地址:https://www.cnblogs.com/lipoicyclic/p/12631383.html

时间: 2024-11-08 18:25:12

Codeforces Round #631 (Div. 2) B. Dreamoon Likes Permutations(排列组合)的相关文章

Codeforces Round #631 (Div. 2) D.Dreamoon Likes Sequences

题目连接:Dreamoon Likes Sequences  题意:给你d和m,让你构造一个递增数组a,使数组b(i==1,b[i]=a[i] ; i>1, b[i]=b[i-1]^a[i])递增,求a有几种,答案模m. 题解:根据异或的性质可以得出:2后边不能有3, 4后边不能有5~7, 8后边不能有9~15...... 然后就很好写了.用数组b记录第i个数可以取得数有多少个,数组dp记录长度为 i 的 a 数组有几种.看下边的代码应该就清楚了. 1 #include<bits/stdc++

Codeforces Round #631 (Div. 2) C. Dreamoon Likes Coloring(贪心好题/意识流题解)

Dreamoon likes coloring cells very much. There is a row of nn cells. Initially, all cells are empty (don't contain any color). Cells are numbered from 11 to nn . You are given an integer mm and mm integers l1,l2,…,lml1,l2,…,lm (1≤li≤n1≤li≤n ) Dreamoo

Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (超几何分布)

题目链接:Codeforces Round #273 (Div. 2) B. Dreamoon and WiFi 题意:"+"表示前进1个单位,"-"表示后退1个单位,问以0为起点经过S1,S2两个命令后达到的位置相同的概率. 思路:统计"+"和"-"的数量.如果S2中的"+"或者"-"比S1中的多,概率是0.其他条件下,形成的是超几何分布. AC代码: #include <std

Codeforces Round #485 (Div. 2) E. Petr and Permutations

Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/problem/E Description Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to gene

Codeforces Round #272 (Div. 2) D.Dreamoon and Sets 找规律

D. Dreamoon and Sets Dreamoon likes to play with sets, integers and .  is defined as the largest positive integer that divides both a and b. Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for

Codeforces Round #631 (Div. 2) Dreamoon Likes Sequences

题面很短,别的博客也讲了就不说题意了. 做法: 异或是没有进位的加法,所以ai + 1的二进制最高位要大于ai的二进制最高位,才能满足ai递增,bi也递增的条件.呐这样的话,选了4,(5,6,7)就都不能选了,只能选比7大的数. 这样分析下来a数组最长也只有30,(2^30>1e9) 直接按照数字大小dp会TLE 思路角度1:换一个角度,我们把二进制最高位相同的看作一组,因为这一组内只能选一个数. 有点像分组背包.但是我们现在只看分组背包的方案数,所以就不用枚举每一组内的物品了. dpij表示考

Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)

题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer number in range[1, a

Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式

C. Dreamoon and Sums Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer numb

Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)

题目链接 这个题取模的时候挺坑的!!! 题意:div(x , b) / mod(x , b) = k( 1 <= k <= a).求x的和 分析: 我们知道mod(x % b)的取值范围为 1  - (b-1).那么我们可以从这一点入口来进行解题.. mod (x, b) = 1 时, x  =  b + 1, 2b + 1, 3b + 1..... a * b + 1. mod (x , b) = 2 时, x =  2b + 2, 4b + 2, 6b + 2, ..... 2a * b