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. 【swiper】 滑块组件说明

    swiper 滑块视图容器,其原型如下: <swiper indicator-dots="[Boolean]" indicator-color="[Color]&q ...

  2. Centos7添加静态路由

    本文摘取自 Centos7系统配置上的变化(二)网络管理基础 一.ip route显示和设定路由 1.显示路由表 [root@centos7 ~]# ip route show default via ...

  3. Paper Reading - Learning like a Child: Fast Novel Visual Concept Learning from Sentence Descriptions of Images ( ICCV 2015 )

    Link of the Paper: https://arxiv.org/pdf/1504.06692.pdf Innovations: The authors propose the Novel V ...

  4. isX字符串方法

    islower():返回True,如果字符串至少有一个字母,并且所有字母都是小写: 例如:>>> spam='Hello world' >>> spam.islow ...

  5. mweb test

    目录 Markdown syntax guide and writing on MWeb Philosophy Notice Headers This is an <h1> tag Thi ...

  6. 怎么用js精确判断li已经在ul存在过了?

    <ul class="memory_messagelist" id="memory_messagelist"> <li><span ...

  7. 自测之Lesson9:时钟与信号

    题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...

  8. Fox and Number Game

    Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can ...

  9. Thunder团队第二周 - Scrum会议7

    Scrum会议7 小组名称:Thunder 项目名称:i阅app Scrum Master:杨梓瑞 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...

  10. 11.24Daily Scrum(4)

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.1007 实现视频浏览的功能 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.1008 实现视频浏 ...