【CF3D Least Cost Bracket Sequence】题解
本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15728520.html
题目链接
题目
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.
给一个序列,序列里面会有左括号、问号、右括号。对于一个‘?’而言,可以将其替换为一个‘(’,也可以替换成一个‘)’,但是都有相应的代价。问:如何替换使得代价最小。前提是替换之后的序列中,括号是匹配的。如果不能替换为一个括号匹配的序列则输出-1。
思路
先把序列中所有的问号变成右半括号,当不够左半括号时,把前面代价最小的一个右半括号变成左半括号即可,这个过程可以用优先队列来维护。
总结
这是一道非常好的思维题。
一开始我想的是 的dp(就是枚举前 个字符中有 个左半括号待匹配的最小代价),这会超时。
我看了一下标签,优先队列,自然想到可撤销贪心,但推出的方法具有后效性。
后来看题解,才发现可以把所有问号先变成右半括号。其实原因是在枚举过程中,右半括号必须满足前面有对应的左半括号,而左半括号无限制,只需要最后满足即可。于是可以先把题目中的变量变成具有限制的量,而可撤销贪心是可以撤销成无限制的量,以消除后效性。
同时,对于这种每种物品只有两个转态的,可以多想想可撤销贪心。
Code
1 | // Problem: CF3D Least Cost Bracket Sequence |





