Codeforces 567F Mausoleum dp
考虑将数字从大到小一种一种填进去, 新加入的数字只会在之前组成序列的两侧, 所以形状一直是连续的一段。
dp[ k ][ i ][ j ] 表示填入了前 k 大的数字, 它们位于i - j 的合法方案数, 转移的时候check一下能否转移。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 700 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, k; LL dp[36][71][71]; int lh[101], rh[101], op[101]; char s[10]; bool check(int l, int r, int p1, int p2) { for(int i = 1; i <= k; i++) { if(lh[i] == rh[i]) continue; if(lh[i] == p1 && rh[i] == p2) { if(op[i] == 1 || op[i] == 2) return false; } else if(lh[i] == p1 || lh[i] == p2) { if(l <= rh[i] && rh[i] <= r) { if(op[i] != 1 && op[i] != 3) return false; } else { if(op[i] != 2 && op[i] != 4) return false; } } else if(rh[i] == p2 || rh[i] == p1) { if(l <= lh[i] && lh[i] <= r) { if(op[i] != 2 && op[i] != 4) return false; } else { if(op[i] != 1 && op[i] != 3) return false; } } else { if(rh[i] < l || lh[i] > r) continue; if(l <= lh[i] && rh[i] <= r) continue; if(lh[i] < l && rh[i] > r) continue; if(lh[i] < l) { if(op[i] != 1 && op[i] != 3) return false; } else { if(op[i] != 2 && op[i] != 4) return false; } } } return true; } int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= k; i++) { scanf("%d%s%d", &lh[i], s, &rh[i]); if(strlen(s) == 1) { if(s[0] == '=') op[i] = 0; else if(s[0] == '<') op[i] = 1; else if(s[0] == '>') op[i] = 2; } else { if(s[0] == '<') op[i] = 3; else if(s[0] == '>') op[i] = 4; } if(lh[i] > rh[i]) { if(op[i] == 1) op[i] = 2; else if(op[i] == 2) op[i] = 1; else if(op[i] == 3) op[i] = 4; else if(op[i] == 4) op[i] = 3; swap(lh[i], rh[i]); } } for(int i = 1; i < 2 * n; i++) { dp[n][i][i + 1] = 1; for(int j = 1; j <= k; j++) { if(lh[j] == i && rh[j] == i + 1 && (op[j] == 1 || op[j] == 2)) { dp[n][i][i + 1] = 0; break; } } } for(int k = n; k > 1; k--) { int len = (n - k + 1) * 2; for(int i = 1; i + len - 1 <= 2 * n; i++) { int j = i + len - 1; if(!dp[k][i][j]) continue; if(i >= 3 && check(i, j, i - 2, i - 1)) { dp[k - 1][i - 2][j] += dp[k][i][j]; } if(j <= 2 * n - 2 && check(i, j, j + 1, j + 2)) { dp[k - 1][i][j + 2] += dp[k][i][j]; } if(i >= 2 && j <= 2 * n - 1 && check(i, j, i - 1, j + 1)) { dp[k - 1][i - 1][j + 1] += dp[k][i][j]; } } } printf("%lld\n", dp[1][1][2 * n]); return 0; } /* */