题目传送门

题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度。

思路:

首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b,考虑两个字符串,这两个如果min(a1,b2)小于min(a2,b1)时,第一个字符串是不是应该排在前面呢?如果两个相同的话,当然应该吧右括号比较多的放在前面,左括号比较多的放在后面。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
inline int rd() {
int f = ; int x = ; char s = getchar();
while (s<'' || s>'') { if (s == '-')f = -; s = getchar(); }
while (s >= ''&&s <= '') { x = x * + s - ''; s = getchar(); }x *= f;
return x;
}
inline ll gcd(ll a, ll b) {
if (b == )return a;
return gcd(b, a%b);
}
struct s
{
int a, b;
}z[];
char c[];
bool comp(s a, s b)
{
int x = min(a.a, b.b);
int y = min(a.b, b.a);
if (x == y)
{
if (a.a == b.a)
{
return a.b < b.b;
}
return a.a > b.a;
}
return x > y;
}
int main(void)
{
int t, n, i, len, j, a, b;
long long ans;
scanf("%d", &t);
while (t--)
{
ans = ;
scanf("%d", &n);
for (i = ; i < n; i++)
{
scanf("%s", c);
len = strlen(c);
a = ;
b = ;
for (j = ; j < len; j++)
{
if (c[j] == '(')
{
a++;
}
else
{
if (a)
{
a--;
ans++;
}
else
{
b++;
}
}
}
z[i].a = a;
z[i].b = b;
}
sort(z, z + n, comp);
a = ;
b = ;
for (i = ; i < n; i++)
{
if (a >= z[i].b)
{
ans += z[i].b;
a -= z[i].b;
}
else
{
ans += a;
a = ;
}
a += z[i].a;
}
printf("%lld\n", ans * );
}
return ;
}

Balanced Sequence

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

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

hdu6299 Balanced Sequence 贪心的更多相关文章

  1. HDU6299 Balanced Sequence (多校第一场1002) (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. HDU 6299 Balanced Sequence(贪心)

    题目:给出N个只有左右括号字符串 ,这N个字符串的排列顺序是任意的 , 问按最优的排序后 , 得到最多匹配的括号个数 分析: 我们很容易的想到 字符串)()()(( , 这样的字符串可以精简为)(( ...

  3. hdu 6299 Balanced Sequence (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. hdu多校1002 Balanced Sequence

    Balanced Sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...

  5. HDU5014Number Sequence(贪心)

    HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...

  6. HDU 多校对抗赛 B Balanced Sequence

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  8. hdu 6299 Balanced Sequence (括号序列,贪心)

    大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t). 首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解. 那么这样最终 ...

  9. Balanced Sequence(毒瘤啊)排序贪心 HDU多校

    Problem Description Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type ...

随机推荐

  1. DAY2-python数据类型、字符编码、文件处理

    阅读目录 一.引子 二.数字 三.字符串 四.列表 五.元祖 六.字典 七.集合 八.数据类型总结 九.运算符 十.字符编码 十一.文件处理 一.引子 1 什么是数据? x=10,10是我们要存储的数 ...

  2. python爬虫实战(1)--爬取糗事百科

    这里利用正则表达式进行匹配,糗事百科是不需要登录的,所以也没必要用到Cookie,另外糗事百科有的段子是附图的,我们把图抓下来图片不便于显示,那么我们就尝试过滤掉有图的段子. 本篇目标 1.抓取糗事百 ...

  3. myeclipse.ini

    myeclipse10 32位 我的配置 #utf8 (do not remove) #utf8 (do not remove) -startup ../Common/plugins/org.ecli ...

  4. Swing绘图API

    ----------------siwuxie095                             工程名:TestSwingPaintAPI 包名:com.siwuxie095.swing ...

  5. 基于unity3d IFC的虚拟仿真系统

  6. GCD 学习(三)Main&Global Dispatch Queue

    摘录自:http://zhuyanfeng.com/archives/3066 Main Dispatch Queue是在主线程中执行任务的Dispatch Queue.因为主线程只有1个,所以Mai ...

  7. Luogu 4197 Peaks

    BZOJ 3545 带权限. 考虑离线,把所有边按照从小到大的顺序排序,把所有询问也按照从小到大的顺序排序,然后维护一个并查集和一个权值线段树,每处理一个询问就把比这个询问的$x$更小的边连上,具体来 ...

  8. python sort、sorted、reverse、reverd的区别

    sort.sorted.reverse.reversed的区别 !!! error 首先应该区分的是,sort和reverse是列表的一个方法.字符串.元组.字典.集合是没有这两个方法的.而sorte ...

  9. kafka的producer执行卡住的问题

    使用windows开发producer然后向远程的kakfa集群发送数据,但是一直卡着, 在window的hosts文件添加kafka集群的主机名和ip的映射就好了 网上搜了下,大致是producer ...

  10. 分享常用的GoLang包工具

    包名 链接地址 备注 Machinery异步队列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/eclipse/paho.mqt ...