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, ...
随机推荐
- List<Model>转String 、String 转List<string>
var ltCode = from item in psw.VehicleInsuranceItem select item.Code; string code = string.Join(" ...
- ImgQuoteUIWindow
using System;using UnityEngine;using UnityEngine.UI;using UnityEditor;using System.Collections;using ...
- Oracle(限定查询2)
3.2 对数据进行限定查询 在标准SQL之中定义了许多的运算符. 3.2.1.关系运算符 范例: 范例: 范例: 在使用关系运算符判断字符数据的时候注意大小写的编写问题.因为Oracle是区分大小写的 ...
- 重装win7系统并激活
备份 大白菜制作启动盘 下载大白菜软件UEFI版(新电脑使用uefi版本,装机版支持的主板多) 选择默认安装 选择默认模式开始制作 下载iso镜像文件,复制到u盘(手动复制) 设置bios ...
- The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet- mapping*,session-config?,mime-map
修改了一下web.xml,加入了一个<filter>,然后就报这样的错??? The content of element type "web-app" must ma ...
- 学习笔记44—Linux下安装freesurfer
第一步:安装ubuntu (略过) 第二步:下载freesurfer:从freesurfer的官方网站上下载:http://surfer.nmr.mgh.harvard.edu/fswiki/Down ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J Ka Chang
Ka Chang 思路: dfs序+树状数组+分块 先dfs处理好每个节点的时间戳 对于每一层,如果这一层的节点数小于sqrt(n),那么直接按照时间戳在树状数组上更新 如果这一层节点个数大于sqrt ...
- Codeforces 686 D - Kay and Snowflake
D - Kay and Snowflake 思路: 树的重心 利用重心的一个推论,树的重心必定在子树重心的连线上. 然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心 ...
- CentOS 7系统安装配置图解教程
操作系统:CentOS 7.3 备注: CentOS 7.x系列只有64位系统,没有32位.生产服务器建议安装CentOS-7-x86_64-Minimal-1611.iso版本 一.安装CentOS ...
- 放弃 Tightvnc, 选择 Tigervnc
构建headless vnc server ,我终于放弃了Tightvnc 基于以下原因: 1) 已知的Qt5的键盘映射问题,导致virtualbox 的使用出现困难 https://unix.sta ...