大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列.

最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e6+10;
int n, l[N], r[N], len[N], vis[N];
char s[N];
int fa[N];
int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);} int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d%d", l+i,r+i);
REP(i,1,n+4) fa[i]=i;
if (l[n]>1) return puts("IMPOSSIBLE"),0;
len[n] = 2;
PER(i,1,n-1) {
int t = 1;
if (l[i]==1) {len[i]=2;continue;}
for (int j=find(i+1); j<=n; j=find(j)) {
t += len[j];
fa[j] = j+1;
if (l[i]<=t&&t<=r[i]) {
len[i] = t+1;
break;
}
}
if (!len[i]) return puts("IMPOSSIBLE"),0;
}
int now = 1;
REP(i,1,n) {
while (s[now]) ++now;
s[now] = '(', s[now+len[i]-1] = ')';
}
puts(s+1);
}

Arthur and Brackets CodeForces - 508E (贪心,括号匹配)的更多相关文章

  1. Arthur and Questions CodeForces - 518E (贪心模拟)

    大意: 给定序列$a$, 某些位置为'?', 求给'?'赋值使得序列$(a_1+a_2+...+a_k,a_2+a_3+...+a_{k+1},...)严格递增, 且$\sum|a_i|$最小. 化简 ...

  2. POJ 2955 Brackets 区间DP 最大括号匹配

    http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...

  3. CodeForces 508E Arthur and Brackets 贪心

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

  4. Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]

    E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...

  5. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  6. Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心

    E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...

  7. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

  8. poj 2955 Brackets (区间dp 括号匹配)

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

  9. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

随机推荐

  1. Web、OAuth2/SSO相关拾遗

    OAuth2认证相关:(SSO资源访问流程也应类似设计,它与OAuth2第三方认证.授权不同,是同一个应用系统间的认证.授权过程,且需要实现一个点授权,可访问所有点,一个点退出,收回所有点授权,且有时 ...

  2. Windows FindFirstFile利用

    目前大多数程序都会对上传的文件名加入时间戳等字符再进行MD5,然后下载文件的时候通过保存在数据库里的文件ID读取文件路径,一样也实现了文件下载,这样我们就无法直接得到我们上传的webshell文件路径 ...

  3. Template 制作模版

    直接写在选项里的模板 直接在构造器里的template选项后边编写.这种写法比较直观,但是如果模板html代码太多,不建议这么写. var vm = new Vue({ el:"#app&q ...

  4. Google advertiser api开发概述——批量处理

    批处理 大多数服务都提供同步 API,要求您发出请求然后等待响应,但 BatchJobService 允许您对多项服务执行批量操作,而无需等待操作完成. 与各服务的特定 mutate 操作不同,Bat ...

  5. Docker save & load

    docker save Estimated reading time: 1 minute Description Save one or more images to a tar archive (s ...

  6. eclipse创建springBoot项目

    创建Spring Boot 工程 先在eclipse中安装spring -tool -suite插件,然后根据以下步骤可以创建1.新建Spring Starter Project 2.Packagin ...

  7. idea 常用设置初始化

    1.idea中mybatis关联到mapper.xml文件 2.idea热部署设置 3.IDEA Properties中文unicode转码问题

  8. 解决Linux服务器磁盘空间不足的问题

    在linux服务器执行程序时报错: awk: write failure (No space left on device)awk: close failed on file /dev/stdout ...

  9. POJ 3693 Maximum repetition substring(连续重复子串)

    http://poj.org/problem?id=3693 题意:给定一个字符串,求重复次数最多的连续重复子串. 思路: 这道题确实是搞了很久,首先枚举连续子串的长度L,那么子串肯定包含了r[k], ...

  10. 【Java】【路径】

    [java中Class.getResource用法(用于配置文件的读取)] 用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大常最经常用的,就是用JAVA的F ...