hdu6299 Balanced Sequence 贪心
题目大意:给出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
+ 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.
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.
1
)()(()(
2
)
)(
2
hdu6299 Balanced Sequence 贪心的更多相关文章
- HDU6299 Balanced Sequence (多校第一场1002) (贪心)
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 6299 Balanced Sequence(贪心)
题目:给出N个只有左右括号字符串 ,这N个字符串的排列顺序是任意的 , 问按最优的排序后 , 得到最多匹配的括号个数 分析: 我们很容易的想到 字符串)()()(( , 这样的字符串可以精简为)(( ...
- hdu 6299 Balanced Sequence (贪心)
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu多校1002 Balanced Sequence
Balanced Sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...
- HDU5014Number Sequence(贪心)
HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...
- HDU 多校对抗赛 B Balanced Sequence
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- hdu 6299 Balanced Sequence (括号序列,贪心)
大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t). 首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解. 那么这样最终 ...
- Balanced Sequence(毒瘤啊)排序贪心 HDU多校
Problem Description Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type ...
随机推荐
- os模块 os.stat('path/filename') os.path.dirname(path) os.path.exists(path) os.path.join(path1[, path2[, ...]])
提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于 ...
- ORACLE体系结构一 (实例(instance))--ORACLE_SID
数据库实例(也称为服务器Server)就是用来访问一个数据库文件集的一个存储结构及后台进程的集合.它使一个单独的数据库可以被多个实例访问(也就是ORACLE并行服务器-- OPS).实例在操作系统中用 ...
- oracle create user &tablespace & imp
一.表空间 1.创建表空间 CREATE TABLESPACE 空间名称 DATAFILE '文件名1' SIZE 数字M [,'文件名2' SIZE 数字….] EXTENT MANAGEMENT ...
- Mongo client - cross-platform MongoDB management tool
Mongo client for Ubuntu or Windows http://robomongo.org/download.html
- IB使用
A:给控件添加方法或变量. 1.窗口上拖个控件 NSButton 2..点右上那张狗脸(Editor)对上的. 3.右键控件.拖到头文件中. 4 .选择加响应方法或变量.
- Luogu 3979 遥远的国度
树剖已经是人尽皆知的sb题了吗…… 很早以前就想填掉这坑了…… 考虑到树链唯一,进行操作并不会对换根产生影响,那么我们的换根操作只要记下root在哪里就好了 询问的时候分类讨论: 1:root == ...
- backstop无法访问
解决方案:重新build代码,重新启动虚拟机.再等一会儿,就OK了.
- 解决java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext问题
使用ClassPathXmlApplicationContext加载项目时, ClassPathXmlApplicationContext context = new ClassPathXmlAppl ...
- MySQL安装与管理
数据库服务器.数据库和表的关系 –所谓安装数据库服务器,只是在机器上装了一个数据库管理程序,这个管理程序可以管理多个数据库,一般开发人员会针对每一个应用创建一个数据库. –为保存应用中实体的数据,一般 ...
- 自己封装一个MySignal函数,方便以后直接copy.
传统的signal可能会有信号未决或者信号重入或多或少的问题,毕竟这个函数已经很多年了. 所以推荐使用sigaction函数,但是sigaction函数相对signal较为复杂,而且每次要写一大堆.因 ...