codeforces 3D . Least Cost Bracket Sequence 贪心
给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1.
弄一个变量num, 如果是( 那么num++,如果是)那么num--。 如果碰到?, 那么先将这个地方弄成), 然后把make_pair(b-a, i)加到一个队列里面。a是换成左括号的值, b是换成右括号的值, i是这个位置的坐标。
如果遇到num小于0的情况, 那么就将队首元素取出, 将这个位置换成左括号。 如果队列为空, 就是不满足, 直接输出-1。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
string s;
priority_queue <pll> q;
int main()
{
cin>>s;
ll ans = ;
int a, b, num = ;
for(int i = ; i<s.size(); i++) {
if(s[i] == '(')
num++;
else if(s[i]==')')
num--;
else {
scanf("%d%d", &a, &b);
ans += b;
s[i] = ')';
num--;
q.push(mk(b-a, i));
}
if(num<) {
if(q.empty()) {
puts("-1");
return ;
}
pll tmp = q.top(); q.pop();
ans -= tmp.first;
s[tmp.second] = '(';
num+=;
}
}
if(num!=) {
puts("-1");
return ;
}
cout<<ans<<endl;
cout<<s<<endl;
return ;
}
codeforces 3D . Least Cost Bracket Sequence 贪心的更多相关文章
- CF3D Least Cost Bracket Sequence 贪心
Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
D. Least Cost Bracket Sequence 题目连接: http://www.codeforces.com/contest/3/problem/D Description This ...
- Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...
- 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 题解
题目 This is yet another problem on regular bracket sequences. A bracket sequence is called regular, i ...
- 【31.93%】【codeforces 670E】Correct Bracket Sequence Editor
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 3D】Least Cost Bracket Sequence
Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价 ...
- CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】
Description 给出一个括号序列,中间有一些问号,将第i个问号换成左括号代价是a[i],换成右括号代价是b[i],问如果用最少的代价将这个括号序列变成一个合法的括号序列 Input 第一行一个 ...
- 【贪心】【CF3D】 Least Cost Bracket Sequence
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
随机推荐
- jquery知识点积累
网上资源汇总学习: jquery的选择器是CSS1-3,xpath的结合物.JQuery提取了这二种查询语言最好的部分,创造出了最终的jquery表达式查询语言. xpath是一门在xml文档里查找信 ...
- 在Win7的IIS上搭建FTP服务及用户授权
FTP服务 FTP是文件传输协议(File Transfer Protocol)的简称,该协议属于应用层协议(端口号通常为21),用于Internet上的双向文件传输(即文件的上传和下载).在网络上有 ...
- jquery easyui filebox 上传附件 + asp.net后台
form必须加这个属性enctype="multipart/form-data",否则后台获取不到文件 <script> function uploadFiles() ...
- 解决chrome下上传文件 返回值带 <pre style="word-wrap:break-word;white-space:prewrap;"></pre>
解决办法:后台 response.setContentType("text/html");
- bootstrap 简易模版
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Linux查看网卡状态
观看网卡传送.接收数据包的状态 $ netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK ...
- Entity Framework学习笔记
原文地址:http://www.cnblogs.com/frankofgdc/p/3600090.html Entity Framework学习笔记——错误汇总 之前的小项目做完了,到了总结经验和 ...
- js获得url内的参数
/** * js获得url内的参数 * 返回值: string or undefined * 使用方法:$_GET['from'] */ var $_GET = (function() { var s ...
- 2016.09.01 html5兼容
<!--[if lt IE 9]> <script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min ...
- 此证书的签发者无效Missing iOS Distribution signing identity问题解决
问题描述 今天准备打包上传AppStore,结果Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Deve ...