CF3D Least Cost Bracket Sequence 贪心
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 贪心的更多相关文章
- 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(2500的实力贪心...
哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...
- 【贪心算法】CF3D Least Cost Bracket Sequence
题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
- Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...
- 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
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
- CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】
Description 给出一个括号序列,中间有一些问号,将第i个问号换成左括号代价是a[i],换成右括号代价是b[i],问如果用最少的代价将这个括号序列变成一个合法的括号序列 Input 第一行一个 ...
随机推荐
- 信道估计(channel estimation)图解——从SISO到MIMO原理介绍
1. 引言 在所有通信中,信号都会通过一个介质(称为信道),并且信号会失真,或者在信号通过信道时会向信号中添加各种噪声.正确解码接收到的信号而没有太多错误的方法是从接收到的信号中消除信道施加的失真和噪 ...
- 如何通过AzureAD平台提供的授权方式访问sharepoint online
官方文档: 1.https://docs.microsoft.com/zh-cn/previous-versions/azure/dn645543(v=azure.100)?redirectedfro ...
- Netty 源码解析: Netty 的 ChannelPipeline
ChannelPipeline和Inbound.Outbound 我想很多读者应该或多或少都有 Netty 中 pipeline 的概念.前面我们说了,使用 Netty 的时候,我们通 ...
- 【代理】内网穿透工具 frp&frps
frp 是一个高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务,支持 tcp, http, https 等协议类型,并且 web 服务支持根据域名进行路由转发. ### frp 的作 ...
- FastStone+ImageReady+Kutools plus导入图片到Excel单元格
先前打算自己做一个也附带训练下,发现有下面方法也好. 1)做帮助文档时需要一种格式(需要将图片导入到Excel中时,假如是按此法归类汇总) 2)FastStone滚动截图 粘贴到Photo ...
- mybatis的缓存2
原文:https://blog.csdn.net/qq_38274974/article/details/100898145 mybatis的缓存分为一级缓存.二级缓存那么,我们为什么要使用缓存呢? ...
- C#构造函数 -0028
默认构造函数 声明基本构造函数的语法就是声明一个与类同名的方法,但该方法没有返回类型: public class MyClass { public MyClass() { } // rest of c ...
- php 判断设备是手机还是平板还是pc
1 <?php 2 //获取USER AGENT 3 $agent = strtolower($_SERVER['HTTP_USER_AGENT']); 4 5 //分析数据 6 $is_pc ...
- 十万同时在线用户,需要多少内存?——Newbe.Claptrap 框架水平扩展实验
Newbe.Claptrap 项目是笔者正在构建以反应式.Actor模式和事件溯源为理论基础的一套服务端开发框架.本篇我们将来了解一下框架在水平扩展方面的能力. 前情提要 时隔许久,今日我们再次见面. ...
- Redis进阶之使用Lua脚本自定义Redis命令
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 1.在Redis ...