Arthur and Brackets CodeForces - 508E (贪心,括号匹配)
大意: 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 (贪心,括号匹配)的更多相关文章
- Arthur and Questions CodeForces - 518E (贪心模拟)
大意: 给定序列$a$, 某些位置为'?', 求给'?'赋值使得序列$(a_1+a_2+...+a_k,a_2+a_3+...+a_{k+1},...)严格递增, 且$\sum|a_i|$最小. 化简 ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- CodeForces 508E Arthur and Brackets 贪心
题目: E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input s ...
- 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 ...
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- 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 ...
- 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 ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...
随机推荐
- P3128 [USACO15DEC]最大流Max Flow
思路 这个题哪里有那么费脑筋 我们可以树链剖分嘛LCT昨天学的时候睡着了,不是太会 两遍dfs+一个5行的BIT 其实树链剖分学好了对倍增和LCT理解上都有好处 一条路径上的修改 由于一条剖出来的链是 ...
- SpringBoot Redis使用fastjson进行序列化
在使用spring-data-redis,默认情况下是使用org.springframework.data.redis.serializer.JdkSerializationRedisSerializ ...
- HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解
题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...
- What is event bubbling and capturing?
What is event bubbling and capturing? 答案1 Event bubbling and capturing are two ways of event propaga ...
- python 安装wheel .whl文件
首先得有pip没有的话安装一个. 然后:pip install wheel 然后:pip install 路径\文件名.whl ===================== pip --versionp ...
- Druid介绍
Druid (大数据实时统计分析数据存储) Druid 是一个为在大数据集之上做实时统计分析而设计的开源数据存储.这个系统集合了一个面向列存储的层,一个分布式.shared-nothing的架构,和一 ...
- C# winform窗体间传值(使用委托或事件)
窗体间传值 今天得空,刚好看到网上好多人再找winform窗体间传值的问题,由于昨天项目的优化的感觉不错,就写了个C# winform窗体间传值的demo,希望能给需要的人的带来帮助: 工程的源代码地 ...
- new和malloc的用法和区别
从以下几个方面总结下new和malloc的区别: 参考博客: https://blog.csdn.net/nie19940803/article/details/76358673 https://bl ...
- HTML元素 绑定href属性
给<li>标签绑定href属性 <ul class="honor-list clearfix"> <li style="cursor:poi ...
- hdu 2034 改革春风吹满地 多边形面积
改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...