Codeforces 508E Arthur and Brackets 区间dp
区间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的更多相关文章
- CodeForces 508E Arthur and Brackets 贪心
题目: E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input s ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- Brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3624 Accepted: 1879 Descript ...
- POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- codeforce 149D Coloring Brackets 区间DP
题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- Code Forces 149DColoring Brackets(区间DP)
Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- wamp安装失败原因大全
wamp 是 Windos.Apache.Mysql.PHP集成安装环境 为了安装hdwiki 所以需要这个环境 1.下载wampserver_x86_3.0.6 64位 环境包,安装路径禁止有空格 ...
- 【刷题】BZOJ 1487 [HNOI2009]无归岛
Description Neverland是个神奇的地方,它由一些岛屿环形排列组成,每个岛上都生活着之中与众不同的物种.但是这些物种都有一个共同的生活习性:对于同一个岛上的任意两个生物,他们有且仅有一 ...
- luogu1540 [NOIp2011]机器翻译 (队列)
#include<bits/stdc++.h> #define pa pair<int,int> #define CLR(a,x) memset(a,x,sizeof(a)) ...
- Mac 显示sudo: pip: command not found
Mac显示sudo: pip: command not found mac在安装完pip模块后,使用pip命令会提示sudo: pip: command not found moyanzhudeMac ...
- Easyui的DateBox日期格式化
http://www.cnblogs.com/wintalen/archive/2011/06/10/2077171.html DateBox 日期显示默认的格式为“dd/mm/yyyy”,如果想自定 ...
- Windows的DOS命令基础
Windows的DOS命令基础 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1.dir: 查看当前路径下的目录(directory)详细信息 . 详细信息: a>.dir ...
- 【DS】排序算法之希尔排序(Shell Sort)
一.算法思想 希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本.希尔排序是非稳定排序算法.希尔排序是基于插入排序的以下两点性质而提出改进方法的:1)插入排序在对几乎已经排好序的数据操作 ...
- 何凯文每日一句打卡||DAY14
- tensorflow随机张量创建
TensorFlow 有几个操作用来创建不同分布的随机张量.注意随机操作是有状态的,并在每次评估时创建新的随机值. 下面是一些相关的函数的介绍: tf.random_normal 从正态分布中输出随机 ...
- MySQL binlog导入失败
一个同事问我,说他用innobackupex恢复数据后用mysqlbinlog导入增量数据时,发现数据没有导入进去并且也没有报错. mysqlbinlog /u01/mysql_py/database ...