B. Dreamoon Likes Permutations

194 篇文章 1 订阅
13 篇文章 0 订阅
给定一个序列aa,由两个非零长度的排列p1和p2拼接而成,需要找到这两个排列。输出可能的划分方式及其长度。
摘要由CSDN通过智能技术生成

链接:https://codeforces.ml/contest/1330/problem/B

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
0

Note

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

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

In the third example, there are no possible ways.

代码:

#include<bits/stdc++.h>
using namespace std;
long long n,h,t,k,x,s,min1,max1,ans;
long long a[200001];
long long l[200001],r[200001];
map<long long,long long>m;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		m.clear();
		ans=0;
		min1=1,max1=n;
		int flag=1;
		k=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			m[a[i]]++;
		}
		int j=1;
		while(m[j]==2)
		{
			j++;
		}
		s=j-1;
		while(m[j]==1)
		{
			j++;
		}
		k=j-1;
		if(s+k!=n)
		cout<<0<<endl;
		else
		{
			ans=0;
			m.clear();
			flag=1;
			for(int i=1;i<=s;i++)
			{
				m[a[i]]++;
			}
			for(int i=1;i<=s;i++)
			{
				if(m[i]==1)
				continue;
				else
				{
					flag=0;
					break;
				}
			}
			m.clear();
			for(int i=s+1;i<=n;i++)
			{
				m[a[i]]++;
			}
			for(int i=1;i<=k;i++)
			{
				if(m[i]==1)
				continue;
				else
				{
					flag=0;
					break;
				}
			}
			if(flag==1)
			{
				ans++;
				l[ans]=s;
			}
			flag=1;
			m.clear();
			for(int i=1;i<=k;i++)
			{
				m[a[i]]++;
			}
			for(int i=1;i<=k;i++)
			{
				if(m[i]==1)
				continue;
				else
				{
					flag=0;
					break;
				}
			}
			m.clear();
			for(int i=k+1;i<=n;i++)
			{
				m[a[i]]++;
			}
			for(int i=1;i<=s;i++)
			{
				if(m[i]==1)
				continue;
				else
				{
					flag=0;
					break;
				}
			}
			if(flag==1&&s!=k)
			{
				ans++;
				l[ans]=k;
			}
			cout<<ans<<endl;
			for(int i=1;i<=ans;i++)
			cout<<l[i]<<' '<<n-l[i]<<endl;
		}
		
	}
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值