hdu多校1002 Balanced Sequence

Posted 2014slx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu多校1002 Balanced Sequence相关的知识,希望对你有一定的参考价值。

Balanced Sequence
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3280    Accepted Submission(s): 846


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
 

Source
2018 Multi-University Training Contest 1
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6308 6307 6306 6305 6304 
 

Statistic | Submit | Discuss | Note
Home | Top    Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2018 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.037002(s) query 6, Server time : 2018-07-24 20:55:23, 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[100010];
char t[100000];
bool cmp(node a,node b)
{
    if(a.r==0) return 1;
    if(b.r==0) return 0;
    if(a.l>=a.r&&b.l<b.r) return 1;
    if(a.l<a.r&&b.l>=b.r) return 0;
    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=0;
    for(int i=0;i<n;i++)
    {
       cin>>t;

        int len=strlen(t);
        c[i].l=c[i].r=0;
        for(int j=0;j<len;j++)
        {
            if(t[j]==()
                c[i].l++;
            else
            {
            if(c[i].l)
                c[i].l--,ans+=2;
            else
                c[i].r++;
            }
        }
    }
        sort (c,c+n,cmp);

         int L=0,R=0;
        for(int i=0;i<n;i++)
        {
            if(c[i].r<=L) ans+=c[i].r*2,L-=c[i].r;
            else ans+=2*L,L=0;
            L+=c[i].l;
        }
        printf("%d
",ans);

    }

    return 0;
}

 

以上是关于hdu多校1002 Balanced Sequence的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5289 Assignment(多校联合第一场1002)

hdu多校5

hdu 2018 多校 第五场

2017多校Round10(hdu6171~hdu6181)

HDU多校第一场

2017多校Round2(hdu6045~hdu6055)