【题目链接】: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的更多相关文章

  1. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. 【hdu 6342】Expression in Memories

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把所有的问号都改成'1' 然后会发现只有+0?这种情况 需要把?改成+. 看看这样的0后面的1是不是由问号改过来的就好了.是的话 再 ...

  2. java+selenium+testNG+excel 实现 web 网页的自动化测试

    webdriver的关键字从excel读取,这样测试人员只需要在excel中填写相关用例即可 前端微站和后台系统的用例可整合在同一excel中,这样可实现前端与后台的闭循环测试 除了一些基本的校验规则 ...

  3. asp.net-EF事物与存储过程

    FK_Equipment_EquipmentClass 这个是sql中的命名规范,外键名称在前面,主键名称在后面 EF事务的代码 DbTransaction tran = null; try { ne ...

  4. 在centOS6.5 上安装使用pipework

    需求:镜像生成了2个含有tomcat的容器,用nginx进行负载均衡.但是容器重启后ip会自动改变...所以使用pipework进行分配静态ip pipework安装 OS:centos6.5 第一步 ...

  5. pthread_cond 唤醒特定线程的方法

  6. 機器學習基石 机器学习基石 (Machine Learining Foundations) 作业2 Q16-18 C++实现

    大家好,我是Mac Jiang,今天和大家分享Coursera-NTU-機器學習基石(Machine Learning Foundations)-作业2 Q16-18的C++实现.尽管有非常多大神已经 ...

  7. codecombat之KithGard地牢19-37关代码分享

    codecombat中国游戏网址:http://www.codecombat.cn/ 全部代码为javascript代码分享 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 19 ...

  8. BroadcastReceiver类

    java.lang.Object    ↳ android.content.BroadcastReceiver 已知直接子类 AppWidgetProvider DeviceAdminReceiver ...

  9. java 正则表达式(一)

    1匹配验证-验证Email是否正确 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void main(String[] args)  ...

  10. NEUOJ 1702 撩妹全靠魅力值 (三维偏序)

    题目链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1702 题目大意:就是问每个人三个属性同时不低于另外几个人....人不分先后 经典的三维偏序问题 解 ...