Balanced Sequence(栈+贪心排序)[sort的排序]

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

#include<bits/stdc++.h>

#define lowbit(x) (x&(-x))
#define ls(x)  (x<<1)
#define rs(x)  (x<<1|1)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-a;i>=a;i--)
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> PII;
const int mod=1e9+7;
const int maxn=1e5+10;

struct node{
    int l,r;
    int id;
}p[maxn];
char str[maxn],stk[maxn];


inline void solve(int& ans,int id){
    int top=-1;
    int len=strlen(str);
    rep(i,0,len){
        if(top>-1&&str[i]==')'&&stk[top]=='('){
            ans++;top--;
        }
        else{
                stk[++top]=str[i];
        }
    }
    p[id].l=0,p[id].r=0;
    rep(i,0,top+1){
        if(stk[i]==')')p[id].l++;
        else p[id].r++;
    }
    p[id].id=id;
}

bool cmp(const node& A,const node& B){
	//另一种写法
     if(min(A.r,B.l)==min(A.l,B.r)) return A.r>B.r;
     return min(A.r,B.l)>min(A.l,B.r);
/*
    if(A.l<=A.r&&B.l>B.r)
            return true;
    if(A.l>A.r&&B.l<=B.r)
            return false;
    if(A.l<=A.r&&B.l<=B.r)
        return A.l<B.l;
    return A.r>B.r;
*/
}

int main(){
   // freopen("1002.txt","r",stdin);
   // freopen("123.txt","w",stdout);
    int T;
    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++){
        int n;
        scanf("%d",&n);
        int ans=0;
        rep(i,1,n+1){
            scanf(" %s",str);
            solve(ans,i);
        }
        sort(p+1,p+n+1,cmp);
        int num=0;
        rep(i,2,n+1){
            num+=p[i-1].r;
            if(p[i].l>=num){
                ans+=num;
                num=0;
            }
            else{
                ans+=p[i].l;
                num-=p[i].l;
            }
        }
        printf("%d\n",ans*2);
    }
    return 0;
}

sort排序,用于流水线调度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
常用的外排序算法有以下几种: 1. 归并排序(Merge Sort):归并排序是一种稳定的排序算法,它将待排序的数据分成若干个子序列进行排序,然后将排好序的子序列合并成一个大的有序序列。在外排序中,归并排序通常使用多路归并的方式,将大文件分成多个块,每次从这些块中选取最小的元素进行合并,直到所有元素都被合并为有序序列。 2. 快速排序(Quick Sort):快速排序是一种高效的排序算法,它通过选取一个基准元素,将待排序的数据分成左右两部分,左边的元素都小于等于基准元素,右边的元素都大于等于基准元素,然后递归地对左右两部分进行排序。在外排序中,快速排序通常需要将大文件划分为多个小文件,分别进行排序后再进行合并。 3. 堆排序(Heap Sort):堆排序是一种基于二叉堆数据结构的排序算法,它通过构建最大堆或最小堆来进行排序。在外排序中,堆排序通常使用多路归并的方式,通过构建最小堆来选取最小的元素进行合并。 4. 多路平衡归并(Multiway Balanced Merge):多路平衡归并是一种优化的归并排序算法,它通过将大文件分成多个块,并使用平衡树(如B树)来管理这些块,以减少磁盘的读写次数。多路平衡归并能够有效地利用磁盘的顺序读写特性,提高排序的效率。 5. 外部哈希排序(External Hash Sort):外部哈希排序是一种基于哈希表的排序算法,它将大文件划分为多个块,并使用哈希函数将数据分配到不同的块中进行排序排序完成后,再按照哈希函数的结果进行合并。外部哈希排序适用于关键字分布均匀的情况。 这些算法都是用于对大规模数据进行排序的外排序算法。它们通过合理地划分数据、利用磁盘读写特性和适当的数据结构设计来提高排序效率,并尽量减少对磁盘的读写次数。具体选择哪种算法取决于排序数据的特点和要求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值