HDU 6299.Balanced Sequence-贪心、前缀和排序 (2018 Multi-University Training Contest 1 1002)
HDU6299.Balanced Sequence
这个题就是将括号处理一下,先把串里能匹配上的先计数去掉,然后统计左半边括号的前缀和以及右半边括号的前缀和,然后结构体排序,然后遍历一遍,贪心策略走一遍就可以了。
但是我写的时候排序写挫了,左(括号)多右(括号)少的和左少右多的,肯定左多的在前面,左少右多和左多右少的,肯定左多的在前面,左少右多和左少右多,谁的左边的多谁在前面,以及其他情况都按右少的前排,按这四种情况考虑就可以完美贪心了。只是可惜自己智障,其他就没什么了。
代码:
//1002-6299-贪心,前缀和,排序,使得匹配数量最多,排序写挫了,是真的菜
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cassert>
#include<set>
#include<queue>
#include<vector>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f; struct node{
int l,r; bool operator< (const node &a) const{
if(l>r&&a.l<=a.r) return true;//左多右少 左少右多
if(l<=r&&a.l>a.r) return false;//左少右多 左多右少
if(l<=r&&a.l<=a.r) return l>a.l;//左少右多 左少右多
return r<a.r;//其他都按右边排
} }a[maxn]; char s[maxn];
int main()
{
int t;scanf("%d",&t);
while(t--){
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
a[i].l=a[i].r=;
int ans=;
for(int i=;i<=n;i++){
scanf("%s",s);
int len=strlen(s);
for(int j=;j<len;j++){
if(s[j]=='(') a[i].l++;
else{
if(a[i].l>) a[i].l--,ans++;
else a[i].r++;
}
}
}
sort(a+,a++n);
int lsum=;
for(int i=;i<=n;i++){
if(a[i].r<=lsum) ans+=a[i].r,lsum-=a[i].r;
else ans+=lsum,lsum=;
lsum+=a[i].l;
}
printf("%d\n",ans*);
}
return ;
}
HDU 6299.Balanced Sequence-贪心、前缀和排序 (2018 Multi-University Training Contest 1 1002)的更多相关文章
- 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 6299 Balanced Sequence (括号序列,贪心)
大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t). 首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解. 那么这样最终 ...
- hdu 6299 Balanced Sequence(贪心)题解
题意:题意一开始不是很明白...就是他给你n个串,让你重新排列组合这n个串(每个串内部顺序不变),使得匹配的括号长度最大.注意,题目要求not necessary continuous,括号匹配不需要 ...
- hdu 6299 Balanced Sequence( 2018 Multi-University Training Contest 1 )
#include <stdio.h> #include <iostream> #include <cstdlib> #include <cmath> # ...
- hdu6299 Balanced Sequence 贪心
题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...
- hdu 6047 Maximum Sequence 贪心
Description Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: ...
- HDU 6047 Maximum Sequence(贪心+线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- HDU 6047 Maximum Sequence (贪心+单调队列)
题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由 ...
随机推荐
- jQuery监控动画执行完毕事件
$('#yourElement').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend' ...
- python-使用pip安装第三方库报UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 7: invalid continuation byte 错误解决方案
在python 的安装目录下找到Lib\ntpath.py文件,找到def join(path, *paths):方法,添加如下两行语句: reload(sys) sys.setdefaultenco ...
- ironic-conductor与ipa交互clean部分代码分析
clean的动作会在provide和delete阶段才会触发 从代码分析: 对节点执行的node provide/deleted/clean会先发送到ironicclient ironicclient ...
- [转载]kd tree
[本文转自]http://www.cnblogs.com/eyeszjwang/articles/2429382.html k-d树(k-dimensional树的简称),是一种分割k维数据空间的数据 ...
- Leetcode with Python -> Sort
349. Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Ex ...
- Flex学习笔记
Flex —— Flexible Box 弹性布局 用来为盒子模型提供灵活性 /* 块级元素 */ .box{ display: flex; } /* 行内元素 */ .box{ display: i ...
- struct&&class 空的大小
#include using namespace std; class ClassA { }; class ClassB { private: int b; }; class ClassC : pub ...
- 原 HBase 常用Shell命令
HBase 常用Shell命令 1.进入hbase shell console $HBASE_HOME/bin/hbase shell 如果有kerberos认证,需要事先使用相应的keytab进行一 ...
- 泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,Queue<T>,Stack<T>等
泛型与非泛型集合类在C#程序中是非常重要的一个基础概念,这里列一个表来进行对比: 非泛型集合类 泛型集合类 描述 ArrayList List<T> 表示具有动态大小的对象数组 Hasht ...
- angularjs的service
1.首先我们创建一个模块 var module = angular.module( "my.new.module", [] ); 2.然后写具体的service 可以看到它是一个很 ...