zoukankan      html  css  js  c++  java
  • (GREEDY) 3D

    D. Least Cost Bracket Sequence
    time limit per test
    1 second
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    This is yet another problem on regular bracket sequences.

    A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence.

    For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest.

    Input

    The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5·104. Then there follow m lines, where m is the number of characters "?" in the pattern. Each line contains two integer numbers ai and bi (1 ≤ ai,  bi ≤ 106), where ai is the cost of replacing the i-th character "?" with an opening bracket, and bi — with a closing one.

    Output

    Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.

    Print -1, if there is no answer. If the answer is not unique, print any of them.

    Sample test(s)
    input
    (??)
    1 2
    2 8
    output
    4
    ()()
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    using namespace std;
    pair<int,int> t;
    priority_queue< pair<int,int> > q;
    char s[50010];
    int main()
    {
          scanf("%s",s);
          int len=strlen(s),sum=0;
          long long ans=0;
          for(int i=0;i<len;i++)
          {
                if(s[i]=='(')
                    sum++;
                else if(s[i]==')')
                    sum--;
                else if(s[i]=='?')
                {
                      int a,b;
                      scanf("%d%d",&a,&b);
                      ans+=b;
                      s[i]=')',sum--;
                      q.push( make_pair(b-a,i) );
                }
                if(sum<0)
                {
                    if(q.empty()) break;
                    t=q.top();
                    q.pop();
                    ans-=t.first,s[t.second]='(',sum+=2;
                }
          }
          if(sum) printf("-1
    ");
          else
          {
                printf("%I64d
    %s
    ",ans,s);
          }
          return 0;
    }
    

      

  • 相关阅读:
    FCK常用Js,获取FCK内容,统计FCK字数,向FCK写入指定代码
    asp 点击链接 下载图片文件
    使用微软的 Visual Studio International Pack 1.0 进行网站简体与繁体的互转和得到汉字、拼音、笔画
    mysql alter 语句用法,添加、修改、删除字段等
    C#指定窗口显示位置的方法
    Soukey采集软件源码
    [转](收藏)《博客园精华集》分类索引
    YUI CSS Foundation讲座 博客文库 博客园
    sql group by 和having
    sql 多表查询
  • 原文地址:https://www.cnblogs.com/a972290869/p/4250392.html
Copyright © 2011-2022 走看看