有一個長度爲序列 \(a\),其中某些位置的值是 \(-1\)。dom
你要把 \(a\) 補成一個排列。spa
定義 \(b_i=\min(a_{2i-1},a_{2i})\),求有多少種可能的 \(b\)。code
\(n\leq 300\)get
若是 \(a_{2i-1}\) 和 \(a_{2i}\) 都有值,就把這兩個位置扔掉。string
記 \(c_i\) 表示 \(i\) 這個值是否在初始的 \(a\) 中。it
從後往前DP。記 \(f_{i,j,k}\) 表示已經處理完了 \(i\) 後面的數,有多少個 \(j>i,c_j=1\) 的數匹配的是 \(\leq i\) 的數,有多少個 \(j>i,c_j=0\) 的數匹配的是 \(\leq i\) 的數。io
若是 \(c_i=1\) 且日後匹配的是 \(c_j=0\),那麼方案數爲 \(1\)。(由於 \(\min=i\))function
若是 \(c_i=0\) 且日後匹配的是 \(c_j=0\),那麼先暫定方案數爲 \(1\)。(由於暫時不能肯定 \(i\) 填在哪一個位置。)記這種匹配對數爲 \(cnt\)。class
若是 \(c_i=0\) 且日後匹配的是 \(c_j=1\),那麼方案數爲 \(j\)。(由於能夠肯定 \(i\) 填在哪一個位置。)im
最後方案數要乘上 \(cnt!\),由於這些位置的 \(b\) 能夠隨便交換。
時間複雜度:\(O(n^3)\)
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<ctime> #include<functional> #include<cmath> #include<vector> #include<assert.h> //using namespace std; using std::min; using std::max; using std::swap; using std::sort; using std::reverse; using std::random_shuffle; using std::lower_bound; using std::upper_bound; using std::unique; using std::vector; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef std::pair<int,int> pii; typedef std::pair<ll,ll> pll; void open(const char *s){ #ifndef ONLINE_JUDGE char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout); #endif } void open2(const char *s){ #ifdef DEBUG char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout); #endif } int rd(){int s=0,c,b=0;while(((c=getchar())<'0'||c>'9')&&c!='-');if(c=='-'){c=getchar();b=1;}do{s=s*10+c-'0';}while((c=getchar())>='0'&&c<='9');return b?-s:s;} void put(int x){if(!x){putchar('0');return;}static int c[20];int t=0;while(x){c[++t]=x%10;x/=10;}while(t)putchar(c[t--]+'0');} int upmin(int &a,int b){if(b<a){a=b;return 1;}return 0;} int upmax(int &a,int b){if(b>a){a=b;return 1;}return 0;} const ll p=1000000007; const int N=310; void add(ll &a,ll b) { a=(a+b)%p; } int n; int a[2*N]; ll f[2*N][N][N]; int b[2*N]; int c[2*N]; int t; int main() { open2("f"); scanf("%d",&n); for(int i=1;i<=2*n;i++) scanf("%d",&a[i]); int cnt=0; for(int i=1;i<=2*n;i+=2) if(a[i]!=-1&&a[i+1]!=-1) b[a[i]]=b[a[i+1]]=2; else if(a[i]!=-1) b[a[i]]=1; else if(a[i+1]!=-1) b[a[i+1]]=1; else cnt++; for(int i=1;i<=2*n;i++) if(b[i]==1) c[++t]=1; else if(b[i]==0) c[++t]=2; f[t][0][0]=1; for(int i=t;i>=1;i--) for(int j=0;j<=t&&j<=n;j++) for(int k=0;k<=t&&k<=n;k++) if(c[i]==1) { add(f[i-1][j+1][k],f[i][j][k]); if(k) add(f[i-1][j][k-1],f[i][j][k]); } else { add(f[i-1][j][k+1],f[i][j][k]); if(k) add(f[i-1][j][k-1],f[i][j][k]); if(j) add(f[i-1][j-1][k],f[i][j][k]*j); } ll ans=f[0][0][0]; for(int i=1;i<=cnt;i++) ans=ans*i%p; printf("%lld\n",ans); return 0; }