Arthur and Brackets

区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案。

然后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 = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int f[N][N];
int path[N][N]; int n, L[N], R[N];
char ans[N << ]; 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] <= && R[i] >= ;
f[i][j] = false;
for(int k = i; k <= j; k++) {
if( * (k - i) + < L[i] || * (k - i) + > R[i]) continue;
if(dp(i + , k) && dp(k + , 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 * + ] = ')';
print(i + , i + cnt, start + , path[i + ][i + cnt]);
print(i + cnt + , j, start + * cnt + , path[i + cnt + ][j]);
} int main() {
memset(f, -, sizeof(f));
scanf("%d", &n);
ans[ * n + ] = '\0';
for(int i = ; i <= n; i++) scanf("%d%d", &L[i], &R[i]);
if(!dp(, n)) puts("IMPOSSIBLE");
else {
print(, n, , path[][n]);
puts(ans + );
}
return ;
} /*
*/

Codeforces 508E Arthur and Brackets 区间dp的更多相关文章

  1. CodeForces 508E Arthur and Brackets 贪心

    题目: E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input s ...

  2. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  3. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  4. Brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3624   Accepted: 1879 Descript ...

  5. POJ2955:Brackets(区间DP)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  6. codeforce 149D Coloring Brackets 区间DP

    题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...

  7. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  8. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

  9. Code Forces 149DColoring Brackets(区间DP)

     Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. MT【188】一个正切余切有关的恒等式

    (2017北大特优)求$9\tan 10^\circ+2\tan 20^\circ+4\tan 40^\circ-\tan 80^\circ=$_____ A.$0$ B.$\dfrac{\sqrt ...

  2. BZOJ 3625: [Codeforces Round #250]小朋友和二叉树

    3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 304  Solved: 13 ...

  3. Python的生成器进阶玩法

    Python的生成器进阶玩法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.yield的表达式形式 #!/usr/bin/env python #_*_coding:utf-8 ...

  4. 在线Python学习网站

    目前我们使用的Python集成环境是Anaconda3,然后使用Jupyter Notebook和Spyder两个开发环境 Goole推出了在线的开发环境,在线网站: https://colab.re ...

  5. centos6.x下安装maven

    转自:http://www.centoscn.com/image-text/install/2014/0507/2923.html 1.下载maven包首先从官网上 http://maven.apac ...

  6. IDEA启动Tomcat报错1099 is already in use

    IDEA中启动Tomcat报错,Error running Tomcat7.0.52: Address localhost:1099 is already in use 或者是 java.rmi.se ...

  7. bzoj千题计划226:bzoj2763: [JLOI2011]飞行路线

    http://www.lydsy.com/JudgeOnline/problem.php?id=2763 这也算分层图最短路? dp[i][j]到城市i,还剩k次免费次数的最短路 #include&l ...

  8. shell 示例1 从1叠加到100

    从1叠加到100 echo $[$(echo +{..})] echo $[(+)*(/)] seq -s |bc

  9. 以python代码解释fork系统调用

    import os print('Process (%s) start...' % os.getpid()) # Only works on Unix/Linux/Mac: pid = os.fork ...

  10. vue路由DEMO

    index.js,index.vue,course.vue,master.vue等 import Vue from 'vue' import Router from 'vue-router' impo ...