D. Least Cost Bracket Sequence
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
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.
Sample test(s)
Input
(??)
1 2
2 8
Output
4
()()
题意:给出一堆左右括号和问号,还有各个问号变成左右括号的花费,问你是否能将问号变成左右括号,使其配对,且花费最少。
题解:进行贪心策略,先假设所有问号都是右括号,然后若右括号过多(要使得串能够匹配,每一个字符左边的字串中左括号个数必须大于等于右括号的个数),利用优先队列把左右括号花费差最大的变成左括号,这样就可以使花费最小了,若最后括号数不能平衡,则输出-1
package org.lf.algorithms.least.bracket;
import java.util.PriorityQueue;
import java.util.Scanner;
public class LeastBracket {
int x[] = null ;
int y[] = null ;
StringBuffer sb = null ;
int result = 0 ;
public LeastBracket(){
}
public void input(){
Scanner scan = new Scanner(System.in) ;
//读入表达式
sb = new StringBuffer(scan.next()) ;
x = new int[sb.length()] ;
y = new int[sb.length()] ;
int i = 0 ;
while(i<sb.length()){
if(sb.charAt(i)=='?'){
int a = scan.nextInt() ;
int b = scan.nextInt() ;
x[i] = a ;
y[i] = b ;
}
i++ ;
}
}
public void process(){
PriorityQueue<Point> pq = new PriorityQueue<Point>() ;
int i = 0 ;
int count = 0 ;
while(i<sb.length()){
if(sb.charAt(i)=='('){
count++ ;
}else if(sb.charAt(i)==')'){
count-- ;
}else{
Point p = new Point(i,y[i] - x[i]) ;
pq.add(p) ;
sb.replace(i, i+1, ")") ;
count-- ;
result = result + y[i] ;
}
if(count < 0){
if(pq.isEmpty()){
break ;
}
Point temp = pq.remove() ;
pq.remove() ;
sb.replace(temp.num, temp.num+1, "(") ;
result = result - temp.val ;
count = count + 2 ;
}
i++ ;
}
if(count==0){
System.out.println(result) ;
System.out.println(sb) ;
}else{
System.out.println(-1);
}
}
public static void main(String[] args) {
LeastBracket lb = new LeastBracket() ;
lb.input() ;
lb.process() ;
}
}
class Point implements Comparable<Point>{
int val ;
int num ;
public Point(int num,int val){
this.num = num ;
this.val = val ;
}
public int compareTo(Point point) {
//降序排列
if(this.val > point.val)
return -1 ;
if(this.val < point.val)
return 1 ;
return 0 ;
}
}