【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 ...
随机推荐
- 学习redis遇到的问题
1. pipeline为什么批量执行速度会变快? 答:是因为在tcp连接中减少了交互往返的时间,因为每次执行还要返回响应值,并且是一条执行完成之后才会执行下一条,但是批量执行只需要一次往返,所以节省了 ...
- POJ2389 Bull Math
/* POJ2389 Bull Math http://poj.org/problem?id=2389 高精度乘法 * */ #include <cstring> #include < ...
- hdu 2435dinic算法模板+最小割性质
hdu2435最大流最小割 2014-03-22 我来说两句 来源:hdu2435最大流最小割 收藏 我要投稿 2435 There is a war 题意: 给你一个有向图,其中可以有一条边是无敌的 ...
- rabbitMQ学习笔记(三) 消息确认与公平调度消费者
从本节开始称Sender为生产者 , Recv为消费者 一.消息确认 为了确保消息一定被消费者处理,rabbitMQ提供了消息确认功能,就是在消费者处理完任务之后,就给服务器一个回馈,服务器就会将 ...
- [SharePoint2010开发入门经典]8集成业务线数据,使用业务联通服务
本章概要: 1.了解office business application 2.理解商务联通服务(BCS),如何使用BCS构建OBA 3.通过BCS使用SharePoint和办公集成技术
- lvs 负载均衡环境搭建
记录一下搭建lvs环境的步骤 当中master 10.0.0.11,backup 10.0.0.15.realserver1 10.0.0.119.realserver210.0.0.11 1.mkd ...
- idea使用技巧资料篇
基本使用:https://my.oschina.net/lujianing/blog/177042#OSC_h1_2 解决idea卡顿问题:http://www.cnblogs.com/ae6623/ ...
- hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)
Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 【POJ 1741】 Tree
[题目链接] http://poj.org/problem?id=1741 [算法] 点分治 要求距离不超过k的点对个数,不妨将路径分成两类 : 1. 经过根节点 2. 不经过根节点 考虑第1类路径, ...
- tomcat指定JDK版本
在windows环境下以批处理文件方式启动tomcat,只要运行<CATALINA_HOME>/bin/startup.bat这个文件,就可以启动Tomcat. 在启动时,startup. ...