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. JAVA基础学习之路(一)基本概念及运算符

    JAVA基础概念: PATH: path属于操作系统的属性,是系统用来搜寻可执行文件的路径 CALSSPATH: java程序解释类文件时加载文件的路径 注释: 单行注释  // 多行注释 /*... ...

  2. 小球下落 (Dropping Balls,UVA 679)

    题目描述: 题目思路: 1.直接用数组模拟二叉树下落过程 //超时 #include <iostream> #include <cstring> using namespace ...

  3. java核心技术 笔记

    一 . 总览 1. 类加载机制:jdk内嵌的class_loader有哪些,类加载过程.--后面需要补充 2. 垃圾收集基本原理,常见的垃圾收集器,各自适用的场景.--后面需要补充 3. 运行时动态编 ...

  4. [leetcode-670-Maximum Swap]

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  5. [leetcode-667-Beautiful Arrangement II]

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  6. ZOJ 3689 Digging(DP)

    Description When it comes to the Maya Civilization, we can quickly remind of a term called the end o ...

  7. nodejs基础学习

    一:复制官网的代码,建立一个简单的服务器 const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; ...

  8. C语言 内存分配 地址 指针 数组 参数 实例解析

    . Android源码看的鸭梨大啊, 补一下C语言基础 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/detai ...

  9. JDK版本Java SE、Java EE、Java ME的区别

    想在win7 X64上搭建JAVA开发环境来着(只是尝试下),打开JAVA 官网下载JDK,发现好多版本懵了,百度了下找到这些版本的区别,故有了下文 1.JAVA SE Java2平台标准版(Java ...

  10. tc:逼良为娼

    tc的学习原来是想着直接从用户态学习的,但是万万没想到哇,qdisc class两个概念直接把我给搞晕了,直接看代码吧 调用:tc qdisc add dev tap0 root handle 1: ...