codeforces 508E (构造)

题目链接:点击这里

题意:依次给出从左到右每一个左括号到与其匹配的右括号的距离范围,求出一个合法的括号序列。

注意到如果一个括号他的右括号已经可以放了那么就可以直接放上去。那么直接先把左括号扔进一个栈,并且记录下所有栈中的左括号在没找到与之匹配的右括号之前中间长度增加了多少。扫一边就好了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
#define Clear(x,y) memset (x,y,sizeof(x))
#define Close() ios::sync_with_stdio(0)
#define Open() freopen ("more.in", "r", stdin)
#define get_min(a,b) a = min (a, b)
#define get_max(a,b) a = max (a, b);
#define y0 yzz
#define y1 yzzz
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define pb push_back
#define pl c<<1
#define pr (c<<1)|1
#define lson tree[c].l,tree[c].mid,pl
#define rson tree[c].mid+1,tree[c].r,pr
#define mod 1000000007
typedef unsigned long long ull;
template <class T> inline T lowbit (T x) {return x&(-x);}
template <class T> inline T sqr (T x) {return x*x;}
template <class T>
inline bool scan (T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9') ) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
const double pi = 3.14159265358979323846264338327950288L;
using namespace std;
#define INF 1e17
#define maxn 605
#define maxm 1000005
//-----------------morejarphone--------------------//

int n, l[maxn], r[maxn];
stack <int> gg;
string ans = "";
int more[maxn];

int main () {
    //Open ();
    Clear (more, 0);
    while (!gg.empty ()) gg.pop ();
    cin >> n;
    for (int i = 1; i <= n; i++) { //cout << i << endl;
        cin >> l[i] >> r[i];
        ans += '(';
        if (l[i] == 1) {
            ans += ')';
            more[i] = 2;
            if (!gg.empty ()) {
                int j = gg.top (); more[j] += 2;
            }
            while (!gg.empty ()) {
                int j = gg.top ();
                if (more[j]+1 <= r[j] && more[j]+1 >= l[j]) {
                    ans += ')';
                    gg.pop ();
                    more[j] += 2;
                    if (!gg.empty ()) {
                        int now = gg.top (); more[now] += more[j];
                    }
                }
                else if (more[j]+1 > r[j]) {
                    cout << "IMPOSSIBLE" << endl;
                    return 0;
                }
                else break;
            }
        }
        else
            gg.push (i);
    }// cout << ans << endl;
    if (!gg.empty ()) cout << "IMPOSSIBLE" << endl;
    else cout << ans << endl;
    return 0;
}
/*
6
9 9
3 3
1 1
3 3
1 1
1 1
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值