I - Interesting Permutation Gym - 102394I
Gym - 102394I
如果h[i]==h[i-1],那么它只能选择最大值和最小值直接的位置,假设为tot
如果h[i]>h[i-1],那么当前位置为最大值或者最小值,ans*=2,差值也变大了,tot+=h[i]-h[i-1]-1;
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //2019.11.9 //by war using namespace std; long long T,n,ans,tot,Mod=1e9+7; long long a[N]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } signed main(){ in(T); while(T--){ in(n); ans=1;tot=0; For(i,1,n){ in(a[i]); if(a[i]>=n) ans=0; } if(a[1]!=0) ans=0; For(i,2,n){ if(a[i]==a[i-1]){ ans*=tot--; ans%=Mod; } else if(a[i]>a[i-1]) ans<<=1,tot+=a[i]-a[i-1]-1,ans%=Mod; else { ans=0;break; } } o(ans);p(' '); } return 0; }