• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
2014>
博客园    首页    新随笔    联系   管理    订阅  订阅
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\n",ans);

    }

    return 0;
}

 

posted on 2018-07-24 20:59  2014>  阅读(153)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3