【贪心】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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值