Parentheses
Accepted : Submit :
Time Limit : MS Memory Limit : KB Parentheses Bobo has a very long sequence divided into n consecutive groups. The i-th group consists of li copies of character ci where ci is either "(" or ")". As the sequence may not be valid parentheses sequence, Bobo can change a character in the i-th group from "(" to ")" (and vice versa) with cost di. He would like to know the minimum cost to transform the sequence into a valid one. Note: An empty string is valid.
If S is valid, (S) is valid.
If U,V are valid, UV is valid. Input The input contains zero or more test cases and is terminated by end-of-file. For each test case: The first line contains an integer n. The i-th of the following n lines contains li,ci,di. ≤n≤
≤l1+l2+⋯+ln≤
l1+l2+⋯+ln is even.
≤di≤
The sum of n does not exceed . Output For each case, output an integer which denotes the result.
Sample Input (
(
(
) )
( Sample Output Note For the first sample, Bobo should change only the character in the second group. For the second sample, Bobo should change half of characters in both groups. Source
XTU OnlineJudge /**
题目:Parentheses
链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1266
题意:从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号,
或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都可以选择翻或者不翻,使整个括号序列是一个合法括号序列。
思路: 我们知道合法括号序列,满足前面左括号的数量>=有括号的数量。前2k个,那么左括号数量>=k个。
首先把所有左括号翻成右括号,那么所有的括号都变成右括号了。可以先计算花费。然后把那些左括号变成右括号的
那些Di变成-Di。那么以后再对它进行翻转的时候,相当于一开始就没有翻转。可以抵消原先加上的花费。 从左到右对所有右括号贪心,满足前面左括号的数量>=有括号的数量的这种规则去处理。维护一个堆存储那些可以翻成左括号的位置,
如果当前需要翻成一个左括号,那么比较当前位置和堆里的,选择一个花费更小的翻成左括号计算花费。同时更新堆。 举个荔枝:
假设没有组的情况。只是纯粹的若干个括号;
位置:
1 2 3 4 5 6 7 8
) ) ) ) ) ) ) )
一开始第一个位置,显然必须翻成左括号,由于堆为空,所以当前翻成左括号。堆仍然为空。
然后对pos=2处理,此时位置2,应该要至少一个左括号,因为前面pos=1已经翻成左括号了,所以当前这个暂时不用翻成左括号。把它扔进堆里。
pos=3;至少要有两个左括号,还差一个。堆顶的是pos=2的花费,当前是pos=3的花费。比较花费更小的,把它翻成左括号,然后更新堆。依次处理。 当然题目是分组的: 第一组:把所有右括号更新到堆。然后计算当前这个位置需要多少个左括号,从堆中取出来。
然后把第二组所有右括号放入堆,然后计算第二组末尾这个位置需要多少个左括号。然后减去原来已经有的左括号数量。
表示还要多少左括号,从堆中取出。依次处理即可。 */ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e5+;
struct node1
{
LL num;
char ch[];
LL cost;
}a[maxn]; struct node
{
LL num;
LL cost;
bool operator < (const node&k)const{
if(cost!=k.cost) return cost>k.cost;///先出小的。
return num>k.num;
}
}t;
priority_queue<node> qu;
/// priority_queue<P, vector<P>, greater<P> > qu;
int main()
{
int n;
while(scanf("%d",&n)==)
{
LL ans = ;
for(int i = ; i < n; i++){
scanf("%I64d%s%I64d",&a[i].num,a[i].ch,&a[i].cost);
if(a[i].ch[]=='('){
ans += a[i].num*a[i].cost;
a[i].cost = -a[i].cost;
}
}
while(!qu.empty()) qu.pop();
LL pre = , sum = , need;
for(int i = ; i < n; i++){
t.num = a[i].num;
t.cost = a[i].cost;
qu.push(t);
sum += a[i].num;
need = (sum+)/;
need = need - pre;
pre = (sum+)/;
while(need>&&!qu.empty()){
t = qu.top();
qu.pop();
if(t.num>need){
t.num -= need;
qu.push(t);
ans += t.cost*need;
break;
}else
{
if(t.num==need){
ans += t.cost*need;
break;
}else
{
need -= t.num;
ans += t.cost*t.num;
}
}
}
}
printf("%I64d\n",ans); }
return ;
}

2017-5-14 湘潭市赛 Parentheses 转化思想+贪心 使括号序列合法的最小花费。满足前面左括号的数量>=有括号的数量。的更多相关文章

  1. Old Sorting(转化成单调序列的最小次数,置换群思想)

     Old Sorting Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit S ...

  2. Gitlab一键端的安装汉化及问题解决(2017/12/14目前版本为10.2.4)

    Gitlab的安装汉化及问题解决 一.前言 Gitlab需要安装的包太TM多了,源码安装能愁死个人,一直出错,后来发现几行命令就装的真是遇到的新大陆一样... ... 装完之后感觉太简单,加了汉化补丁 ...

  3. [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树

    [CERC2017]Intrinsic Interval https://www.luogu.org/blog/ywycasm/solution-p4747# 这种“好的区间”,见得还是比较多的了. ...

  4. hdu6212[区间dp] 2017青岛ACM-ICPC网络赛

    原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...

  5. 2017 icpc 沈阳网络赛

    cable cable cable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. ACM-ICPC 2017 西安赛区现场赛 K. LOVER II && LibreOJ#6062. 「2017 山东一轮集训 Day2」Pair(线段树)

    题目链接:西安:https://nanti.jisuanke.com/t/20759   (计蒜客的数据应该有误,题目和 LOJ 的大同小异,题解以 LOJ 为准)     LOJ:https://l ...

  7. B. Dispersed parentheses 记忆化搜索 + 括号序列的状压表示

    http://codeforces.com/gym/100633/problem/B B. Dispersed parentheses time limit per test 2 seconds me ...

  8. Java 第十一届 蓝桥杯 省模拟赛 合法括号序列

    合法括号序列 题目 问题描述 由1对括号,可以组成一种合法括号序列:(). 由2对括号,可以组成两种合法括号序列:()().(()). 由4对括号组成的合法括号序列一共有多少种? 答案提交 这是一道结 ...

  9. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. angularjs自动加载和手动加载

    (一)自动加载 ng-app是angular的一个指令,代表一个angular应用(也叫模块).使用ng-app或ng-app=""来标记一个DOM结点,让框架会自动加载.也就是说 ...

  2. iOS使用CoreData实现收藏功能

    一般做收藏都是使用数据库或者归档,使用CoreData实现收藏功能就是没事时练一下,实现大概和数据库差不多. 首先创建一个工具类继承NSObject,在里面实现所需要的方法. 工具类的.h文件: ty ...

  3. PHP计算字符串长度,PHP如何计算短信的长度/字数?

    PHP计算字符串长度,包括计算英文.GBK.UTF-8多种字符集下PHP如何计算字符串长度. 英文字符串长度,strlen()是PHP自带的计算英文字符串的函数. GBK字符串长度 中文字符计算为2个 ...

  4. SVN安装中遇到的问题

    新的版本:1.9.5 必须使用Apache Portable Runtime Utility 1.5.4 Released没有安装的话需要先安装 需要安装apr.apr-util sqlite zli ...

  5. Virtualbox环境中安装Oracle 11gr2 RAC(ASM)

    系统Oracle Linux 6.5,Oracle 11.2.0.1 终于开始安装ASM和RAC的行程了.开始前需要想清楚的几个事情: 如何规划网络配置(配置多网卡,实现连通性,规划内外网,eth0, ...

  6. Android--使用XMLPull解析xml

    在Android中极力推荐的xmlpull方式解析xml.xmlpull不只能够使用在Android上.相同也适用于javase,但在javase环境下.你须要自己去获取xmlpull所依赖的类库. ...

  7. OOP几大原则【转】

    设计模式遵循的一般原则: 1.开-闭原则(Open-Closed Principle, OCP):一个软件实体应当对扩展开发,对修改关闭.说的是,再设计一个模块的时候,应当使这个模块可以在不被修改的前 ...

  8. Java开发中的23种设计模式详解 【转】

    创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 行为型模式,共十一种:策略模式.模板方法模式.观察者模式.迭代子模式.责任链模式.命令模式.备忘录模式.状态模式.访问 ...

  9. oracle 存储过程的一个小问题

    在存储过程中,一般不能直接使用DDL语句,需要借助 execute immediate方法,否则可能报 pls00103错误 例子如下: create or replace procedure rt_ ...

  10. 转:【AI每日播报】从TensorFlow到Theano:横向对比七大深度学习框架

    http://geek.csdn.net/news/detail/139235 说到近期的深度学习框架,TensorFlow火的不得了,虽说有专家在朋友圈大声呼吁,不能让TensorFlow形成垄断地 ...