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. Centos6设置DNS

    通过编辑 vi /etc/resolv.conf 设置首选DNS和次要DNS.如下,排在前面的就是首选DNS,后面一行就是次要的DNS服务器DNS vi /etc/resolv.conf namese ...

  2. net::ERR_ABORTED ,引入js文件出现报错的解决方法

    在head头里面添加 <mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation ...

  3. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  4. 环境变量PATH

    一.举例 我在用户主文件夹执行命令“ls”,会在屏幕显示该文件夹下的所有文件.然而,ls的完整文件名为“/bin/ls”,按道理我不在/bin下要想执行ls命令必须输入“/bin/ls”,但我仅仅需要 ...

  5. Saruman's Army(贪心)

    Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra ...

  6. 一步步学敏捷开发:1、敏捷开发及Scrum介绍

    敏捷开发之 历史背景 20世纪60年代:软件作坊,软件规模小,以作坊式开发为主:70年代:软件危机,硬件飞速发展,软件规模和复杂度激增,引发软件危机:80年代:软件过程控制,引入成熟生产制造管理方法, ...

  7. 后端设置cookie写不到前端页面

    javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie("id",session.getId()); co ...

  8. 团队作业7——第二次项目冲刺(Beta版本)-第二篇

    1.工作分工: 团队成员 分工 郭达22120 项目整合,后台代码 刘德培44060 数据库模块 石浩洋22061 前台界面优化 曾繁钦22056 前台界面优化.测试 孙斌22030 后台代码 2.燃 ...

  9. Debian 7 amd64问题

    Debian 7 发布了有1段时间,最近才在自己的电脑硬盘安装,用户体验还算可以.在安装Debian的过程中,有问题还是要记录一下的. 注意:遇到的问题跟硬件体系相关,可能在个别电脑没法重现. 1.默 ...

  10. table与div互相嵌套注意

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...