Balanced Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1320    Accepted Submission(s): 316

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
 
题意:给你n组括号字符串,你可以对着n组字符串进行交换顺序后的首尾拼接,求拼接后最大的balance串长度
balance 串 在题目中的定义就是  () 、(())、()()之类的
题解:记录左括号和右括号的数量,按照左括号大小排序,依次拼接,每次拼接找到最优的拼接方法,O(n)扫一遍即可
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
struct node{
int l,r;
}a[maxn];
bool cmp(node a,node b){
if(a.l==b.l) return a.r<b.r;
return a.l<b.l;
}
int ans[maxn];
int vis[maxn];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
memset(vis,,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a+,a+m+,cmp);
priority_queue<int,vector<int>,greater<int> >q;
for(int i=; i<=n; i++){
ans[i]=;vis[i]=;
q.push(i);
}
int l=a[].l,r=a[].l;
for(int i=;i<=m;i++){
for(;l<a[i].l;l++){
if(vis[l]) q.push(ans[l]);
}
for(;r<=a[i].r;r++){
if(r>=a[i].l){
ans[r]=q.top();q.pop();
vis[r]=;
}
}
}
for(int i=; i<=n; i++) {
printf("%d",ans[i]);
if(i==n) printf("\n");
else printf(" ");
}
}
}

HDU 多校对抗赛 B Balanced Sequence的更多相关文章

  1. HDU 多校对抗赛第二场 1010 Swaps and Inversions

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU 多校对抗赛第二场 1004 Game

    Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. HDU 多校对抗赛 D Distinct Values

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. HDU 多校对抗赛 C Triangle Partition

    Triangle Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Oth ...

  5. HDU 多校对抗赛 A Maximum Multiple

    Maximum Multiple Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 多校对抗赛 J Time Zone

    Time Zone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. hdu多校1002 Balanced Sequence

    Balanced Sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...

  8. HDU6299 Balanced Sequence (多校第一场1002) (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. hdu 6299 Balanced Sequence (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. 以源码安装的lamp环境为依托,源码安装zabbix监控系统

    1.源码安装lamp环境 1)安装httpd, 以源码httpd-2.4.33为基础,解压后,执行./configure --prefix=/usr/local/ --sysconfdir=/etc/ ...

  2. 密码发生器 南阳acm519

    密码发生器 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如 ...

  3. 打印N个真值的所有真值组合

    例:N=2 (true,true),(false,true),(true,false),(false,false) #include<stdio.h> int count=0; void ...

  4. MyBatis的笔记

    1.#{}和${}的区别是什么? #{}是预编译处理,${}是字符串替换. #{}是sql的参数占位符,${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替 ...

  5. python爬取数据需要注意的问题

    1 爬取https的网站或是接口的时候,如果是不受信用的SSL证书,会报错,需要添加如下代码,如下代码可以保证当前代码块内所有的请求都自动屏蔽ssl证书问题: import ssl # 这个是爬取ht ...

  6. PostgreSQL字段名和表名大小写的问题

    创建表的时候,表名和字段名必须全小写,然后查询的时候不管全大写或全小写,或是Camel模式都不会报错.只要名称中有大写字母,或者全大写,查询时就必须保证大小写正确并用双引号包起来,否则就会报“XXX不 ...

  7. 『AngularJS』ngValue

    原文 描述 绑定给定的表达式到input[select]或input[radio]的值,以便当这个元素被选中的时候,设置这个元素的ngModel到绑定的值.当需要使用ng-repeat来动态生成rad ...

  8. (1)分布式下的爬虫Scrapy应该如何做-安装

    关于Scrapy的安装,网上一搜一大把,一个一个的安装说实话是有点麻烦,那有没有一键安装的?答案显然是有的,下面就是给神器的介绍: 主页:http://conda.pydata.org/docs/ 下 ...

  9. AutoMapper.RegExtension 介绍

    AutoMapper.RegExtension 为一个特小特小特小的用来根据约定自动调用AutoMapper中的方法配置映射的扩展库.你可以引入该库也可以将源码中核心部分的代码文件夹整个拷贝至项目中. ...

  10. Android 上能提高学习工作效率的应用

    在知乎上有朋友问 Android 上能提高学习.工作效率的应用有哪些?我给他们的推荐获得了最多赞同.以后会不断完善更新此贴. Any.do :规划日程,各平台都有. Evernote:记笔记,各平台都 ...