3 D. Least Cost Bracket Sequence
题目大意:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
typedef __int64 LL;
const LL INF = 1e9+;
const LL MOD = 1e9+;
const LL maxn = ;
char str[maxn];
LL n;
struct node
{
int a, b, c;
int Index;
bool friend operator < (node B,node A)
{
return A.c < B.c;
}
} P[maxn]; LL solve()
{
LL k = , ans = , cnt = ;
priority_queue<node> Q;
node Pn;
for(int i=; str[i]; i++)
{
if(str[i] == '?')
{
Q.push(P[k]);
ans += P[k].b;
str[i] = ')';
k ++;
cnt --;
}
else if(str[i] == '(')
cnt ++;
else
cnt --; if(cnt < )
{
while(Q.size() && cnt < )
{
Pn = Q.top();
Q.pop();
cnt += ;
ans = ans - Pn.b + Pn.a;
str[Pn.Index] = '(';
}
}
if(cnt < )
return -;
}
if(cnt > )
return -;
return ans;
} int main()
{
scanf("%s", str); for(int i=; str[i]; i++)
{
if(str[i] == '?')
{
scanf("%d %d", &P[n].a, &P[n].b);
P[n].c = P[n].a - P[n].b;
P[n ++].Index = i;
}
}
LL ans = solve(); printf("%I64d\n", ans); if(ans != -)
puts(str); return ;
}
3 D. 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 贪心
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 题解
题目 This is yet another problem on regular bracket sequences. A bracket sequence is called regular, i ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
- 【Codeforces 3D】Least Cost Bracket Sequence
Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价 ...
- 【贪心】【CF3D】 Least Cost Bracket Sequence
传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...
- CF3D Least Cost Bracket Sequence(2500的实力贪心...
哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...
随机推荐
- Linux下sqlite的安装与使用
简介 SQLite是一款轻量级数据库,是遵守ACID的关联式数据库管理系统.它的设计目的是嵌入式.目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百KB的内存就 ...
- ui线程和后台线程异步
private void Button_Click(object sender, RoutedEventArgs e) { CreateElementOnSeperateThread(() => ...
- 在oracle中怎么把一张表的数据插入到另一张表中
把table2表的数据插入到table1中 insert into table1 select * from table2
- MySQL性能状态查看方式
1. QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Question% ...
- 【HOJ2430】【贪心+树状数组】 Counting the algorithms
As most of the ACMers, wy's next target is algorithms, too. wy is clever, so he can learn most of th ...
- Windows2003 下 MySQL 数据库每天自动备份
1. 环境: windows server 2003 + Apache 2.0 + PHP5 + MySQL 4.0.26 . 2. 假设 PHP 安装目录为 D:/php ,MySQL 安装目录为 ...
- github 分支 合并
Git如何进行分支管理? 1.创建分支 创建分支很简单:git branch <分支名> 2.切换分支 git checkout <分支名&g ...
- function field , store={}...
def _get_product_state(self,cr,uid,ids,fields,arg=None,context=None): res={} dic=dict( self.pool.get ...
- Android RecyclerView Adapter 新式用法之SortedListAdapterCallback
引言 前几天在同事的提醒下发现V7中有了一个新的工具类SortedListAdapterCallback,配合RecyclerView Adapter和SortedList一起使用更加方便的管理我们在 ...
- STM32学习笔记——USART串口(向原子哥和火哥学习)
一.USART简介 通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换.USART利用分数波特率发生器提供宽范围的波特率选择. S ...