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 ...
随机推荐
- 计算数组arr中所有元素的和
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- css的relative与absolute(一)
relative与absolute是position的两个值,本文对这两个值得关系进行了一个小实验 实验一: 首先定义了两个div元素,代码如下所示: <!doctype html> &l ...
- mysql show status 参数解析
状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...
- Linux 压缩文件 和解压文件
.zip 解压:unzip FileName.zip 压缩:zip FileName.zip DirName .rar 解压:rar -x FileName.zip 压缩:rar -a FileNam ...
- c++primer-p100.用迭代器进行二分法搜索
#include <vector> #include <iostream> using namespace std; int main() { vector<int> ...
- Java开源中文分词类库
IKAnalyzer IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始,IKAnalyzer已经推出了3个大版本.最初,它是以开 ...
- Hibernate和JPA
ORM(Object/Relational Mapping : 对象关系映射)就是利用描述对象和数据库之间映射的元数据,自动(且透明)的将java应用程序中的对象持久化到关系数据库的表中.HIbern ...
- IP地址在mysql的存储
因为int比varchar(15)更高效,且php和mysql都有ip和int互转的函数,所以在ip地址在mysql中用int存储最优. mysql存储这个值是字段需要用int UNSIGNED.不用 ...
- java中怎么把String转化为字符数组呢?
我想让用户输入一组字符串,然后将这个字符串的每一个赋给一个char.即,abcde char ch[]=new char[5]; ch[0]=a;ch[1]=b;..... 解决方案如下图所示: St ...
- eclipse打包第三方jar的超简便方法
原帖地址 http://wenku.baidu.com/link?url=0kCBUc-7g4pGgXUrBA2jdCRkrSQmQzaDgGT59TyECAundF8c6R9yxy4EaBN3L8c ...