Codeforces 508E Arthur and Brackets 区间dp
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 508E Arthur and Brackets 区间dp
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Arthur and Brackets
區(qū)間dp, dp[ i ][ j ]表示第 i 個(gè)括號(hào)到第 j 個(gè)括號(hào)之間的所有括號(hào)能不能形成一個(gè)合法方案。
然后dp就完事了。
#include<bits/stdc++.h> #define LL 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 ull unsigned long long using namespace std;const int N = 600 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8;int f[N][N]; int path[N][N];int n, L[N], R[N]; char ans[N << 1];int dp(int i, int j) {if(i > j) return true;if(~f[i][j]) return f[i][j];if(i == j) return L[i] <= 1 && R[i] >= 1;f[i][j] = false;for(int k = i; k <= j; k++) {if(2 * (k - i) + 1 < L[i] || 2 * (k - i) + 1 > R[i]) continue;if(dp(i + 1, k) && dp(k + 1, j)) {f[i][j] = true;path[i][j] = k - i;break;}}return f[i][j]; }void print(int i, int j, int start, int cnt) {if(i > j) return;ans[start] = '(';ans[start + cnt * 2 + 1] = ')';print(i + 1, i + cnt, start + 1, path[i + 1][i + cnt]);print(i + cnt + 1, j, start + 2 * cnt + 2, path[i + cnt + 1][j]); }int main() {memset(f, -1, sizeof(f));scanf("%d", &n);ans[2 * n + 1] = '\0';for(int i = 1; i <= n; i++) scanf("%d%d", &L[i], &R[i]);if(!dp(1, n)) puts("IMPOSSIBLE");else {print(1, n, 1, path[1][n]);puts(ans + 1);}return 0; }/* */?
??
轉(zhuǎn)載于:https://www.cnblogs.com/CJLHY/p/10391579.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces 508E Arthur and Brackets 区间dp的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java!越来越像Kotlin了!!
- 下一篇: sort