HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence
+ 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.
)(
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h> using namespace std; struct node
{
int l, r;
node() {}
node(int a, int b) { l = a, r = b; }
bool operator < (const node &a) const
{
if (r >= l && a.r >= a.l)
return l>a.l;
else if (l > r && a.l <= a.r)
return true;
else if (l <= r && a.l > a.r)
return false;
else
return r < a.r;
}
}node[]; int main()
{
int t;
scanf("%d", &t); while (t--)
{
int n;
long long num = ;
scanf("%d", &n);
char ch[];
for (int i = ; i < n; i++)
{
scanf("%s", ch);
int len = strlen(ch);
int l = , r = ;
for (int j = ; j < len; j++)
{
if (ch[j] == '(')
l++;
else
{
if (l)
{
l--;
num += ;
}
else
r++;
} node[i].l = l;
node[i].r = r;
}
}
sort(node, node + n);
//for (int i = 0; i < n; i++)
//cout << node[i].l << ends << node[i].r << endl;
int l = ;
for (int i = ; i < n; i++)
{
int r = node[i].r;
if (l&&r)
{
int x = min(l, r);
num += x * ;
l -= x;
}
l += node[i].l;
}
cout << num << endl;
}
return ;
}
HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence的更多相关文章
- HDU6333-2018ACM暑假多校联合训练1002-Harvest of Apples-莫队+费马小定理
题意很简单啦,求S(n,m)的值 通过打表我们可以知道 S(n + 1, m) = S(n, m) * 2 - C(n, m); S(n - 1, m) = (S(n, m) + C(n - 1, m ...
- HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造
Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和
题意是给了一种矩阵的生成方式 让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值) 没看标程之前硬撸写了160行 用了前缀和以后代码量缩短到原来的1/3 根据规律可以推导出这个矩阵是在不断重复 ...
- HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories
Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262 ...
- HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube
就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...
- HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组
本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y) 实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大, ...
- HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple
题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...
- HDU6301-2018ACM暑假多校联合训练1004-Distinct Values
题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...
- HDU6308-2018ACM暑假多校联合训练1011-Time Zone
题目大意就是给你UTC-8时区的时间 让你求对应时区的时间 哇 这个题 看似简单,但是一开始怎么都过不了啊 同学用自己写的read过了,后来看了一下各位大佬说改成分钟随便过,就随便过了 Problem ...
随机推荐
- 第一章 走进Java(待续)
·········
- Maven Assembly打包提示[WARNING] transitive dependencies if any will not be available
maven assembly打包出现错误 [WARNING] The POM for com.flink.xxr:0.0.1-SNAPSHOT is invalid, transitive depen ...
- leetcode454
public class Solution { public int FourSumCount(int[] A, int[] B, int[] C, int[] D) { var dic = new ...
- laravel中的模型关联之(一对一)
一对一 一对一的关联关系就相当于,赞和文章之间的关系,一个用户只能赞一次一个文章,只能和文章发生一次关系, 用户和赞的文章是唯一的一份,这里举得是判断用户是否赞过某个文章 一个参数都是你要获取的模型, ...
- laravel中的数据库操作(增删改查)方法一
导入命名空间和DBnamespace App\Http\Controllers; use Illuminate\Support\Facades\DB; public function index(){ ...
- 717. 1-bit and 2-bit Characters最后一位数是否为0
[抄题]: We have two special characters. The first character can be represented by one bit 0. The secon ...
- python 输入参数解包,模块导入,接收IO输入参数
#coding=utf-8 from sys import argv script,first,second,third = argv print "the script is=" ...
- Logistic Regression 用于预测马是否生病
1.利用Logistic regression 进行分类的主要思想 根据现有数据对分类边界线建立回归公式,即寻找最佳拟合参数集,然后进行分类. 2.利用梯度下降找出最佳拟合参数 3.代码实现 # -* ...
- Linux下安装memcache PHP扩展
[root@centos memcache-2.2.4]# wget http://pecl.php.net/get/memcache-2.2.4.tgz [root@centos memcache- ...
- (转)基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用
原文地址:http://www.cnblogs.com/wuhuacong/p/3317223.html 在前面介绍了两篇关于我的基于MVC4+EasyUI技术的Web开发框架的随笔,本篇继续介绍其中 ...