zoukankan      html  css  js  c++  java
  • CodeForces 508E Arthur and Brackets 贪心

     题目:

    time limit per test
    2 seconds
    memory limit per test
    128 megabytes
    input
    standard input
    output
    standard output

    Notice that the memory limit is non-standard.

    Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur's favorite correct bracket sequence just to spite him.

    All Arthur remembers about his favorite sequence is for each opening parenthesis ('(') the approximate distance to the corresponding closing one (')'). For the i-th opening bracket he remembers the segment [li, ri], containing the distance to the corresponding closing bracket.

    Formally speaking, for the i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [li, ri].

    Help Arthur restore his favorite correct bracket sequence!

    Input

    The first line contains integer n (1 ≤ n ≤ 600), the number of opening brackets in Arthur's favorite correct bracket sequence.

    Next n lines contain numbers li and ri (1 ≤ li ≤ ri < 2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one.

    The descriptions of the segments are given in the order in which the opening brackets occur in Arthur's favorite sequence if we list them from left to right.

    Output

    If it is possible to restore the correct bracket sequence by the given data, print any possible choice.

    If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes).

    Examples
    input
    4
    1 1
    1 1
    1 1
    1 1
    output
    ()()()()
    input
    3
    5 5
    3 3
    1 1
    output
    ((()))
    input
    3
    5 5
    3 3
    2 2
    output
    IMPOSSIBLE
    input
    3
    2 3
    1 4
    1 4
    output
    (())()

    题意:

    按从左到右的顺序给出n对括号的距离范围[l,r],问这些括号是否能组成合法的括号对。

    样例二:

    题解:

    最右边的一对括号距离肯定要是1,因为它右边不会再有括号被包含了,

    同理,从右往左扫,扫到第i对括号的时候,它的右边的所有括号的长度都是确定下来的,然后从i+1到n,考虑最小的能够被i合法包含的括号对,这里贪心找最小能够为左边提供更多方案,从只要有解,都不会漏掉。

    时间复杂度总共O(n^2)。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 using namespace std;
     7 
     8 const int maxn = 666;
     9 
    10 int n;
    11 
    12 struct Node {
    13     int x, y;
    14     bool operator < (const Node& tmp) const {
    15         return x>tmp.x || (x == tmp.x) && y>tmp.y;
    16     }
    17 }nds[maxn];
    18 
    19 char ans[maxn * 2];
    20 int used[maxn * 2];
    21 int fst, las;
    22 
    23 void init() {
    24     memset(used, 0, sizeof(used));
    25 }
    26 
    27 int main() {
    28     //    freopen("data_in.txt","r",stdin);
    29     while (scanf("%d", &n) == 1 && n) {
    30         init();
    31         for (int i = 0; i<n; i++) {
    32             scanf("%d%d", &nds[i].x, &nds[i].y);
    33         }
    34         int su = 1;
    35         for (int i = n - 1; i >= 0; i--) {
    36             if (nds[i].x == 1) {
    37                 nds[i].x = 1;
    38             }
    39             else {
    40                 int sum = 1, flag = 0;
    41                 for (int j = i + 1; j<n;) {
    42                     sum += (nds[j].x + 1);
    43                     if (sum >= nds[i].x&&sum <= nds[i].y) {
    44                         nds[i].x = sum; flag = 1; break;
    45                     }
    46                     j += (nds[j].x + 1) / 2;
    47                 }
    48                 if (flag == 0) {
    49                     su = 0; break;
    50                 }
    51             }
    52         }
    53         if (!su) printf("IMPOSSIBLE
    ");
    54         else {
    55             for (int i = 0; i<n; i++) {
    56                 for (int j = 0; j<2 * n; j++) {
    57                     if (used[j] == 0) {
    58                         ans[j] = '('; used[j] = 1;
    59                         ans[j + nds[i].x] = ')'; used[j + nds[i].x] = 1;
    60                         break;
    61                     }
    62                 }
    63             }
    64             for (int i = 0; i<2 * n; i++) printf("%c", ans[i]);
    65             printf("
    ");
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    python归并排序
    初学者迭代python
    大数相乘
    基本蚁群算法
    MATLAB绘图,绘双坐标轴,绘一图二轴等
    为什么说TCP协议是可靠的
    TCP协议-报文段数据中的自定义包头
    net start npf启用失败
    富时A50中国指数学习笔记
    ProtoBuffer学习总结
  • 原文地址:https://www.cnblogs.com/fenice/p/5449680.html
Copyright © 2011-2022 走看看