题目:Dreamoon Likes Permutations
题目链接:喵喵喵
题意:如果m个整数的序列恰好包含一次从1到m的所有整数,则称为置换。 数m称为排列的长度。
Dreamoon有两个长度分别为l1和l2的置换p1和p2。
现在,Dreoonoon将这两个置换连接成长度为l1 + l2的另一个序列a。 a的前l1个元素是排列p1,a的下一个l2个元素是排列p2。
给定序列a,您需要找到两个排列p1和p2。 如果有几种方法可以还原它们,则应找到所有它们。 (请注意,也有可能没有办法。)
HINT:特判l1与l2长度相等时的长度!
source code:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define speed(x) ios::sync_with_stdio(false),cin.tie(x),cout.tie(x)
#define bug(x) cout<<#x<<" == "<<x<<'\n';
using namespace std;
const int MAX_N=2e5+5;
int t,n;
#define P pair<int,int>
#define vec vector<int>
#define ump unordered_map<int,int>
vector<P>ans;
void solve(vec a,int maxn){
ump m;
bool flag=true;
for(int i=1;i<=maxn;i++) m[i]=0;
for(int i=0;i<maxn;i++) m[a[i]]=1;
for(int i=1;i<=maxn;i++){
if(!m[i]) flag=false;
}
m.clear();
for(int i=1;i<=a.size()-maxn;i++) m[i]=0;
for(int i=maxn;i<a.size();i++) m[a[i]]=1;
for(int i=1;i<=a.size()-maxn;i++){
if(!m[i]) flag=false;
}
if(flag) ans.emplace_back(P(maxn,a.size()-maxn));
}
int main(){
speed(nullptr);
cin>>t;
while(t--){
cin>>n;
vector<int>a(n);
int maxn=0;
ump m;
bool flag=true;
for(auto &i:a){
cin>>i;
maxn=max(i,maxn);
m[i]++;
if(m[i]>2) flag=false;
}
if(!flag){
cout<<"0"<<'\n';
continue;
}
ans.clear();
solve(a,maxn);//for(auto i:a) cout<<i<<' ';cout<<'\n';
//reverse(a.begin(),a.end());
//防止前后两端符合要求且长度相等导致重复计算
if(maxn*2!=n) solve(a,n-maxn);//for(auto i:a) cout<<i<<' ';cout<<'\n';
if(ans.size()){
cout<<ans.size()<<'\n';
for(auto i:ans) cout<<i.first<<' '<<i.second<<'\n';
}else cout<<"0\n";
}
return 0;
}