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 ...
随机推荐
- HGOI20180904(NOIP2018模拟sxn出题)
sol 输入n和H表示n个人,选H个人gcd最大抓住排列,是x[1,n]的正整数,是连续的整数,假设现在最大的公因数是k其中k一定是在[1,n]那么在排列中最多出现的个数为w那么kw是最大的含有因数k ...
- dedecms添加文章时提示标题为空,编辑文章时编辑器空白的解决办法
dedecms添加文章时提示标题为空,编辑文章时编辑器空白的解决办法 dedecms出现这个问题与代码无关,主要是和PHP的版本有关,用的PHP5.4,更换成PHP5.2之后就不会有这个问题了. 问题 ...
- Centos7.4+openvpn-2.4.4+easy-rsa-3.0物理机安装教程
完整CentOS搭建OpenVPN服务环境图文教程 大福技术 关注 2016.02.17 09:28* 字数 3017 阅读 34000评论 18喜欢 21赞赏 3 对于OpenVPN环境有什么用途老 ...
- python中的BeautifulSoup使用小结
1.安装 pip install beautifulsoup4 2.代码文件中导入 from bs4 import BeautifulSoup 3. 解析器 使用方法 优势 劣势 Python标准库 ...
- BFC的个人理解
BFC是Block Formatting Context (块级格式化上下文)的缩写,是一个独立的渲染区域,这个东西的存在是为了隔绝一些内部子元素对外部元素的影响. 例如: 我们用overflow:h ...
- saltstack主机管理项目【day39】:主机管理项目开发
项目目标 salt state.apply -h "ubuntu,centos" -g "ubuntu,centos" -f "ubuntu,cent ...
- springSecurity入门小demo--配置文件xml的方式
本例子只是一个最最最简单的入门demo,重点讲解xml的配置参数的意思和遇到的坑,主要的功能有: 自定义登录页面,错误页面 配置角色 csrf-403报错解决方法(加上一行代码配置就ok) 后台ifr ...
- Sparrow.Chart.Wpf控件的动态调用
最近需要在Wpf程序中显示曲线,感觉Sparrow.Chart.Wpf控件不错(http://sparrowtoolkit.codeplex.com/),完全开源的一个控件支持,可以通过nuget下载 ...
- LFS、BLFS、ALFS、HLFS的区别
转自:http://www.ha97.com/3927.html Linux From Scratch (LFS) 及其后代代表一种新方法,向用户揭示 Linux 操作系统是如何工作的.LFS 基于这 ...
- long的变量后面没有L加会有什么后果
不加L的话,默认就是int型了. 当给long赋值一个超过int范围的值的时候,会出问题. java中对字面的数值是以int的形式来表示的 例如:long l= 6553555522222 报错:T ...