【题目链接】: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 6336】 Matrix from Arrays

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找个规律会发现 M[i][j] = M[i-2*L][j] = M[i][j-2*L] 先预处理出来(1,1)-(2L,2L)这个矩 ...

  2. jquery---- 数组根据值进行删除

    var chooseAttr = []; //serviceId为值 chooseAttr.splice($.inArray(serviceId,chooseAttr),);

  3. Python Study (01) 之 特殊方法

    Python深入:特殊方法和多范式 Python是一切皆对象,意思就是python的天生就是个"纯面向对象语言"呀. 但是!!! Python还是一个多范式语言(multi-par ...

  4. HDU 4342

    先确定M的大致范围后即可求. #include <iostream> #include <cstdio> #include <algorithm> #include ...

  5. [HTML 5] aria-live

    "aria-live" is a method to tell the information to the screen reader once value changed. a ...

  6. Bootstrap警告

    警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 警告(Alerts) 步骤: 1.创建一个 <div> 2.并向其加入一个 .alert ...

  7. hdu5319 Painter(模拟)

    题目链接:点击打开链接 题目大意:给一个矩形.有两把刷子,一把刷红色,一把刷蓝色,红色的方向是东南,蓝色的方向是西北,红色加蓝色等于绿色,如今已知这面墙当前的状态.求从白墙到这个状态最少刷了多少次. ...

  8. 新东方雅思词汇---6.1、oppose

    新东方雅思词汇---6.1.oppose 一.总结 一句话总结:op(相反)+pos(放) 英 [ə'pəʊz]  美 [ə'poz]  vt. 反对:对抗,抗争 vi. 反对 [ 过去式 oppos ...

  9. 循环神经网络(RNN, Recurrent Neural Networks)介绍

    原文地址: http://blog.csdn.net/heyongluoyao8/article/details/48636251# 循环神经网络(RNN, Recurrent Neural Netw ...

  10. ubuntu 使用阿里云 apt 源

    以下内容来自 https://opsx.alibaba.com/mirror Ubuntu对应的“帮助”信息 修改方式:打开 /et/apt/sources.list 将http://archive. ...