Least Cost Bracket Sequence CodeForces - 3D

题目描述

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 a i and b i (1 ≤ a i,  b i ≤ 106), where a i is the cost of replacing the i-th character "?" with an opening bracket, and b i — 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
()()

分析

一句话题意::给一个序列,序列里面会有左括号、问号、右括号。对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价。问:如何替换使得代价最小。前提是替换之后的序列中,括号是匹配的。如果不能替换为一个括号匹配的序列则输出-1

这道题要用到贪心的思想,我们可以先遍历一遍,把所有的问号都改成右括号

然后我们再从左到右进行匹配,同时记录一下左括号的个数\(cnt\),如果当前遍历到了一个右括号,那么\(cnt\)-- 代表有一对括号匹配成功

如果\(cnt<0\)那么我们就从之前由问号变成的右括号中找一个,把它变成左括号,同时把\(cnt+2\)

那么我们找哪一个呢?肯定是找变成左括号的价值减去变成右括号的价值最小的那一个,我们可以用一个优先队列维护

要是找不到就说明匹配失败,直接输出\(-1\)

最后循环结束的时候如果左括号的个数不为0,也要输出\(-1\)

否则就输出你记录的字符串

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+5;
char s[maxn],s2[maxn];
struct asd{
int lef,rig,num;
asd(int aa=0,int bb=0,int cc=0){
lef=aa,rig=bb,num=cc;
}
bool operator < (const asd& A) const {
return (lef-rig)>(A.lef-A.rig);
}
};
priority_queue<asd> q;
int main(){
scanf("%s",s);
int n=strlen(s);
int cnt=0;
long long ans=0;
for(int i=0;i<n;i++){
if(s[i]=='('){
cnt++;
s2[i]='(';
}
else{
cnt--;
s2[i]=')';
if(s[i]=='?'){
int aa,bb;
scanf("%d%d",&aa,&bb);
ans+=(long long)bb;
q.push(asd(aa,bb,i));
}
if(cnt<0){
if(q.empty()){
printf("-1\n");
return 0;
}
cnt+=2;
int ll=q.top().lef,rr=q.top().rig,id=q.top().num;
s2[id]='(';
q.pop();
ans+=(long long)(ll-rr);
}
}
}
if(cnt!=0) printf("-1\n");
else printf("%lld\n%s\n",ans,s2);
return 0;
}

CF3D Least Cost Bracket Sequence 贪心的更多相关文章

  1. CF3D Least Cost Bracket Sequence 题解

    题目 This is yet another problem on regular bracket sequences. A bracket sequence is called regular, i ...

  2. cf3D Least Cost Bracket Sequence

    This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if b ...

  3. CF3D Least Cost Bracket Sequence(2500的实力贪心...

    哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...

  4. 【贪心算法】CF3D Least Cost Bracket Sequence

    题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...

  5. codeforces 3D . Least Cost Bracket Sequence 贪心

    题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...

  6. Least Cost Bracket Sequence(贪心)

    Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...

  7. Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列

    D. Least Cost Bracket Sequence 题目连接: http://www.codeforces.com/contest/3/problem/D Description This ...

  8. 【贪心】【CF3D】 Least Cost Bracket Sequence

    传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...

  9. CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】

    Description 给出一个括号序列,中间有一些问号,将第i个问号换成左括号代价是a[i],换成右括号代价是b[i],问如果用最少的代价将这个括号序列变成一个合法的括号序列 Input 第一行一个 ...

随机推荐

  1. 减少if...的使用

    最近维护一批代码,其中包括一堆if...的使用,多的情况嵌套8.9层,痛苦不堪,所以搜寻一些可以降低if...else的方法来改善一下代码,写个简单总结. 第一种: 优化前 if (measuredV ...

  2. SpringCloud多数据源实现

    1.枚举多数据源-定义一一对应变量 /** * * 列出所有的数据源key(常用数据库名称来命名) * 注意: * 1)这里数据源与数据库是一对一的 * 2)DatabaseType中的变量名称就是数 ...

  3. Numpy中数据的常用的保存与读取

    保存到文本文件numpy.savetxt()numpy.loadtxt() import numpy as np x= np.arange(0,10,0.1) np.savetxt('save_x', ...

  4. 微信小程序实现连续扫码功能(uniapp)

    注:本文使用的是 uniapp 语法. 微信小程序提供了扫码API:wx.scanCode,但它只能扫一次码,想要实现连续扫码,需要借用 camera 组件.camera 组件不仅能拍照,还具有扫码功 ...

  5. 如何修改npm仓库地址

    解决方案 npm config set registry http://registry.npm.taobao.org/ 将npm默认设置为淘宝镜像地址 发布包 当你想发布自己的包时,需要将地址修改回 ...

  6. Nice Jquery Validator 内置属性

    required - 必填 适用于 input.textarea.select 输入框.(checkbox 与 radio 请使用 checked 规则)字段必填,则值不能为空.字段非必填,则值为空的 ...

  7. Alink漫谈(七) : 如何划分训练数据集和测试数据集

    Alink漫谈(七) : 如何划分训练数据集和测试数据集 目录 Alink漫谈(七) : 如何划分训练数据集和测试数据集 0x00 摘要 0x01 训练数据集和测试数据集 0x02 Alink示例代码 ...

  8. EIGRP-16-其他和高级的EIGRP特性-2-非等价负载分担

    与大多数内部路由协议不同的是, EIGRP能够将流量负载分到多条非等价路径上,而不仅仅使用去往目的地最近距离的那一条路径.提供这项功能的特性称为非等价负载分担.   非等价负载分担的核心概念是可行后继 ...

  9. 顺序表的基本方法实现C语言版

    顺序表--------------线性表的第一个儿子 这个儿子的结构体定义: typedef int ElemType;//取别名 typedef struct link{ ElemType * he ...

  10. equals与hashCode的区别

    equals与hashCode的区别 1.类中的equals方法是一定要重写/覆盖(Override)的,因为要让它按照设计的需求来根据特征值判断等价性. 这里的特征值,就是String类型的name ...