Arthur and Brackets

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

然后dp就完事了。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define PLL pair<LL, LL>
  7. #define PLI pair<LL, int>
  8. #define PII pair<int, int>
  9. #define SZ(x) ((int)x.size())
  10. #define ull unsigned long long
  11. using namespace std;
  12.  
  13. const int N = + ;
  14. const int inf = 0x3f3f3f3f;
  15. const LL INF = 0x3f3f3f3f3f3f3f3f;
  16. const int mod = 1e9 + ;
  17. const double eps = 1e-;
  18.  
  19. int f[N][N];
  20. int path[N][N];
  21.  
  22. int n, L[N], R[N];
  23. char ans[N << ];
  24.  
  25. int dp(int i, int j) {
  26. if(i > j) return true;
  27. if(~f[i][j]) return f[i][j];
  28. if(i == j) return L[i] <= && R[i] >= ;
  29. f[i][j] = false;
  30. for(int k = i; k <= j; k++) {
  31. if( * (k - i) + < L[i] || * (k - i) + > R[i]) continue;
  32. if(dp(i + , k) && dp(k + , j)) {
  33. f[i][j] = true;
  34. path[i][j] = k - i;
  35. break;
  36. }
  37. }
  38. return f[i][j];
  39. }
  40.  
  41. void print(int i, int j, int start, int cnt) {
  42. if(i > j) return;
  43. ans[start] = '(';
  44. ans[start + cnt * + ] = ')';
  45. print(i + , i + cnt, start + , path[i + ][i + cnt]);
  46. print(i + cnt + , j, start + * cnt + , path[i + cnt + ][j]);
  47. }
  48.  
  49. int main() {
  50. memset(f, -, sizeof(f));
  51. scanf("%d", &n);
  52. ans[ * n + ] = '\0';
  53. for(int i = ; i <= n; i++) scanf("%d%d", &L[i], &R[i]);
  54. if(!dp(, n)) puts("IMPOSSIBLE");
  55. else {
  56. print(, n, , path[][n]);
  57. puts(ans + );
  58. }
  59. return ;
  60. }
  61.  
  62. /*
  63. */

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. HGOI20180904(NOIP2018模拟sxn出题)

    sol 输入n和H表示n个人,选H个人gcd最大抓住排列,是x[1,n]的正整数,是连续的整数,假设现在最大的公因数是k其中k一定是在[1,n]那么在排列中最多出现的个数为w那么kw是最大的含有因数k ...

  2. dedecms添加文章时提示标题为空,编辑文章时编辑器空白的解决办法

    dedecms添加文章时提示标题为空,编辑文章时编辑器空白的解决办法 dedecms出现这个问题与代码无关,主要是和PHP的版本有关,用的PHP5.4,更换成PHP5.2之后就不会有这个问题了. 问题 ...

  3. Centos7.4+openvpn-2.4.4+easy-rsa-3.0物理机安装教程

    完整CentOS搭建OpenVPN服务环境图文教程 大福技术 关注 2016.02.17 09:28* 字数 3017 阅读 34000评论 18喜欢 21赞赏 3 对于OpenVPN环境有什么用途老 ...

  4. python中的BeautifulSoup使用小结

    1.安装 pip install beautifulsoup4 2.代码文件中导入 from bs4 import BeautifulSoup 3. 解析器 使用方法 优势 劣势 Python标准库 ...

  5. BFC的个人理解

    BFC是Block Formatting Context (块级格式化上下文)的缩写,是一个独立的渲染区域,这个东西的存在是为了隔绝一些内部子元素对外部元素的影响. 例如: 我们用overflow:h ...

  6. saltstack主机管理项目【day39】:主机管理项目开发

    项目目标 salt state.apply -h "ubuntu,centos" -g "ubuntu,centos" -f "ubuntu,cent ...

  7. springSecurity入门小demo--配置文件xml的方式

    本例子只是一个最最最简单的入门demo,重点讲解xml的配置参数的意思和遇到的坑,主要的功能有: 自定义登录页面,错误页面 配置角色 csrf-403报错解决方法(加上一行代码配置就ok) 后台ifr ...

  8. Sparrow.Chart.Wpf控件的动态调用

    最近需要在Wpf程序中显示曲线,感觉Sparrow.Chart.Wpf控件不错(http://sparrowtoolkit.codeplex.com/),完全开源的一个控件支持,可以通过nuget下载 ...

  9. LFS、BLFS、ALFS、HLFS的区别

    转自:http://www.ha97.com/3927.html Linux From Scratch (LFS) 及其后代代表一种新方法,向用户揭示 Linux 操作系统是如何工作的.LFS 基于这 ...

  10. long的变量后面没有L加会有什么后果

    不加L的话,默认就是int型了. 当给long赋值一个超过int范围的值的时候,会出问题. java中对字面的数值是以int的形式来表示的  例如:long l= 6553555522222 报错:T ...