Balanced Sequence
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): Problem Description
Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type is said to be balanced: + if it is the empty string
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced. Chiaki can reorder the strings and then concatenate them get a new string t. Let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. Chiaki would like to know the maximum value of f(t) for all possible t. Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (≤n≤) -- the number of strings.
Each of the next n lines contains a string si (≤|si|≤) consisting of `(' and `)'.
It is guaranteed that the sum of all |si| does not exceeds ×. Output
For each test case, output an integer denoting the answer. Sample Input )()(()( )
)( Sample Output Source
Multi-University Training Contest Recommend
liuyiding | We have carefully selected several similar problems for you: Statistic | Submit | Discuss | Note
Home | Top Hangzhou Dianzi University Online Judge 3.0
Copyright © - HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.037002(s) query , Server time : -- ::, Gzip enabled

预处理+贪心

最后剩下的左括号的数量为l,右括号的数量为r,

r==0 的在前,之后是l>=r的(r小的在先),

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
struct node
{
int l,r;
}c[];
char t[];
bool cmp(node a,node b)
{
if(a.r==) return ;
if(b.r==) return ;
if(a.l>=a.r&&b.l<b.r) return ;
if(a.l<a.r&&b.l>=b.r) return ;
if(a.l>=a.r&&b.l>=b.r) return a.r<=b.r;
return a.l>=b.l;
}
//按照没有)
//( > ) 要在 ( < )前面
// 如果都是。就按)小的在前面
//左括号多的在前面
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int ans=;
for(int i=;i<n;i++)
{
cin>>t; int len=strlen(t);
c[i].l=c[i].r=;
for(int j=;j<len;j++)
{
if(t[j]=='(')
c[i].l++;
else
{
if(c[i].l)
c[i].l--,ans+=;
else
c[i].r++;
}
}
}
sort (c,c+n,cmp); int L=,R=;
for(int i=;i<n;i++)
{
if(c[i].r<=L) ans+=c[i].r*,L-=c[i].r;
else ans+=*L,L=;
L+=c[i].l;
}
printf("%d\n",ans); } return ;
}

hdu多校1002 Balanced Sequence的更多相关文章

  1. 18年多校-1002 Balanced Sequence

    >>点击进入原题测试<< 思路:自己写没写出来,想不通该怎么排序好,看了杜神代码后补题A掉的.重新理解了一下优先队列中重载小于号的含义,这里记录一下这种排序方式. #inclu ...

  2. HDU 多校对抗赛 B Balanced Sequence

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

  3. HDU6299 Balanced Sequence (多校第一场1002) (贪心)

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

  4. hdu 6299 Balanced Sequence (贪心)

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

  5. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  6. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  7. 2015 HDU 多校联赛 5363 Key Set

    2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...

  8. 2015 HDU 多校联赛 5317 RGCDQ 筛法求解

    2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...

  9. hdu6299 Balanced Sequence 贪心

    题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...

随机推荐

  1. 51nod 1366 贫富差距(flody)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1366 题意: 思路: 如果不是一个连通块的话,肯定是无穷大的. 用flo ...

  2. Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)

    http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...

  3. URL重定向及跳转漏洞

    URL跳转漏洞 URL 跳转漏洞是指后台服务器在告知浏览器跳转时,未对客户端传入的重定向地址进行合法性校验,导致用户浏览器跳转到钓鱼页面的一种漏洞. 使用场景    现在 Web 登录很多都接入了QQ ...

  4. 实现一个简单的flux

    前言 众所周知,React跟Flux是一对好基友. 其中,市场流行的Flux有Redux,Mobx,Reflux. 其中,用法最简单的是Reflux. 其数据流思路如下: +---------+ +- ...

  5. 关于repaint和reflow的笔记

    repaint(重绘) ,repaint发生更改时,元素的外观被改变,且在没有改变布局的情况下发生,如改变outline,visibility,background color,box-shadow不 ...

  6. cocos2dx spine之一 :spine缓存 (c++ & lua)

    cocos2dx版本为3.10 1.在使用spine的过程中,发现了一个比较严重的问题:每次创建SkeletonAnimation的时候都会很卡,即使是使用同一个骨骼数据skeletonData. 跟 ...

  7. 网格视图GridView的使用

    网格视图GridView的排列方式与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要按矩阵格式进行显示时,就可以使用GridView控件来实现. 本文将以一个具体的实例来说明如何使用GridV ...

  8. mo系统常用语句

    mo系统常用语句 一.总结 一句话总结: 1.语言:双语设置(繁体,英语)语句? {:chooseLanguage("確定要刪除么","Are you sure you ...

  9. ffmpeg 无损改变纵横比aspect

    最后发觉, potplayer 里 Ctrl+Enter 或者 Enter 可以扩展到整个屏幕/保持比例,根本不需要额外去转换 如果整个视频都要改的话,把 -ss -t 参数去掉 ffmpeg -ss ...

  10. [MySQL] 中 Schema和Database的区别

    在MySQL中,schema和database是同义词. CREATE SCHEMA和CREATE DATABASE是等效的. 但是其他的数据库产品(几乎所有数据库)有所不同.在oracle数据库产品 ...