• 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;
    }
  • 相关阅读:
    在美国,一名 Uber 司机能赚多少?
    多名Uber司机被指刷单遭封号 一周薪水为0
    腾讯内推【腾讯工作机会内推】【腾讯社招内推】(长期有效)
    优步uber司机怎么注册不了?注册优步司机问题要点
    如何注册成为uber司机 快速成为优步司机网上注册流程攻略 2015最新
    Google AdSense的CPC点击单价超百度联盟(2014)
    非流动资产(财务报表解读)
    资产负债率
    流动资产(财务报表解读)
    UBER人民优步司机注册攻略
  • 原文地址:https://www.cnblogs.com/2014slx/p/9362576.html
Copyright © 2020-2023  润新知