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.
题目大意:
这道题也有点难懂(对于自己),就是给了一个n长的数组,这个数组是由2个部分组成,分别是长度为L1的p1数组,以及长度为L2的L2数组,并且L1+L2=n,并且p1与p2是连续的(就是从1连续),那么题目要求我们还原n的2个部分的可能长度L1和L2.(题目存在无法还原现象)。
public static void main(String[] args) throws  IOException
{
 Scanner sc=new Scanner(System.in);
 int T=sc.nextInt();
 while(T–>0) {
  int n=sc.nextInt();
  int[] a=new int[n+1];
  Boolean[] used=new Boolean[n+1];
  Boolean[] left=new Boolean[n+1];
  Boolean[] right=new Boolean[n+1];
  Arrays.fill(used, false);
  Arrays.fill(left, false);
  Arrays.fill(right, false);
  for(int i=1;i<=n;i++) {
   a[i]=sc.nextInt();
  }
  int max=0;
  for(int i=1;i<=n;i++) {
   if(used[a[i]]) break;
   used[a[i]]=true;
   max=Math.max(max,a[i]);
   if(a[i]i) left[i]=true;
  }
  max=0;
  Arrays.fill(used, false);
  for(int i=n;i>=1;i–) {
   if(used[a[i]]) break;
   used[a[i]]=true;
   max=Math.max(max,a[i]);
   if(max
n-i+1) right[i]=true;
  }
  LinkedList link=new LinkedList<>();
  for(int i=1;i<=n;i++) {
   if(left[i]&&right[i+1]) link.add(i);
  }
  System.out.println(link.size());
  if(link.size()==0) continue;
  for(int i=0;i  }
}

你可能感兴趣的:(java)