Balanced Sequence

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

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 <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#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 pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define FO freopen("in.txt", "r", stdin)
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a))
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn = ;
int _, n;
struct node{
int l, r, ans;// 左, 右, 单匹配
bool operator < (node &x) const {//排序很重要
if(r >= l && x.r < x.l) return false; //右括号少的排前面
if(r < l && x.r >= x.l) return true;
if(r >= l && x.r >= x.l) return l > x.l ; //都是右括号大于左括号 左括号多的排前面
return r < x.r; // 都是左括号大于右括号 右括号少的排前面
}
}a[maxn]; char s[maxn];
void solve() {
scanf("%d", &n);
rep(i, , n) {
a[i].l = a[i].r = a[i].ans = ;
scanf("%s", s);
int len = strlen(s);
rep(j, , len) {
if(s[j] == '(')
a[i].l++;
else {
if(a[i].l > )
a[i].l--, a[i].ans++;
else
a[i].r++;
}
}
}
sort(a, a+n);
int now = ;//维护 (
int sum = ;
rep(i, , n) {
if(a[i].r > now)//如果 ‘)’ 大于 ‘(’
a[i].r = now; //最多匹配 的是 ‘(’ 若不大于的话,就不用变
sum += a[i].r + a[i].ans;//然后累加
now -= a[i].r;//更新 当前的 ‘(’ 数量
now += a[i].l;
}
printf("%d\n", * sum);
} int main() {
for(scanf("%d", &_);_;_--) {
solve();
}
}

这个排序看了好久。我理解的是 a[i] 与 x比较。 由于前两种能分出来谁前谁后,所以返回false true. 比如第一个是false  就是a[i] >= x   也就是x排在前面。  后两种不能直接看出来,就return 大小关系。

fasle 就是 与重载运算符相反的, true就是相同的。——————————————————————————————个人看法。望纠正

HDU6299 Balanced Sequence (多校第一场1002) (贪心)的更多相关文章

  1. HDU6299(2018多校第一场)

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6299 两个字符串的排序可以分成四种情况: (1)str1左少右多 vs str2 左多右 ...

  2. 2019年杭电多校第一场 1002题Operation(HDU6579+线性基)

    题目链接 传送门 题意 初始时有\(n\)个数,现在有\(q\)次操作: 查询\([l,r]\)内选择一些数使得异或和最大: 在末尾加入一个数. 题目强制在线. 思路 对于\(i\)我们记录\([1, ...

  3. 2019HDU多校第一场 String 贪心

    题意:给你一个字符串,问是否存在一个长度为m的子序列,子序列中对应字符的数目必须在一个范围内,问是否存在这样的字符串?如果存在,输出字典序最小的那个. 思路:贪心,先构造一个序列自动机,序列自动机指向 ...

  4. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  5. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  6. 牛客多校第一场 B Inergratiion

    牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...

  7. HDU6581 Vacation (HDU2019多校第一场1004)

    HDU6581 Vacation (HDU2019多校第一场1004) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6581 题意: 给你n+1辆汽车, ...

  8. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  9. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

随机推荐

  1. C Primer Plus学习笔记(四)- 运算符、表达式和语句

    基本运算符 赋值运算符:= 在C语言中,=不是“相等”,而是赋值运算符,把左边的值赋给右边的变量 a = 2018; //把值2018赋给变量a 赋值表达式语句的目的是把值储存到内存位置上,用于储存值 ...

  2. PHP函数(六)-匿名函数(闭包函数)

    匿名函数能够临时创建一个没有名称的函数,常用作回调函数参数的值 <?php $test = function($a){ echo "Hello,".$a; }; $test( ...

  3. hive删除表报错:Specified key was too long; max key length&nb

    我是在hive删除表的时候出现这个错误的,看到这个错误应该就知道是字符集错误. 但是我用​ alter database hive character set latin1; 这个命令将其改成拉丁之后 ...

  4. ZROI2018普转提day6t1

    传送门 分析 记录区间最大值,线段树上二分找比这个点大的最靠前位置即可 代码 #include<iostream> #include<cstdio> #include<c ...

  5. Luogu 3457 [POI2007]POW-The Flood

    感觉自己什么题都写不动了. 又是一个神贪心:把所有城市中的点按照高度从小到大排序之后拿出来逐个计算,枚举其他高度小于它的点向四周扩展,如果这个点不能被之前放过的抽水机覆盖,那么把答案加一,并在这个点放 ...

  6. Redis应用(django)

    自定义使用redis 创建url 定义单例模式连接池 import redis # 连接池 POOL = redis.ConnectionPool(host='10.211.55.4', port=6 ...

  7. Excel神技能

    按住ALT再按数字41420就可打对号 按住ALT再按数字41409就可打叉号

  8. Excel课程学习第三课排序与替换

    一.排序 1.简单排序 点到某一个单元格,然后选择排序,就可以按照相应的顺序来排序. 2.自定义排序 按照重要性条件来排序 也可以按照重要性从轻到重挨个排序. 3.按颜色排序 4. 按照中文数字排序, ...

  9. repo的一些用法

    repo的用法注:repo只是google用Python脚本写的调用git的一个脚本,主要是用来下载.管理Android项目的软件仓库.(也就是说,他是用来管理给git管理的一个个仓库的) 1.下载r ...

  10. 使用metasploit进行栈溢出攻击-4

    有了漏洞我们就可以进行攻击了.首先我们需要了解metasploit的exploit模块,具体可以看 http://www.offensive-security.com/metasploit-unlea ...