HDU6299 Balanced Sequence (多校第一场1002) (贪心)
Balanced Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6207 Accepted Submission(s): 1616
+ 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
#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) (贪心)的更多相关文章
- HDU6299(2018多校第一场)
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6299 两个字符串的排序可以分成四种情况: (1)str1左少右多 vs str2 左多右 ...
- 2019年杭电多校第一场 1002题Operation(HDU6579+线性基)
题目链接 传送门 题意 初始时有\(n\)个数,现在有\(q\)次操作: 查询\([l,r]\)内选择一些数使得异或和最大: 在末尾加入一个数. 题目强制在线. 思路 对于\(i\)我们记录\([1, ...
- 2019HDU多校第一场 String 贪心
题意:给你一个字符串,问是否存在一个长度为m的子序列,子序列中对应字符的数目必须在一个范围内,问是否存在这样的字符串?如果存在,输出字典序最小的那个. 思路:贪心,先构造一个序列自动机,序列自动机指向 ...
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- 牛客多校第一场 B Inergratiion
牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...
- HDU6581 Vacation (HDU2019多校第一场1004)
HDU6581 Vacation (HDU2019多校第一场1004) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6581 题意: 给你n+1辆汽车, ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
随机推荐
- python-xlrd 实现excel 导入数据
首先安装 xlrd 两种方式: 1.wheel 方式 安装: 首先要下载 wheel :
- java selenium webdriver第一讲 seleniumIDE
Selenium是ThoughtWorks公司,一个名为Jason Huggins的测试为了减少手工测试的工作量,自己实现的一套基于Javascript语言的代码库 使用这套库可以进行页面的交互操作, ...
- maven依赖的添加
maven可是个管理jar依赖的好玩意,不用再关心导这个jar包那个jar包,这个jar包是谁家的,和谁有啥关系.有了maven,简简单单就搞定,下面以eclipse为例,在一个springboo ...
- 空中楼阁 ( House )最短路
题目描述: 话说Z4阴差阳错地来到了神秘岛.不久,他们发现,这是一个由n个小岛和一个中心岛组成的群岛,群岛之间有m座桥.令他们感到惊讶的是,这些桥并不是固定不变的,经较长时间的观察,发现它们会随时间作 ...
- eclipse中server location为灰色,不能修改
Eclipse中tomcat service设置发布时间︰选择window ----show view---services可以看到服务的面板双击tomcat进入配置界面Service Locatio ...
- 卸载sql2008r2简易版
Sql Server 2008完全卸载方法(其他版本类似)第1/2页作者: 字体:[增加 减小] 类型:转载 本文介绍如何卸载 Microsoft SQL Server 2008的方法.当您按照本文中 ...
- 在异步回调中调用MessageBox.Show
public static void Test() { ThreadStart aThreadStart = delegate() { ); MessageBox.Show("Good!&q ...
- JavaScript的编译原理
尽管通常将 JavaScript 归类为“动态”或“解释执行”语言,但事实上它是一门编译语言.这个事实对你来说可能显而易见,也可能你闻所未闻,取决于你接触过多少编程语言,具有多少经验.但与传统的编译语 ...
- Luogu 4197 Peaks
BZOJ 3545 带权限. 考虑离线,把所有边按照从小到大的顺序排序,把所有询问也按照从小到大的顺序排序,然后维护一个并查集和一个权值线段树,每处理一个询问就把比这个询问的$x$更小的边连上,具体来 ...
- Entity Framework Code-First(21):Automated Migration
Automated Migration: Entity framework 4.3 has introduced Automated Migration so that you don't have ...