2018 Multi-University Training Contest 1 - B Balanced Sequence (贪心)
题意:对N个由(,)组成的字符串,求拼接后得到的最大的balance序列的长度。balance序列:空串/ A+B(A,B都是b序列)/ (+A+),A为b序列。此三种情况。
分析:在读入N每个字符串时,先将单独一个字符串中的b序列长度提取出来,记录其没有被使用的左弧L和右弧R的数目。因为要使其最后拼接后得到的子序列长度尽可能地大,那么按照贪心的思想,肯定希望左弧多的字符串在前,右弧多的字符串在后,所以要对其排序。但是排序时关键字的比较并不只是简单地比较二者的L与R,而且要先比较自身L与R的大小。
排完序后不断用之前已有的左弧去匹配当前字符串的右弧。
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
const int maxn =1e5+;
struct Node{
int L,R;
bool f; //表示L>=R 和L<R的两种状态
bool operator < (const Node &rhs)const {
if(f){
if(!rhs.f) return true; //L>=R的状态要排在L<R之前
else return R<rhs.R; //状态相同则比较关键字R
}
else{
if(rhs.f) return false; //同理
else return L>rhs.L;
} }
}p[maxn]; //#define LOCAL
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v,tmp,K,cas=;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
int res=;
for(int i=;i<N;++i){
char op[maxn];
scanf("%s",op);
int L=,R=,len=strlen(op);
for(int j=;j<len;++j){
if(op[j]=='(') L++;
else{
if(L){L--;res+=;}
else R++;
}
}
p[i].L = L,p[i].R=R,p[i].f= (L>=R?true:false);
}
sort(p,p+N);
int preL=,tmp;
for(int i=;i<N;++i){
tmp = min(preL,p[i].R);
res+=*tmp;
preL-=tmp;
preL+=p[i].L;
}
printf("%d\n",res);
}
return ;
}
2018 Multi-University Training Contest 1 - B Balanced Sequence (贪心)的更多相关文章
- 2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301 Distinct Values Time Limit: 4000/2000 MS (Java/Ot ...
- 2015 Multi-University Training Contest 1 OO’s Sequence
OO’s Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- hdu6299 Balanced Sequence 贪心
题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...
- 2018 Nowcoder Multi-University Training Contest 2
目录 Contest Info Solutions A. run D. monrey G. transform H. travel I. car J. farm Contest Info Practi ...
- 2018 Nowcoder Multi-University Training Contest 1
Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...
- 2018 Nowcoder Multi-University Training Contest 5
Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ ...
- 2018 Nowcoder Multi-University Training Contest 10
Practice Link J. Rikka with Nickname 题意: 给出\(n\)个字符串,要求依次合并两个串\(s, t\),满足将\(t\)合并到\(s\)中变成\(r\),使得\( ...
- 2015 Multi-University Training Contest 1 - 10010 Y sequence
Y sequence Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Mean: 有连续数列A={1,2,3,4,5,6, ...
- 2016 Multi-University Training Contest 5 Divide the Sequence
Divide the Sequence 题意: 给你一个序列A,问你最多能够分成多少个连续子序列,使得每个子序列的所有前缀和均不小于0 题解: 这题是比赛时候的水题,但我比的时候也就做出这一题, = ...
随机推荐
- WCF系列 Restful WCF
由于项目需要,需要完成移动端与服务端以json格式的数据交互,所以研究了Restful WCF相关内容,以实现ios端,android端与浏览器端能够与后台服务交互. 那么首先我们来了解下什么是Res ...
- net mvc 小目标
1.前台视图去找指定的控制器(非默认) 2.控制器去找指定的视图(非默认)
- asp.net知识汇总-页面跳转Server.Transfer和Response.Redirect
1. Server.Transfer 服务器端跳转 webform1.aspx跳转到webform2.aspx页面 webform1.aspx代码如下: protected void Page_Loa ...
- ASP.NET中Dictionary基本用法实例分析
本文实例讲述了ASP.NET中Dictionary基本用法.分享给大家供大家参考,具体如下: //Dictionary位于System.Collections.Generic命名空间之下 /* * ...
- structure machine learning projects 课程笔记
orthogonalization/ one metric train.dev/test 划分 开发集和测试集一定来自同一分布 onthe same distribution Human leve ...
- SmartStoreNet解图
概述: Ioc: Autofac 1. 通过继承, 对MVC的Controller的加强.
- String, JSONArray , JSONObject ,Map<String, Object> 与对象
String pic = "[{\"picServiceUrl\": \"0f4bb44afb2e48d48b786d3bbdeec283/20180408/6 ...
- C#封装C++DLL(特别是char*对应的string)
1.新建一个C#-Windows-类库(用于创建C#类库(.dll)的项目)类型的工程 2.对于普通C++函数 XXXX_API void cppFun(int i); 在cs代码中添加 [DllIm ...
- 关于在ubuntu平台下使用apt-get命令下载速度太慢的问题解决
1. 进入设置,从哪进都一样找到就行. 2.选择软件与更新(Software and updates,英语估计是这个把) 点击下载自:这个下拉框,选中其它站点,选择镜像 选择阿里的或者搜狐的镜像,然后 ...
- pro_select_roleinfo_p3
DELIMITER | drop procedure if exists pro_select_roleinfo_p3; CREATE PROCEDURE pro_select_roleinfo_p3 ...