【贪心】hdu 6299 Balanced Sequence

Balanced Sequence

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

 

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:  6318 6317 6316 6315 6314 

排序函数时:return 1,是按传递进来的 排,return 0,是反着的

//先放全是左括号的,最后放全是右括号的
//如果前者是左括号多,后者是右括号多,就这么放,否则逆序放
//如果都是左括号多,就把右括号少的放前面
//否则就是左括号多的放前面
//都是左括号多的情况下,右括号少的放前面和左括号多的放前面不相等
//如:)))((((   和  ))))(((((((

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn=1e5+10;
char a[maxn];
struct node
{
    int l,r;
}p[maxn];

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,ans=0,L=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) p[i].l=p[i].r=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a);
            int len=strlen(a);
            for(int j=0;j<len;j++)//存储每一个字符串的左右括号数(已经去除了自己可以配对的)
            {
                if(a[j]=='(') p[i].l++;
                else
                {
                    if(p[i].l>0)
                    {
                        p[i].l--;
                        ans+=2;
                    }
                    else
                    {
                        p[i].r++;
                    }
                }
            }
        }
        sort(p+1,p+n+1,cmp);
        for(int i=1;i<=n;i++)
        {
            if(p[i].r<=L)
            {
                ans+=(p[i].r)*2;
                L-=p[i].r;
            }
            else
            {
                ans+=L*2;
                L=0;
            }
            L+=p[i].l;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81227114