http://blog.csdn.net/black_miracle/article/details/69307458
题意:给你一串序列 每个问号变成 ( 的代价和变成 ) 的代价 问最小匹配代价和匹配后的序列是什么
题解:我们可以假设?全部是右括号 那么for过去 如果发现一个点 左括号个数小于右括号 那么我们可以从前面的问号拿出一个转化代价最小的 转化为左括号
这么做下去 判掉不可能的情况即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
int num,lab;
bool operator<(const node& a)const{
return num>a.num;
}
}e[50005];
char s[50005];
priority_queue<node,vector<node>,less<node> >sp;
int main(){
int n,i,j;
scanf("%s",s+1);
int len=strlen(s+1);
ll ans=0;
int l=0,r=0,flag=1,x,y;
for(i=1;i<=len;i++){
if(s[i]=='(')l++;
else if(s[i]==')')r++;
else{
scanf("%d%d",&x,&y);
ans+=y;
sp.push((node){x-y,i});
r++;
}
if(l<r){
if(sp.empty())flag=0;
else{
ans+=sp.top().num;
s[sp.top().lab]='(';
sp.pop();
r--;
l++;
}
}
}
if(!flag||l!=r)printf("-1\n");
else{
printf("%lld\n",ans);
for(i=1;i<=len;i++)printf("%c",s[i]=='?'?')':s[i]);
printf("\n");
}
return 0;
}