hdu 6299 Balanced Sequence( 2018 Multi-University Training Contest 1 )
2024-09-24 19:18:58
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; #define Faster ios::sync_with_stdio(false),cin.tie(0)
#define Read freopen("in.txt", "r", stdin),freopen("out.txt", "w", stdout)
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + ;
const int MOD = 1e9 + ; struct node{
int left, right;
bool operator < (const node& x) const {
if(left <= right && x.right < x.left) return false;
else if(left > right && x.right >= x.left) return true;
if(left <= right && x.right >= x.left) return left > x.left;
else return right < x.right;
}
}a[maxn]; int main()
{
Faster;
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int ans = ;
for(int i = ;i < n;i++){
string s;
cin >> s;
a[i].left = a[i].right = ;
for(int j = ;j < s.size();j++){
if(s[j] == '('){
a[i].left++;
}
else if(s[j] == ')'){
if(a[i].left > ){
a[i].left--;
ans += ;
}
else{
a[i].right++;
}
}
}
}
sort(a, a+n);
int now = ; //记录有多少左括号没有匹配
for(int i = ;i < n;i++){
if(a[i].right > now)
a[i].right = now;
ans += a[i].right*;
now -= a[i].right;
now += a[i].left;
}
cout << ans << endl;
}
return ;
}
最新文章
- .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
- PL/SQL导出到execl中,数据前面的0发生丢失的解决办法
- java中是否会存在内存泄漏
- Webform——中国省市三级联动以及IsPostBack
- Linq之Lambda表达式
- 用命令访问D:\python学习\wendjia教程\aa.py
- linux工程管理工具make入门
- js中跨域请求原理及2种常见解决方案
- 关于VS 2010 RDLC 报表的详细使用说明
- SE 2014 年4月21日(一)
- 《JavaScript高级程序设计》读书笔记 ---Array 类型
- Spring学习笔记(二)之装配Bean
- alibaba/druid 下的 密码加密
- tornado 初解
- react-native中的动画
- C#循环语句整理
- .Net转Java.01.从Main(main)函数说起
- springboot启动配置原理之一(创建SpringApplication对象)
- MySQL 性能管理及架构设计指南
- jdbc --- javabean