2018杭电暑期多校 hdoj6299 Balanced Sequence

Balanced Sequence

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


 

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>
using namespace std;
const double PI=acos(-1.0);
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define clr(x) memset(x,0,sizeof(x))
struct kuo{
	int l,r;
}sk[100005];
bool cmp1(kuo a,kuo b){
	if(a.l==b.l)
		return a.r>b.r;
	return a.l<b.l;
}
bool cmp2(kuo a,kuo b){
	if(a.r==b.r)
		return a.l<b.l;
	return a.r<b.r;
}
bool cmp3(kuo a,kuo b){
    if(a.r>=a.l&&b.r>=b.l) return a.l<b.l;
    else if(a.r>=a.l) return 1;
    else if(b.r>=b.l) return 0;
    return a.r>b.r;
}
int main(){
	//本地测试
	#ifdef ONLINE_JUDGE
	#else
    freopen("C://Users//yan//Desktop//in.txt","r",stdin);
	#endif
	ios::sync_with_stdio(false);//取消同步
	std::cin.tie(0);//解除cin与cout的绑定,进一步加快执行效率。
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		stack<char> l;
		string s;
//		clr(sk);
		ll ans=0;
		rep(i,0,n){//用栈来预处理() 
			cin>>s;
			int len=s.length();
//			cout<<s<<endl;
			rep(j,0,len){
				if(!l.empty()&&l.top()=='('&&s[j]==')'){
					l.pop();
					ans++;
					continue;
				}
				l.push(s[j]);
			}
			sk[i].l=0;
			sk[i].r=0;
			while(!l.empty()){
				if(l.top()=='(')
					sk[i].r++;
				else
					sk[i].l++;
				l.pop();
			}
		}
		sort(sk,sk+n,cmp1);
		int temp1=0,now=0;//now是(的个数 temp1是第一种答案 
		rep(i,1,n){
			now+=sk[i-1].r;
			if(sk[i].l>now){
				temp1+=now;
				now=0;
			}				
			else{
				now=now-sk[i].l;
				temp1+=sk[i].l;
			}
//			cout<<now<<endl;	
		}
//		cout<<"1----"<<endl;
//		rep(i,0,n){
//			cout<<sk[i].l<<" "<<sk[i].r<<endl;
//		}
		sort(sk,sk+n,cmp2);
		int temp2=0;
		now=0;
		rep(i,1,n){
			now+=sk[i-1].r;
			if(sk[i].l>now){
				temp2+=now;
				now=0;
			}				
			else{
				now=now-sk[i].l;
				temp2+=sk[i].l;
			}	
		}
//		cout<<"2----"<<endl;
//		rep(i,0,n){
//			cout<<sk[i].l<<" "<<sk[i].r<<endl;
//		}
		sort(sk,sk+n,cmp3);
		int temp3=0;
		now=0;
		rep(i,1,n){
			now+=sk[i-1].r;
			if(sk[i].l>now){
				temp3+=now;
				now=0;
			}				
			else{
				now=now-sk[i].l;
				temp3+=sk[i].l;
			}	
		}
//		cout<<"3----"<<endl;
//		rep(i,0,n){
//			cout<<sk[i].l<<" "<<sk[i].r<<endl;
//		}
//		cout<<" temp1  "<<temp1<<" temp2 "<<temp1<<" temp3 "<<temp1<<endl;
		ans+=max(max(temp1,temp2),temp3);//三种排序取最优 
		cout<<ans*2<<endl;
	} 
}

猜你喜欢

转载自blog.csdn.net/remarkableyan/article/details/81190925