题意:给定n个只有左右括号的序列,要求将它们重新排序使得匹配的括号对数最大。

n<=1e5

s[i]<=1e5

sum s[i]<=5e6

思路:

先把每个串内部的匹配数量减去,剩下的就是不匹配的左右括号数量

对于左括号数量大于右括号的串,按右括号数量从小到大排序

对于右括号数量大于左括号的串,按左括号数量从大到小排序

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair int read()
{
int v=, f=;
char c=getchar();
while (c<||<c) {if(c=='-') f=-; c=getchar();}
while (<=c&&c<=) v=(v<<)+v+v+c-, c=getchar();
return v*f;
} const int N=;
int a[N],b[N],n,ans;
char ch[N];
PII A[N],B[N],C[N]; bool cmp1(const PII &a,const PII &b)
{
return a.fi<b.fi;
} bool cmp2(const PII &a,const PII &b)
{
return a.se>b.se;
} int main()
{ int cas=read();
while(cas--)
{
n=read();
int ans=;
for(int i=;i<=n;i++)
{
scanf("%s",ch+);
a[i]=b[i]=;
int now=;
for(int j=;ch[j];j++)
if(ch[j]==')')
{
if(now) now--,ans++;
else a[i]++;
}
else now++;
b[i]=now;
for(int j=;ch[j];j++) ch[j]=;
}
for(int i=;i<=n;i++) A[i]=MP(a[i],b[i]);
int len1=,len2=;
for(int i=;i<=n;i++)
if(a[i]<b[i]) B[++len1]=A[i];
else C[++len2]=A[i];
sort(B+,B+len1+,cmp1);
sort(C+,C+len2+,cmp2);
int now=;
for(int i=;i<=len1;i++)
{
ans+=min(now,B[i].fi);
now-=min(now,B[i].fi);
now+=B[i].se;
}
for(int i=;i<=len2;i++)
{
ans+=min(now,C[i].fi);
now-=min(now,C[i].fi);
now+=C[i].se;
}
printf("%d\n",ans*);
}
return ;
}

【HDOJ6299】Balanced Sequence(贪心)的更多相关文章

  1. hdu6299 Balanced Sequence 贪心

    题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...

  2. HDU 6299 Balanced Sequence(贪心)

    题目:给出N个只有左右括号字符串 ,这N个字符串的排列顺序是任意的 , 问按最优的排序后 , 得到最多匹配的括号个数 分析: 我们很容易的想到 字符串)()()(( , 这样的字符串可以精简为)(( ...

  3. HDU6299 Balanced Sequence (多校第一场1002) (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. hdu 6299 Balanced Sequence (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu多校1002 Balanced Sequence

    Balanced Sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...

  6. HDU5014Number Sequence(贪心)

    HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...

  7. HDU 多校对抗赛 B Balanced Sequence

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  9. hdu 6299 Balanced Sequence (括号序列,贪心)

    大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t). 首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解. 那么这样最终 ...

随机推荐

  1. LN : leetcode 486 Predict the Winner

    lc 486 Predict the Winner 486 Predict the Winner Given an array of scores that are non-negative inte ...

  2. EventBus 报“Subscriber class already registered to event class”错误

    这句子的话意思也很容易理解,“接收者类已经被注册为事件类了”. 之前我是这么写: 事件注册是写在onStart()里面的 @Override protected void onStart() { su ...

  3. 磁盘格式化mke2fs

    -b 设置每个块的大小,当前支持1024,2048,40963种字节 -i 给一个inode多少容量 -c 检查磁盘错误,仅执行一次-c时候,会进行快速读取测试:-c -c会测试读写,会很慢 -L 后 ...

  4. 浅析 innerHTML 性能优化的原理

    浅析 innerHTML 性能优化的原理 博客分类: web前端 IEAndroidwebkit浏览器UI  昨天看了 lveyo老兄的"innerHTML的性能问题" 一文 ht ...

  5. android应用流量信息提取

    Linux 系统下所有的信息都是以文件的形式存在的,所以应用程序的流量信息也会被保存在操作系统的文件中.Android 2.2 版本以前的系统的流量信息都存放在 proc/net/dev(或者 pro ...

  6. php 批量依照ID建立 文件

    <?php // 登录验证 include_once('inc/conn.php'); // sql查询 $sql="SELECT * FROM zcgl ";// $res ...

  7. 回顾Spring MVC_01_概述_入门案例

    SpringMVC: SpringMVC是Spring为展现层提供的基于MVC设计的优秀的Web框架,是目前最主流的MVC框架之一 SpringMVC通过注解,让POJO成为处理请求的控制器,而无须实 ...

  8. 光线步进——RayMarching入门

    入门实现 先用RayMarching描绘一个球体,最后在进行光照计算参考:https://www.shadertoy.com/view/llt3R4 模拟摄像机射线float3 rayDirectio ...

  9. caffe layer注册机制

    Caffe内部维护一个注册表用于查找特定Layer对应的工厂函数(Layer Factory的设计用到了设计模式里的工厂模式).Layer_factory的主要作用是负责Layer的注册,已经注册完事 ...

  10. 洛谷——P1627 [CQOI2009]中位数

    P1627 [CQOI2009]中位数 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 中位数的题目有关统计的话,可以转 ...