zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    echarts中如何使用timeline组件
    vs发布项目webconfig替换语法
    [置顶] MVC输出缓存(OutputCache参数详解)
    signalr中Group 分组群发消息的简单使用
    echarts异步数据加载(在下拉框选择事件中异步更新数据)
    自定义bootstrap样式-9行样式自定义漂亮大气bootstrap导航栏
    OpenCvSharp 图像旋转
    mybatis获取刚刚插入到数据库的数据的id(转载)
    axios 发 post 请求,后端接收不到参数的解决方案(转载)
    sql 时间获取
  • 原文地址:https://www.cnblogs.com/2014slx/p/9362576.html
Copyright © 2011-2022 走看看