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

(1≤n≤105

) -- the number of strings.
Each of the next n

lines contains a string si

(1≤|si|≤105

) consisting of `(' and `)'.
It is guaranteed that the sum of all |si|

does not exceeds 5×106

.

 
Output
For each test case, output an integer denoting the answer.
 
Sample Input
2
1
)()(()(
2
)
)(
 
Sample Output
4
2
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + ;
struct node {
int l, r, sum;
} qu[maxn];
int cmp(node a, node b) {
if (a.r < a.l && b.r >= b.l) return ;
if (a.r >= a.l && b.r < b.l) return ;
if (a.r >= a.l && b.r >= b.l) return a.l > b.l;
return a.r < b.r;
}
int n, t;
char s[ * maxn];
int main() {
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for (int i = ; i < n ; i++) {
scanf("%s", s);
qu[i].l = qu[i].r = qu[i].sum = ;
int len = strlen(s);
for (int j = ; j < len ; j++) {
if (s[j] == '(') qu[i].l++;
else {
if (qu[i].l > ) qu[i].l--, qu[i].sum++;
else qu[i].r++;
}
}
}
sort(qu, qu + n, cmp);
int ans = , cnt = ;
for (int i = ; i < n ; i++) {
if (qu[i].r > cnt) {
ans += cnt + qu[i].sum;
cnt = ;
} else {
ans += qu[i].r + qu[i].sum;
cnt -= qu[i].r;
}
cnt += qu[i].l;
}
printf("%d\n", ans * );
}
return ;
}

Balanced Sequence(毒瘤啊)排序贪心 HDU多校的更多相关文章

  1. 2018 Multi-University Training Contest 1-1002 -Balanced Sequence(括号匹配+贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6299 题目: 题意:t组数据,每组数据给你一个n表示给你n个括号串,这n个括号串之间进行组合,求能够匹 ...

  2. hdu 6299 Balanced Sequence (贪心)

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

  3. hdu多校1002 Balanced Sequence

    Balanced Sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...

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

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

  5. HDU 多校对抗赛 B Balanced Sequence

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

  6. hdu6299 Balanced Sequence 贪心

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

  7. BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心

    BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...

  8. CodeForces 1294B Collecting Packages(排序+贪心)

    http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否 ...

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

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

随机推荐

  1. spark写入ES(动态模板)

    使用es-hadoop插件,主要使用elasticsearch-spark-20_2.11-6.2.x.jar 官网:https://www.elastic.co/guide/en/elasticse ...

  2. Window下部署MySql数据库

    官网下载地址:https://dev.mysql.com/downloads/mysql/,MySQL Community(社区版) Server 5.7.21,下载完毕后,解压文件. (1)在mys ...

  3. java并发总览

  4. cygwin—excellent work!

    使用cygwin的好处在于可以避免直接使用linux同时又能最大限度的节省资源,共享windows的资源. 安装cygwin 安装安简单,当然,你首先需要使用163或者国内或者亚洲比较好的镜像作为下载 ...

  5. css修改placeholder的样式

    css修改placeholder的样式 input::-webkit-input-placeholder { /* WebKit browsers */ font-size:14px; color: ...

  6. databales详解(一)

    学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...

  7. MFC最基本动作(如创建窗口,点击取消等)函数的执行顺序

    一.MFC应用程序中处理消息的顺序: 1.AfxWndProc()       该函数负责接收消息,找到消息所属的CWnd对象,然后调用AfxCallWndProc2.AfxCallWndProc() ...

  8. Spring Boot(三)自动装配

    @Configuration和@Bean Spring提供了注解@Configuration和@Bean注解用来配置多个Bean,在以前的Spring项目中可以通过xml的方式配置: <bean ...

  9. hive mapjoin优化

    默认为10MB,如果大于该值不会执行mapjoin,hive语句中直接设置的mapjoin也不再起作用. 参考hive wiki把hive.auto.convert.join.noconditiona ...

  10. HTML页面垂直滚动条不见

    <body style="overflow-y:scroll;"> </body>