HDU 6299 (贪心)

Balanced Sequence

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


 

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

 

贪心。

对每个串去掉合法的结果后,一定是“))))))((((((("这样的结构

对于相邻的两个串f1,f2,如果f1在f2前面,一定有min(a.l,b.r)>min(a.r,b.l)

如果这两个值相等,就把左括号多的放前面

#include<cstdio>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,Left[100005],Right[100005],f[100005];
char s[100005];
void swap(int &a,int &b){
    int t=a;
    a=b;
    b=t;
}
int min(int a,int b){
    if(a<b) return a;
    return b;
}
/*void qsort(int l,int r){
    int i=l,j=r,xl=left[(l+r)>>1],xr=right[(l+r)>>1];
    do{
        while(left[i]-right[i]>xl-xr) i++;
        while(left[j]-right[j]<xl-xr) j--;
        if(i<=j){
            swap(left[i],left[j]);
            swap(right[i],right[j]);
            i++;
            j--;
        }
    }while(i<=j);
    if(l<j) qsort(l,j);
    if(i<r) qsort(i,r);
}*/
struct node{
    ll x,y;
}temp[100050];
int cmp(node a,node b){
    if (min(a.x,b.y)==min(a.y,b.x)){
        return a.x>b.x;
    }
    return min(a.x,b.y)>min(a.y,b.x);
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int ans=0;
        for(int i=1;i<=n;i++){
            scanf("%s",s);
            Left[i]=0;
            Right[i]=0;
            for(int j=0;s[j]!='\0';j++){
                if(s[j]=='(') Left[i]++;
                else{
                    if(Left[i]==0) Right[i]++;
                    else{
                        Left[i]--;
                        ans+=2;
                    }
                }
            }
        }
        for (int i=1;i<=n;i++){
           // cout<<i<<" "<<Left[i]<<" "<<Right[i]<<endl;
            temp[i].x=Left[i];
            temp[i].y=Right[i];
        }
        sort(temp+1,temp+n+1,cmp);
       /* for (int i=1;i<=n;i++){
            cout<<i<<" "<<temp[i].x<<" "<<temp[i].y<<endl;
        }*/
        //qsort(1,n);
        int l=0,r=0;
        /*for(int i=1;i<=n;i++){
            int tmp=min(l,right[i]);
            l-=tmp;
            right[i]-=tmp;
            ans+=tmp*2;
            l+=left[i];
            r+=right[i];
        }*/
        for(int i=1;i<=n;i++){
            int tmp=min(l,(int)temp[i].y);
            l-=tmp;
            temp[i].y=tmp;
            ans+=tmp*2;
            l+=temp[i].x;
            r+=temp[i].y;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wyj_alone_smile/article/details/81176950