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]区间里最多能 ...
随机推荐
- 2015.1.15 利用Oracle函数返回表结果 重大技术进步!
-- sql 调用 select * from table( get_airway_subpoint(x,x,x)) ///////////////////////////////////////// ...
- eclipse中创建maven web项目
本文主要说明将maven web项目转成eclipse支持的web项目. 创建一个maven项目设置打包类型为war则其为web项目 结构如下 将mavenweb项目转成eclipse识别的web项目 ...
- hive删除表报错:Specified key was too long; max key length&nb
我是在hive删除表的时候出现这个错误的,看到这个错误应该就知道是字符集错误. 但是我用 alter database hive character set latin1; 这个命令将其改成拉丁之后 ...
- <c:set var="ctx" value="${pageContext.request.contextPath}" />的学习
${pageContext.request.contextPath},是获取当前根目录 set var="ctx",是给这个路径定义了一个变量,用的时候可以通过EL表达式获取:${ ...
- 使用ffmpeg合并视频
命令: ffmpeg -i concat:"1.avi|2.avi" -vcodec copy -acodec copy "3.avi" ffmpeg下载:ht ...
- day17-jdbc 3.jdbc快速入门
通过java程序操作数据库. 对数据库操作是对记录的操作.记录就是DML和DCL. 只要Java程序跟任何设备进行了连接,用完之后必须释放资源.最简单基础班讲I/O流,Java跟文件进行了连接,用完之 ...
- [poj2318]TOYS(直线与点的位置关系)
解题关键:计算几何入门题,通过叉积判断. 两个向量的关系: P*Q>0,Q在P的逆时针方向: P*Q<0,Q在P的顺时针方向: P*Q==0,Q与P共线. 实际就是用右手定则判断的. #i ...
- Ros问题汇总
1.ImportError: No module named beginner_tutorials.srv 解决: cd ~/catkin_ws $ source devel/setup.bash $ ...
- Condition实现多个生产者多个消费者
Condition实现多对多交替打印: import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.R ...
- Entity Framework Tutorial Basics(6):Model Browser
Model Browser: We have created our first Entity Data Model for School database in the previous secti ...