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. 100. Remove Duplicates from Sorted Array && 101. Remove Duplicates from Sorted Array II [easy]

    这两题类似,所以放在一起,先看第一题: Description Given a sorted array, remove the duplicates in place such that each ...

  2. centos 6.5 启动时卡在进度条位置无法进入系统解决办法。

    今天公司服务器因突然断电导致phddns 花生壳 启动失败,一直卡在启动进度条页面. 解决办法 1.按F5查看卡在什么位置, 2.查看解决方法:程序卡住的情况下,直接备份资料后,卸载程序重启就可以了. ...

  3. docker学习2

    今天继续学习docker! 搜索镜像 docker search centos 下载镜像 docker pull name(镜像名字) 查看镜像docker images 字段含义分析: TAG:仓库 ...

  4. 机器人控制tcp通信参数调优

    机器人使用WiFi通信,实现指令下传,状态上传.而WiFi信道平时带宽较稳定,但会在某些时候突然中断,造成ping的延时较高,但可以马上恢复.如果一直ping,则一般情况下ping值很小,但长时间(数 ...

  5. Error: Could not find or load main class org.apache.hadoop.mapreduce.v2.app.MRAppMaster

    自己搭建了一套伪分布的大数据环境,运行Hadoop包中自带的示例时,出现如下错误: 错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMas ...

  6. CP文件覆盖问题

    # \cp -r -a aaa/* /bbb[这次是完美的,没有提示按Y.传递了目录属性.没有略过目录]

  7. PSP1123

    PSP时间图: 类型 任务 开始时间 结束时间 净时间 中断时间 日期 开会 开会 16:17 16:50 33 0 20171027 开会 开会 17:00 17:22 22 0 20171028 ...

  8. Alpha-1

    前言 失心疯病源1 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 10:00~13:00 OpenCV环境配置,Matlab工具包下载 15:40~17:1 ...

  9. lintcode-18-带重复元素的子集

    带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 注意事项 子集中的每个元素都是非降序的 两个子集间的顺序是无关紧要的 解集中不能包含重复子集 样例 如果 S = [1,2,2] ...

  10. TCP系列37—Keep Alive—1、TCP存活检测

    一.TCP存活(keepalive)检测的背景 对于TCP设计来说,如果一个客户端和服务器端建立连接后,不在进行数据传输,那么这个连接将会一直存在下去,理论上即使中间的路由器崩溃重启.或者中间的网络线 ...