codeforces 3D Least Cost Bracket Sequence

来源:互联网 发布:知乎热门话题排行榜 编辑:程序博客网 时间:2024/05/19 13:30
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.

题目的大意是给一个序列,序列里面会有左括号、问号、右括号。对于一个‘?’而言,可以将其替换为一个‘(’,也可以替换成一个‘)’,但是都有相应的代价。

问,如何替换使得代价最小。前提是替换之后的序列中,括号是匹配的。如果不能替换为一个括号匹配的序列则输出-1。

刚开始想的是动态规划,但是想想这个好像是有后效的,所以动态规划就作罢了。

然后就贪心吧。先假设所有的‘?’全部替换成右括号,然后按常规的办法去检测这个序列是否括号匹配。

所谓的常规的办法就是遍历这个序列,维护一个计数器cnt,当遇到左括号时计数器+1,遇到右括号时计数器-1

如果中途遇到cnt小于0的情况,则说明这个序列不是括号匹配的,但是在我们这里,右括号可能是‘?’变来的,所以当遇到cnt小于0时,则去看前面有没有‘?’变过来的右括号,如果没有,那就说明无论如何这个序列无法被替换为括号匹配的序列;如果有的话,则选取一个“最好”的由‘?’变过来的右括号,将其改为左括号,这样的话cnt又可以加2了。这里所谓的“最好”,就是贪心的过程。至于怎样的最好,就自己去想吧。

如果这样到最后cnt还大于0,则说明即使无法获得一个括号匹配的序列,输出-1即可。

1.将所有的问号替换为右括号

2.遍历序列,维护计数器

3.当遇到计数器小于0则考虑从前面找一个问号变成左括号而不是右括号

ps.答案超过了int,请用long long,而且codeforces上的long long用的是%I64d

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <stack>#include <map>#include <queue>using namespace std;struct cost {    int p;    int v;    bool operator < (const cost& a) const {        return v < a.v;     }};cost make(int p, int v) {    cost ret;    ret.p = p;    ret.v = v;    return ret;}char str[50004];int main() {    scanf("%s", str);    int cnt = 0;    int len = strlen(str);    int a, b;    priority_queue<cost> q;    long long ans = 0;    for (int i = 0; i < len; i++) {        cnt += str[i] == '(';        cnt -= str[i] == ')' || str[i] == '?';        if (str[i] == '?') {            scanf("%d %d", &a, &b);            q.push(make(i, b-a));            ans += b;            str[i] = ')';        }        if (cnt < 0 && q.empty()) {            ans = -1;            break;        }        if (cnt < 0) {            cost top = q.top();            q.pop();            ans = ans - top.v;            str[top.p] = '(';            cnt += 2;        }    }    if (cnt > 0) ans = -1;    printf("%I64d\n", ans);    if (ans != -1) printf("%s\n", str);    return 0;}


0 0