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 ;
}

最新文章

  1. Javascript:JSON总结
  2. OpenGL函数思考-glLoadIdentity
  3. php简单单例模式
  4. hadoop 启动停止命令
  5. Netsharp快速入门(之12) 销售管理(开发发货单工作区)
  6. HDU-2527 Safe Or Unsafe
  7. mongodb集群【】
  8. Oracle基础体系浅析
  9. 一个故事带你理解if __name__ == &#39;__main__&#39;
  10. 20175234 2018-2019-2 《Java程序设计》第六周学习总结
  11. Python从入门到精通之eighth!
  12. SQL语句(floor、ceiling和round以及left和right)
  13. C# test
  14. alert.log中的minact-scn: useg scan erroring out with error e:376警告
  15. lamda匿名函数(与sorted(),filter(),map() 一起用), 递归函数, 二分查找
  16. mac下PHPStorm2018.2破解教程
  17. 零基础学习openstack【完整中级篇】及openstack资源汇总
  18. 吃奶酪 状压dp
  19. Robot Framework_Ride(Edit标签)
  20. css模仿微信弹出菜单

热门文章

  1. JQuery中的动画(ppt)
  2. 【spring】spring boot中使用@EnableTransactionManagement 以后,spring mvc接收前台ajax的post方法传过来的参数,使用@RequestBody接收不到参数
  3. Android中的线程池 ThreadPoolExecutor
  4. 科研不是比赛,而是一种对未知和完美的自我追求——跟邢波(Eric Xing)面对面聊科研
  5. python socket timeout设置
  6. IE8 XSS Filter Bypass
  7. javascript数组及操作方法
  8. 安装dubbo-admin报错 URIType BeanCreationException
  9. SpringMVC整合quartz,实现定时任务
  10. msSQL使用表参数