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.
Example
(??)
1 2
2 8
4
()()
给一个包含'(' ')' '?'的括号序列,其中'?'需要替换成'(' ')'使得它变成一个合法的括号序列,每个'?'都有一个变成‘(’ ')'的费用,要使得费用最小
不妨先把所有'?'用')'替换,然后再考虑把其中的一些')'改成'('。这样改回'('的费用就是ai-bi
如果'?'改成了')'导致前k个字符不是合法的括号序列了,就把前k个字符中找个ai-bi最小的改掉,这个维护个堆就好
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 priority_queue<pa,vector<pa>,greater<pa> >q; 26 char s[100010]; 27 int pos[100010]; 28 int l[100010],r[100010]; 29 int n,p; 30 LL ans; 31 int main() 32 { 33 scanf("%s",s+1);n=strlen(s+1); 34 for (int i=1;i<=n;i++) 35 { 36 if (s[i]=='(')p++; 37 else if (s[i]==')') 38 { 39 p--; 40 if (p<0) 41 { 42 if (q.empty()){puts("-1");return 0;} 43 p+=2; 44 pa now=q.top();q.pop(); 45 ans+=now.first;s[now.second]='('; 46 } 47 }else if (s[i]=='?') 48 { 49 int l=read(),r=read(); 50 s[i]=')';p--; 51 ans+=r;q.push(mkp(l-r,i)); 52 if (p<0) 53 { 54 if (q.empty()){puts("-1");return 0;} 55 p+=2; 56 pa now=q.top();q.pop(); 57 ans+=now.first;s[now.second]='('; 58 } 59 } 60 } 61 while (p<0) 62 { 63 p+=2; 64 pa now=q.top();q.pop(); 65 ans+=now.first;s[now.second]='('; 66 } 67 p=0; 68 for (int i=1;i<=n;i++) 69 { 70 if (s[i]=='(')p++;else p--; 71 if (p<0){puts("-1");return 0;} 72 } 73 if (p!=0){puts("-1");return 0;} 74 printf("%lld\n",ans); 75 puts(s+1); 76 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Huawei LiteOS基于Cortex-M4 GD32F4平台移植
· mysql8.0无备份通过idb文件恢复数据过程、idb文件修复和tablespace id不一致处