hdu多校1002 Balanced Sequence
Balanced Sequence
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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 (≤n≤) -- the number of strings.
Each of the next n lines contains a string si (≤|si|≤) consisting of `(' and `)'.
It is guaranteed that the sum of all |si| does not exceeds ×. Output
For each test case, output an integer denoting the answer. Sample Input )()(()( )
)( Sample Output Source
Multi-University Training Contest Recommend
liuyiding | We have carefully selected several similar problems for you: Statistic | Submit | Discuss | Note
Home | Top Hangzhou Dianzi University Online Judge 3.0
Copyright © - HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.037002(s) query , Server time : -- ::, Gzip enabled
预处理+贪心
最后剩下的左括号的数量为l,右括号的数量为r,
r==0 的在前,之后是l>=r的(r小的在先),
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
struct node
{
int l,r;
}c[];
char t[];
bool cmp(node a,node b)
{
if(a.r==) return ;
if(b.r==) return ;
if(a.l>=a.r&&b.l<b.r) return ;
if(a.l<a.r&&b.l>=b.r) return ;
if(a.l>=a.r&&b.l>=b.r) return a.r<=b.r;
return a.l>=b.l;
}
//按照没有)
//( > ) 要在 ( < )前面
// 如果都是。就按)小的在前面
//左括号多的在前面
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int ans=;
for(int i=;i<n;i++)
{
cin>>t; int len=strlen(t);
c[i].l=c[i].r=;
for(int j=;j<len;j++)
{
if(t[j]=='(')
c[i].l++;
else
{
if(c[i].l)
c[i].l--,ans+=;
else
c[i].r++;
}
}
}
sort (c,c+n,cmp); int L=,R=;
for(int i=;i<n;i++)
{
if(c[i].r<=L) ans+=c[i].r*,L-=c[i].r;
else ans+=*L,L=;
L+=c[i].l;
}
printf("%d\n",ans); } return ;
}
hdu多校1002 Balanced Sequence的更多相关文章
- 18年多校-1002 Balanced Sequence
>>点击进入原题测试<< 思路:自己写没写出来,想不通该怎么排序好,看了杜神代码后补题A掉的.重新理解了一下优先队列中重载小于号的含义,这里记录一下这种排序方式. #inclu ...
- HDU 多校对抗赛 B Balanced Sequence
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU6299 Balanced Sequence (多校第一场1002) (贪心)
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 6299 Balanced Sequence (贪心)
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
- 2015 HDU 多校联赛 5363 Key Set
2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...
- 2015 HDU 多校联赛 5317 RGCDQ 筛法求解
2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目 http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...
- hdu6299 Balanced Sequence 贪心
题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...
随机推荐
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- 将实体类、匿名对象转换为SqlParameter列表
/// <summary> /// <remarks> /// <para>将实体类/匿名对象转换为SqlParameter列表</para> /// ...
- header 格式
headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,* ...
- 详解Vue中watch的高级用法
我们通过实例代码给大家分享了Vue中watch的高级用法,对此知识点有需要的朋友可以跟着学习下. 假设有如下代码: <div> <p>FullName: {{fullName} ...
- web自动化测试与Appuim自动化测试对比
web自动化测试-打开浏览器: from selenium import webdriver driver = webdriver.Chrome() #定义chrome驱动 driver.maximi ...
- 一般删除网页数据和jquery下使用Ajax删除数据的区别
1. 一般删除网页数据 就是指用户在点击删除的时候,会跳转到DeleteUser.ashx一般处理程序中,并且通过get传参的方式传递一个id的参数,然后在后台处理 <a href='Delet ...
- Hexo 的next主题下添加网易云音乐作BGM
首先,你要看看你选中的歌能不能在网页版的网易云音乐生成外链,因为版权保护原因,有些音乐是生不成外链的,比如这样的: 所以,选些可以生成外链的音乐.生成对应的外链 比如这里的重点是HTML代码中的src ...
- asdasda
git://git.coding.net/lick468/wf.git git地址:https://git.coding.net/lick468/wf.git
- Spark SQL笔记
HDFS HDFS架构 1.Master(NameNode/NN) 对应 N个Slaves(DataNode/NN)2.一个文件会被拆分成多个块(Block)默认:128M例: 130M ==> ...
- 解释变量(Explanatory Variable)
转自:http://www.statisticshowto.com/explanatory-variable/ What is an Explanatory Variable? An explanat ...