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. TZOJ 复习时间

    描述 为了能过个好年,xhd开始复习了,于是每天晚上背着书往教室跑.为了追求更高的效率,xhd要根据难度值来选择合适的课程进行复习,复习后一门课的效率为前一门课之间的难度差的平方,而复习第一门课的效率 ...

  2. iOS -App主流框架UINavigationController && UITabBarController的简单使用

     一个iOS app几乎没有由一个控制器组成,除非这个app非常简单.       当app中有多个控制器的时候,就需要对这些控制器进行管理,用1个控制器去管理其他多个控制器:       如图所示: ...

  3. 一文带你快速搞懂动态字符串SDS,面试不再懵逼

    目录 redis源码分析系列文章 前言 API使用 embstr和raw的区别 SDSHdr的定义 SDS具体逻辑图 SDS的优势 更快速的获取字符串长度 数据安全,不会截断 SDS关键代码分析 获取 ...

  4. ubuntu12.04 dnw2 fl2440 配置

    1.安装libusb-dev sudo apt-get install libusb-dev 2.dnw2编译配置 源码如下,将其保存为dnw2.c 编译命令 gcc dnw2.c -o dnw2 - ...

  5. EIGRP-13-弥散更新算法-停滞在活动状态

    如果一台路由器参与到了针对某个目的地的弥散计算中(即将相应路由置为活动状态,并发送查询包),它必须首先等待所有邻居都返回响应包,之后它才能执行自已的弥散计算,接着选出新的最优路径,最后开始发送自已的响 ...

  6. Docker学习 ,超全文档!

    我们的口号是:再小的帆也能远航,人生不设限!!        一.学习规划: Docker概述 Docker安装 Docker命令 Docker镜像 镜像命令 容器命令 操作命令 容器数据卷  Doc ...

  7. 设计一个简单的多线程(Fecit)_1

    D6高级编程,Fecit ,学习里面关于线程创建的一个例子.,按照那个例子做的,不过本人喜欢将线程实现部分作为单独的单元,主线程再调用它. unit Unit1; interface uses Win ...

  8. foreach 集合又抛经典异常了,这次一定要刨根问底

    一:背景 1. 讲故事 最近同事在写一段业务逻辑的时候,程序跑起来总是报:集合已修改:可能无法执行枚举操作,硬是没有找到什么情况下会导致这个异常产生,就让我来找一下bug,其实这个异常在座的每个程序员 ...

  9. qt解决release后数据库连接不上的问题

    问题 : 明明已经设置了 "./xxx" , 为什么release之后数据库还是连不上呢 解决 : 项目中建立一个plugins文件夹 将qt安装目录下的sqldrivers复制到 ...

  10. Swagger之外的选择

    今天给大家安利一款接口文档生成器--JApiDocs. swagger想必大家都用过吧,非常方便,功能也十分强大.如果要说swaager有什么缺点,想必就是注解写起来比较麻烦.如果我说有一款不用写注解 ...