zoukankan      html  css  js  c++  java
  • codeforces 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.

    Examples
    input
    (??)
    1 2
    2 8
    output
    4
    ()()

    这题的思路我没想出来,又是看了题解才会的。。

    此题思路:http://blog.csdn.net/baidu_29410909/article/details/50967218

    ac代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <set>
    #include <queue>
    using namespace std;
    typedef long long ll;
    string s;
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        priority_queue<pair<int,int> >q;
        cin>>s;
        ll cnt=0,ans=0,m=0,x,y;
        for(int i=0;i<s.length();++i) {
            if(s[i]=='(') {
                ++cnt;
            }
            else if(s[i]==')') {
                --cnt;
            }
            else {
                s[i]=')';
                cin>>x>>y;
                ans+=y;
                q.push(make_pair(y-x,i));
                --cnt;
                m++;
            }
            pair<int,int> t;
            if(cnt<0) {
                if(q.empty()) break;
                t=q.top();
                q.pop();
                ans-=t.first;
                s[t.second]='(';
                cnt+=2;
            }
        }
        if(cnt!=0) cout<<-1<<endl;
        else {
            cout<<ans<<endl;
            cout<<s<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    AB压力测试(Windows)
    Ensure You Are Not Adding To Global Scope in JavaScript(转)
    使用jasmine来对js进行单元测试
    HTML5安全:CORS(跨域资源共享)简介(转)
    asp.net+jquery Jsonp使用方法(转)
    在ios上时间无法parse返回 "Invalid Date"(转)
    用document.domain完美解决Ajax跨子域 (转)
    IE10、IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题
    NodeJs:module.filename、__filename、__dirname、process.cwd()和require.main.filename 解惑(转)
    关于反射的一些总结(转)
  • 原文地址:https://www.cnblogs.com/zmin/p/7647651.html
Copyright © 2011-2022 走看看