【codeforces 508E】Artur and Brackets
【题目链接】:http://codeforces.com/problemset/problem/508/E
【题意】
让你构造一个括号字符串;
使得每个从左往右数第i个左括号在这个括号序列中与之匹配的右括号的位置与这个左括号的位置的差的在[li..ri]这个区间内;
【题解】
遇到左括号就加入栈内;
然后每次都尝试给栈顶的元素匹配;
能匹配就直接匹配;
不然可能到了后面就不能匹配了(太右);
就按照这个贪心策略搞就能过.
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("W:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2000;
const int K = 110;
int n,l[N],r[N],cnt,pos[N];
char s[N];
stack <int> sta;
void wujie()
{
cout <<"IMPOSSIBLE"<<endl;
exit(0);
}
int main()
{
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n;
rep1(i,1,n)
cin >> l[i] >> r[i];
rep1(i,1,n)
{
sta.push(i);
s[++cnt] = '(';
pos[i] = cnt;
while (sta.size())
{
int x = sta.top();
int idx = pos[x];
if (idx+l[x]>cnt+1) break;
if (idx+r[x]<cnt+1) wujie();
sta.pop();
s[++cnt] = ')';
}
}
if (!sta.empty()) wujie();
for (int i = 1;i <= 2*n;i++)
cout << s[i];
return 0;
}
【codeforces 508E】Artur and Brackets的更多相关文章
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- POI 海量数据
http://blog.csdn.net/Little_Stars/article/details/8266262
- Python爬虫之正則表達式
1.经常使用符号 . :匹配随意字符,换行符 \n 除外 * :匹配前一个字符0次或无限次 ? :匹配前一个字符0次或1次 .* :贪心算法.尽可能的匹配多的字符 .*? :非贪心算法 () ...
- [HTML5] a tag, rel="noopener"
It is a good pratice to add ref="noopener" <a href="/some/domain" target=&quo ...
- Spring+MyBatis双数据库配置
Spring+MyBatis双数据库配置 近期项目中遇到要调用其它数据库的情况.本来仅仅使用一个MySQL数据库.但随着项目内容越来越多,逻辑越来越复杂. 原来一个数据库已经不够用了,须要分库分表.所 ...
- nyoj-647-奋斗小蜗牛在请客(进制转换)
奋斗小蜗牛在请客 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 一路艰辛一路收获.成功爬过金字塔的小蜗牛别提多高兴了.这不为了向以前帮助他的哥们们表达谢意,蜗牛宴请 ...
- 上机题目(中级)- 用小数形式输出指定符号出现的频率 (Java)
题目例如以下:
- springmvc生成注册验证码
通过SPRing MVC为系统添加验证码 1:布局登陆页面,用户名,密码,填写验证码的文本框,及验证码的图片及点击换图 <%@ taglib prefix="c" uri=& ...
- Spring《四-一》解决自动装配的问题
自动化装配使得研发减少了响应的指配工作,但是使得响应的检查难以完成. 解决方法: simple模式: <bean autowire="autodetect" dependen ...
- redis的主从模式
主从通信过程 Master配置: 1:关闭rdb快照(备份工作交给slave) 2:可以开启aof slave配置: 1: 声明slave-of slaveof 192.168.0.102 2: 配置 ...
- 前端学习笔记-CSS