其他分享
首页 > 其他分享> > The Third Problem

The Third Problem

作者:互联网

You are given a permutation a1,a2,…,ana1,a2,…,an of integers from 00 to n−1n−1. Your task is to find how many permutations b1,b2,…,bnb1,b2,…,bn are similar to permutation aa.

Two permutations aa and bb of size nn are considered similar if for all intervals [l,r][l,r] (1≤l≤r≤n1≤l≤r≤n), the following condition is satisfied:

MEX([al,al+1,…,ar])=MEX([bl,bl+1,…,br]),MEX⁡([al,al+1,…,ar])=MEX⁡([bl,bl+1,…,br]),

where the MEXMEX of a collection of integers c1,c2,…,ckc1,c2,…,ck is defined as the smallest non-negative integer xx which does not occur in collection cc. For example, MEX([1,2,3,4,5])=0MEX⁡([1,2,3,4,5])=0, and MEX([0,1,2,4,5])=3MEX⁡([0,1,2,4,5])=3.

Since the total number of such permutations can be very large, you will have to print its remainder modulo 109+7109+7.

In this problem, a permutation of size nn is an array consisting of nn distinct integers from 00 to n−1n−1 in arbitrary order. For example, [1,0,2,4,3][1,0,2,4,3] is a permutation, while [0,1,1][0,1,1] is not, since 11 appears twice in the array. [0,1,3][0,1,3] is also not a permutation, since n=3n=3 and there is a 33 in the array.

Input

Each test contains multiple test cases. The first line of input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. The following lines contain the descriptions of the test cases.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the size of permutation aa.

The second line of each test case contains nn distinct integers a1,a2,…,ana1,a2,…,an (0≤ai<n0≤ai<n) — the elements of permutation aa.

It is guaranteed that the sum of nn across all test cases does not exceed 105105.

Output

For each test case, print a single integer, the number of permutations similar to permutation aa, taken modulo 109+7109+7.

题目大意:给出一个0到n-1的排列,问有多少个0到n-1的全排列满足与原序列对应的任意字串的mex值相等,mex值为区间内未出现的最小非负整数。

思路:容易观察发现,0和1的位置是固定的,假设1在0右边,则有三种情况:1. 2在1右边,2.2在0左边,3.2在1与2中间。可以发现,因为0到1的mex是固定的,所以情况一与情况二的2便固定,考虑3的时候便要3在考虑0到2或者2到1(指的是原序列的地址)的情况,而情况三2便可以放在0到1之间任意空位。依次更新即可。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

#define int long long
const int maxn=1e5+5,mod=1e9+7;
int t,n,a[maxn];

void solve() {
	cin>>n;
	for(int i=1;i<=n;i++) {
		int x;cin>>x;
		a[x]=i;
	}
	int l=a[0],r=a[0];
	int ans=1;
	for(int i=1;i<n;i++) {
		if(a[i]>r) r=a[i];
		else if(a[i]<l) l=a[i];
		else ans= (ans*(r-l-i+1))%mod;
	}
	cout<<ans<<'\n';
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--) {
		solve();
	}
}

 

标签:nn,Third,int,permutation,test,Problem,include,MEX
来源: https://www.cnblogs.com/cbmango/p/16445463.html