C. The Third Problem(构造题)

 

 

 

 思路:给定一个数组a,问有多少个数组b满足对于任意的区间,a[l...r]和b[l...r]的mex值是相同的

如果一个区间的mex的值是x,说明0~x-1在该区间内都出现过。因此,我们可以从0到n-1开始枚举,记录每个i在数组的下标pos[i],对于每一个i,维护0~i-1的最左端点 l 和最右端点 r,如果pos[i]在l和r之间,i可以在区间[l, r]内任意移动,即res = res * (r - l + 1 - i),否则i只能在原来的位置不能更改。

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100010, mod = 1e9 + 7;
const int inf = 1e5+10;
int a[N], pos[N];
int n, t;
int main(){
    cin >> t;
    while(t--){
        cin >> n;
        for(int i=0;i<n;i++){
            cin >> a[i];
            pos[a[i]] = i;
        }
        ll res = 1;
        int l = inf, r = -inf;
        for(int i=0;i<n;i++){
            if(pos[i] > l && pos[i] < r)
                res = (res * (r - l + 1 - i)) % mod;
            l = min(l, pos[i]);
            r = max(r, pos[i]);
        }
        cout << res << endl;
    }
    return 0;
}

 

posted @ 2022-07-06 09:43  聊服一  阅读(37)  评论(0)    收藏  举报