题意:对N个由(,)组成的字符串,求拼接后得到的最大的balance序列的长度。balance序列:空串/ A+B(A,B都是b序列)/ (+A+),A为b序列。此三种情况。

分析:在读入N每个字符串时,先将单独一个字符串中的b序列长度提取出来,记录其没有被使用的左弧L和右弧R的数目。因为要使其最后拼接后得到的子序列长度尽可能地大,那么按照贪心的思想,肯定希望左弧多的字符串在前,右弧多的字符串在后,所以要对其排序。但是排序时关键字的比较并不只是简单地比较二者的L与R,而且要先比较自身L与R的大小。

排完序后不断用之前已有的左弧去匹配当前字符串的右弧。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
const int maxn =1e5+;
struct Node{
int L,R;
bool f; //表示L>=R 和L<R的两种状态
bool operator < (const Node &rhs)const {
if(f){
if(!rhs.f) return true; //L>=R的状态要排在L<R之前
else return R<rhs.R; //状态相同则比较关键字R
}
else{
if(rhs.f) return false; //同理
else return L>rhs.L;
} }
}p[maxn]; //#define LOCAL
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v,tmp,K,cas=;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
int res=;
for(int i=;i<N;++i){
char op[maxn];
scanf("%s",op);
int L=,R=,len=strlen(op);
for(int j=;j<len;++j){
if(op[j]=='(') L++;
else{
if(L){L--;res+=;}
else R++;
}
}
p[i].L = L,p[i].R=R,p[i].f= (L>=R?true:false);
}
sort(p,p+N);
int preL=,tmp;
for(int i=;i<N;++i){
tmp = min(preL,p[i].R);
res+=*tmp;
preL-=tmp;
preL+=p[i].L;
}
printf("%d\n",res);
}
return ;
}

2018 Multi-University Training Contest 1 - B Balanced Sequence (贪心)的更多相关文章

  1. 2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301 Distinct Values Time Limit: 4000/2000 MS (Java/Ot ...

  2. 2015 Multi-University Training Contest 1 OO’s Sequence

    OO’s Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. hdu6299 Balanced Sequence 贪心

    题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...

  4. 2018 Nowcoder Multi-University Training Contest 2

    目录 Contest Info Solutions A. run D. monrey G. transform H. travel I. car J. farm Contest Info Practi ...

  5. 2018 Nowcoder Multi-University Training Contest 1

    Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...

  6. 2018 Nowcoder Multi-University Training Contest 5

    Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ ...

  7. 2018 Nowcoder Multi-University Training Contest 10

    Practice Link J. Rikka with Nickname 题意: 给出\(n\)个字符串,要求依次合并两个串\(s, t\),满足将\(t\)合并到\(s\)中变成\(r\),使得\( ...

  8. 2015 Multi-University Training Contest 1 - 10010 Y sequence

    Y sequence Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Mean: 有连续数列A={1,2,3,4,5,6, ...

  9. 2016 Multi-University Training Contest 5 Divide the Sequence

    Divide the Sequence 题意: 给你一个序列A,问你最多能够分成多少个连续子序列,使得每个子序列的所有前缀和均不小于0 题解: 这题是比赛时候的水题,但我比的时候也就做出这一题, = ...

随机推荐

  1. SQL宝典

    SQL Server 数据库的高级操作 (1) 批处理 (2) 变量 (3) 逻辑控制 (4) 函数 (5) 高级查询 */ (1)批处理 将多条SQL语句作为一个整体去编译,生成一个执行计划,然后, ...

  2. 关于TextView的一些初步解说

    Android里面的textview是一个相当重要的类.相信做安卓的开发人员在每一个应用里面都一定用到了它,而且它也是Button,EditTextView等子控件的父类. 对于View的流程:mea ...

  3. 20个JS 小技巧超级实用

    1. 将彻底屏蔽鼠标右键 oncontextmenu=”window.event.returnValue=false” < table border oncontextmenu=return(f ...

  4. ICO图标的制作与应用

    制作参看:http://www.shouce.ren/tool/ico?action=make 示例: <link href="./js/favicon.ico" rel=& ...

  5. chrome vim设置

    chrome vi 插件:Vrome 配置文件如下 set hintkeys=asdfghjkl;" 把默认使用数字来完成提示改为使用键盘上的这几个键map j 10jmap k 10k&q ...

  6. spotlight on windows 监控

    1. spotlight on windows 安装 下载 https://pan.baidu.com/s/1qYi3lec Spotlight大家可以从其官方网站(http://www.quest. ...

  7. 解决百度地图Fragment切换黑屏问题

    https://blog.csdn.net/rentalphang/article/details/52076330 轻松解决啦!

  8. 自定义字体TextView

    /** * 备注: * 作者:王莹 * 时间:2017/5/4. * ~_~想睡觉了!! * (-o-)~zZ我想睡啦- * π_π?打瞌睡 */ public class FontsTextView ...

  9. ng-template

    一,锚点链接 定义锚点: <a href="#login" class="btn btn-primary">Login</a> < ...

  10. 【BZOJ2653】middle 二分+可持久化线段树

    [BZOJ2653]middle Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个 长度为n的序列s.回答Q个 ...