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类(五)-反射
反射即通过字符串映射或修改程序运行时的状态.属性.方法 有4个方法: hasattr(): hasattr(object,string):object为实例化的对象,string为字符串 判断对象ob ...
- MessageBox如何输出整数
int cx=10;CString s;s.Format(_T("整数是:%d"),cx);MessageBox(s);
- MySessionFactory
package com.ORM; import org.hibernate.HibernateException; import org.hibernate.Session; import org.h ...
- Node.js的__dirname,__filename,process.cwd(),./的含义
简单说一下这几个路径的意思,: __dirname: 获得当前执行文件所在目录的完整目录名 __filename: 获得当前执行文件的带有完整绝对路径的文件名 process.cwd():获得当前执行 ...
- koa1链接mongodb
1.项目下安装mongodb和mongoose npm install mongodb --save-dev npm install mongoose --save-dev 2.router中 var ...
- Ajax02 什么是json、json语法、json的使用、利用jQuery实现ajax
目录 1什么是json 2json语法 3json的使用 4利用jQuery实现ajax编程 1 什么是json JavaScript Object Notation(JavaScript 对象表示法 ...
- function几种自执行的形式
1.(function(){})();这种是最常用的形式 2.var t = function(){}(); 3.-function(){}(); 这三种形式都能自执行
- Git 之 协同开发
GitHub中多人协同开发和单人开发还是有点差别,协同开发一般有两种方式: 合作者,将其他用户添加到仓库合作者中之后,该用户就具有向当前仓库提交代码. 组织,创建一个组织,然后再该组织下可以创建多个项 ...
- HDU 5033 Building (维护单调栈)
题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...
- 【Arcgis android】 离线编辑实现及一些代码段
Arcgis android 离线编辑实现及一些代码段 底图添加 private String path="file:///mnt/sdcard/data/chinasimple.tpk&q ...