Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心)
Describe
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
()()
Solution
因为左右括号匹配,我们从第一个字符遍历,定义一个sum=0,‘(’ 就 +1 ,‘)’ 就-1,‘?’ 就做相应的处理(下面再说),我们要保证遍历过程中一直让sum>=0,当然可以使用‘?’,如果遍历到一个地方,即使使用'?'sum仍<0,输出-1.
为了让()完美匹配,他给的’(‘ 和 ‘)’ 就不能改了,所以我们要处理'?',从左向右遍历,如果遇到一个‘?’,将’?‘变为‘)’仍满足sum>=0,那么我们就让‘?’变为‘)’(因为我们一直让左括号不少的嘛),如果?变为)之后,sum<0了,我们就在前面(包括这一个)找一个贡献大的变为)的?,使之变为(。
最后判定sum,sum==0正确输出,sum!=0则只可能是’(‘比‘)’多,输出-1。(‘)’比‘(’多的在上面已经判过了)。
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
using namespace std;
const int maxn=5e4+5;
typedef long long ll;
char s[maxn];
int sum;
ll ans;
priority_queue<pair<int,int> > q;
int main(){
scanf("%s",s);
int len=strlen(s),x,y;
for(int i=0;i<len;++i){
if(s[i]=='(')sum++;
else if(s[i]==')'){
sum--;
if(sum<0){
if(q.empty()){printf("-1\n");return 0;}
else{
int xx=q.top().first,yy=q.top().second;q.pop();
ans-=(ll)xx;
s[yy]='(';
sum+=2;
}
}
}
else if(s[i]=='?'){
scanf("%d%d",&x,&y);
q.push(make_pair(y-x,i));//后面要改就先改y-x值大的,这样贡献才最大
s[i]=')';
ans+=(ll)y;
sum--;
if(sum<0){
if(q.empty()){printf("-1\n");return 0;}
else{
int xx=q.top().first,yy=q.top().second;q.pop();
ans-=(ll)xx;
s[yy]='(';
sum+=2;
}
}
}
}
if(sum!=0)printf("-1\n");
else printf("%lld\n%s",ans,s);
return 0;
}
hzoi
Least Cost Bracket Sequence(贪心)的更多相关文章
- CF3D Least Cost Bracket Sequence 贪心
Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
D. Least Cost Bracket Sequence 题目连接: http://www.codeforces.com/contest/3/problem/D Description This ...
- CF3D Least Cost Bracket Sequence 题解
题目 This is yet another problem on regular bracket sequences. A bracket sequence is called regular, i ...
- cf3D Least Cost Bracket Sequence
This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if b ...
- 【贪心】【CF3D】 Least Cost Bracket Sequence
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
- CF3D Least Cost Bracket Sequence(2500的实力贪心...
哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...
- 【贪心算法】CF3D Least Cost Bracket Sequence
题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...
- CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】
Description 给出一个括号序列,中间有一些问号,将第i个问号换成左括号代价是a[i],换成右括号代价是b[i],问如果用最少的代价将这个括号序列变成一个合法的括号序列 Input 第一行一个 ...
随机推荐
- audio的自动播放报错解决
使用audio标签时,当前页面没有进行交互时,比如用户刷新了页面后,play()调用就会报错,如下图 查找资料后,发现是2018年4月以后,chrome浏览器对这块进行了优化,为了节约流量,禁止了自动 ...
- Windows系统安装最新版本RabbitMQ3.8.3及报错解决
今天想安装下RabbitMQ写几个用例看下,发现最新的安装包有些问题,不能直接安装使用,遇到一些问题,记录一下解决办法. 下载安装包 因为RabbitMQ是Erlang编写,安装时,需要先安装Erla ...
- VideoView--简单的设置全屏幕播放
我说的最主要的是要在布局哪里设置一下,如: <com.example.mypalyer.fullScreen android:id="@+id/videoView1& ...
- 用C++模拟蚊子的运动来检验概率论内容
背景:在一个很大的方形透明容器内,从某个位置放入适量的蚊子,等待几分钟后观察蚊子的分布. #include <iostream> #include <cstdlib> #inc ...
- 【JAVA基础】11 Scanner类
1. Scanner的概述 一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器. Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配.然后可以使用不同的 ne ...
- Javascript基础之-var,let和const深入解析(二)
你想在在变量声明之前就使用变量?以后再也别这样做了. 新的声明方式(let,const)较之之前的声明方式(var),还有一个区别,就是新的方式不允许在变量声明之前就使用该变量,但是var是可以得.请 ...
- linux之centos安装jdk以及nginx详细过程
一.安装jdk 1:首先下载jdk到本地,然后通过git 上传到linux服务器上 2:进入目录usr,并创建目录java,将jdk的压缩文件移动到该目录下 cd /usr mkdir java mv ...
- PLDroidPlayer 是七牛推出的一款免费的适用于 Android 平台的播放器 SDK,采用全自研的跨平台播放内核,拥有丰富的功能和优异的性能,可高度定制化和二次开发。 https://developer.qiniu.com/pili/sdk/…
PLDroidPlayer PLDroidPlayer 是一个适用于 Android 平台的音视频播放器 SDK,可高度定制化和二次开发,为 Android 开发者提供了简单.快捷的接口,帮助开发者在 ...
- CodeForces - 1245A Good ol' Numbers Coloring (思维)
Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...
- RobotFrameWork 自动化环境搭建(基于 python3.6)
一.需要安装的工具目录 安装 python3.6.0 我这里采用的是 python3.6 (目前慢慢市场在推广 python3.0 版本了) 安装 wxPython(Python 非常有名的一个GUI ...