CodeForces - 3D

本文详细解析了 CodeForces-3D 的 D.LeastCostBracketSequence 问题,介绍了一种算法来寻找给定模式下成本最低的常规括号序列。通过替换模式中的 '?' 符号,确保序列的正确性和最小化总成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CodeForces - 3D

CodeForces - 3D
D. Least Cost Bracket Sequence
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard 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
()()
AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
struct node{
    int num, id;
    bool operator < (const node & a) const{
        return num > a.num;
    }
};
char s[N];
int main(){
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int x, y;
    while(~scanf("%s", s)){
        int len = strlen(s);
        int l = 0, r = 0;
        ll ans = 0;
        bool flag = false;
        priority_queue<node> Q;
        for(int i = 0; i < len; i++){
            if(s[i] == '(') l++;
            else if(s[i] == ')') r++;
            else{
                scanf("%d%d", &x, &y);
                ans += y;
                Q.push((node){x - y, i});
                r++;
            }
            if(l < r){
                if(Q.empty()) flag = true;
                else {
                    ans += Q.top().num;
                    s[Q.top().id] = '(';
                    Q.pop();
                    l++;
                    r--;
                }
            }
        }
        if(flag || l != r) printf("-1");
        else{
            printf("%lld\n", ans);
            for(int i = 0; i < len; i++){
                if(s[i] == '?') printf(")");
                else printf("%c", s[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

posted on 2019-01-12 11:13 坤sir 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值