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. 解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误

    解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误 今天安装启动nginx的时候报 ...

  2. java简单界面实现

    import javax.swing.JFrame; import javax.swing.JPanel; public class DemoFrame extends JFrame{ public ...

  3. node解析post表单信息

    一共有4种解析方式 urlencoded.json.text .raw 发起请求的form表单中可以设置三种数据编码方式 application/x-www-form-urlencoded.multi ...

  4. Java基础——内部类

    一.什么是内部类 将一个类定义在另一个类里面或者一个方法里面,这样的类称为内部类 内部类所在的类在编译成功后,会出现这样两个class文件:OuterClass.class和OuterClass$In ...

  5. android ActionBar 去掉menu分隔线

    自定义Theme继承原来Theme修改其中的分隔线: <item name="actionBarDivider">@null</item>  低版本设置 & ...

  6. .netcore centos环境搭建实战

    步骤 1. 安装VMware Workstation 下载地址:https://my.vmware.com/cn/web/vmware/info/slug/desktop_end_user_compu ...

  7. Jmeter从文件中读取参数值

    1. 通过函数助手,从本地文件中取值选项->函数助手对话框->选择__CSVRead函数->调用参数其中,函数助手对话框中,第一栏填写本地文件所在地址,第二栏写需要入参的值,有点类似 ...

  8. kafka java动态获取topic并动态创建消费者

    1.获取所有topic package com.example.demo; import java.io.IOException; import java.util.List; import org. ...

  9. Python——数据类型之set

    本篇主要内容 • set集合的特点 • set集合的建立 • set集合的17个内置函数 • set集合的数学运算符号 1.set集合类型的特点. 1.无序 2.不同元素 3.元素必须不可变.(数字, ...

  10. mingw编译opencv2.4.13问题记录

    为了在程序中用regex,升级了我的mingw,结果官网上的GCC版本都到6.3了,之前一直用4.8.换了编译器以后,对opencv2.4.10的引用就出了问题:undefined reference ...