CF3D Least Cost Bracket Sequence 题解
题目
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。
输入格式
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed \(5·10^4\). Then there follow \(m\) lines, where \(m\) is the number of characters "?" in the pattern. Each line contains two integer numbers \(a_i\) and \(b_i (1 ≤ a_i, b_i ≤ 10^6)\), where \(a_i\) is the cost of replacing the i-th character "?" with an opening bracket, and \(b_i\) — with a closing one.
第一行是序列,序列长度不超过50000,下面m(m是?的数量)行有每行2个数据,第一个是(的代价,第2个是)的代价
输出格式
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.
第一行打印代价,第二行打印替换后的序列。不行输出-1
样例输入
(??)
1 2
2 8
样例输出
4
()()
题解
使用贪心
先把每个?变成右括号,如果这时候发现这个右括号没有左括号和它匹配,就从当前位置往前在所有没变成左括号的?中选择变为左括号产生的花费最少的一个,转成左括号.
写代码的时候, 扫描每个?,将它赋值为左括号, 然后将其加入优先队列, 找花费最少的时候, 直接从优先队列里找
如果最后还是存在未匹配的括号,就是无解的情况.
另外统计价值之和时要开long long.
代码
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
const int MAXN = 50005;
char s[MAXN];
int n, lb, rb;
struct Node {
int i, l, r;
bool operator<(const Node &other) const { return r - l < other.r - other.l; }
};
std::priority_queue<Node> pq;
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
int cnt = 0;
long long sum = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '(') cnt++;
if (s[i] == '?') {
scanf("%d%d", &lb, &rb);
if (i == 1) {
sum += lb;
s[i] = '(';
cnt++;
continue;
}
sum += rb;
s[i] = ')';
pq.push((Node){i, lb, rb});
}
if (s[i] == ')') {
if (cnt == 0) {
if (pq.empty()) return puts("-1"), 0;
Node t = pq.top();
pq.pop();
if (t.i == n) t = pq.top(), pq.pop();
sum = sum - t.r + t.l;
s[t.i] = '(';
cnt += 2 - (t.i == i);
}
if (s[i] == ')') cnt--;
}
}
if (cnt != 0)puts("-1");
else printf("%lld\n%s", sum, s + 1);
return 0;
}
CF3D Least Cost Bracket Sequence 题解的更多相关文章
- CF3D Least Cost Bracket Sequence 贪心
Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...
- 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(2500的实力贪心...
哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...
- 【贪心算法】CF3D Least Cost Bracket Sequence
题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...
- 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
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
- Least Cost Bracket Sequence,题解
题目链接 题意: 给你一个含有(,),?的序列,每个?变成(或)有一定的花费,问变成课匹配的括号的最小花费. 分析: 首先如果能变成匹配的,那么就有右括号的个数始终不多于左括号且左右括号数量相等,那就 ...
- CF524F And Yet Another Bracket Sequence 题解
题目链接 算法:后缀数组+ST表+贪心 各路题解都没怎么看懂,只会常数巨大的后缀数组+ST表,最大点用时 \(4s\), 刚好可以过... 确定合法序列长度 首先一个括号序列是合法的必须满足以 ...
随机推荐
- TB6612FNG电机驱动模块
TB6612FNG电机驱动模块 模块原理图 模块的使用 TB6612是双驱动,也就是可以驱动两个电机 下面分别是控制两个电机的IO口 STBY口接单片机的IO口清零电机全部停止, 置1通过AIN1 A ...
- call,apply,bind的理解
2020-03-19 call,apply,bind的理解 先说区别call, apply基本上没什么不一样,唯一不一样的地方是传参方式不同 但是bind和call,apply有区别.bind是重新绑 ...
- python + selenium登陆并点击百度平台
from PIL import Imagefrom selenium.webdriver import DesiredCapabilitiesfrom selenium import webdrive ...
- Android学习笔记点击事件和触摸事件的区别
当我们点击手机屏幕的时候Android系统不仅会触发单击事件,还会触发触摸事件.在Android中它会先触发触摸事件,如果这个触摸事件没有被消费掉再去触发单击事件 代码示例: MainActivty. ...
- vc++,MFC,组合框控件设置时0xC0000005: 读取位置 0x00000020 时发生访问冲突
511.exe 中的 0x78bb5dec (mfc90ud.dll) 处未处理的异常: 0xC0000005: 读取位置 0x00000020 时发生访问冲突 _AFXWIN_INLINE int ...
- 龙芯团队完成CoreCLR MIPS64移植,在github开源
国产龙芯的软件生态之中.NET不会缺席,毕竟 C# 与 .NetCore/Mono 也是全球几大主流的编程语言和运行平台之一,最近一段时间听到太多的鼓吹政务领域不支持.NET, 大家都明白这是某些人为 ...
- JMETER学习宝典
1. 简介 Apache JMeter是100%纯java桌面应用程序,被设计用来测试客户端/服务器结构的软件(例如web应用程序).它可以用来测试包括基于静态和动态资源程序的性能,例如静态文件,Ja ...
- InnoDB 中 B+ 树索引的分裂
数据库中B+树索引的分裂并不总是从页的中间记录开始,这样可能会导致空间的浪费,例如下面的记录: 1, 2, 3, 4, 5, 6, 7, 8, 9 插入式根据自增顺序进行的,若这时插入10这条记录后需 ...
- 常用电子邮件协议服务POP3/IMAP/SMTP/Exchange
标题: 常用电子邮件协议服务POP3/IMAP/SMTP/Exchange 作者: 梦幻之心星 347369787@QQ.com 标签: [电子邮件, 服务, 协议] 目录: [客户端] 日期: 20 ...
- 线上redis问题修复:JedisConnectionException: Unexpected end of stream.
经过: 项目上线后经常报 Unexpected end of stream.; nested exception is redis.clients.jedis.exceptions.JedisConn ...