题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4922

题解

记录每一个串的没有匹配的右括号 \()\) 的数量为 \(a_i\),为匹配的左括号 \((\) 的数量为 \(b_i\)。

令 \(h\) 表示前面的所有括号序列的剩下的未匹配的左括号 \((\)。可以发现,每一个串的作用就是先让 \(h\) 减少 \(a_i\),如果 \(h \geq 0\),那么再让 \(h\) 增加 \(b_i\)。

这是一种很常见的贪心模型,类似于 https://lydsy.com/JudgeOnline/problem.php?id=4619 的贪心策略,对于 \(a_i \leq b_i\) 的,需要尽量让每一个都可以选上,因此要把 \(a_i\) 从小到大排序,以便最后获得尽量大的 \(\max h\)。

对于 \(a_i > b_i\) 的,需要尽量让需要的前置 \(h\) 尽量小,因此把 \(b_i\) 从大到小排序。

最后总体上,\(a_i \leq b_i\) 的排在前面。

然后就是一个对于 \(h\) 的 01 背包了。


下面是代码,时间复杂度 \(O(n^3)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 300 + 7;
const int M = 90000 + 7;
const int INF = 0x3f3f3f3f; int n, m;
char s[N];
int dp[M]; struct Qthx {
int a, b, len;
inline bool operator < (const Qthx &c) const {
if (a < b) {
if (c.a < c.b) return a < c.a;
else return 1;
} else {
if (c.a < c.b) return 0;
else return b > c.b;
}
}
} c[N]; inline void work() {
std::sort(c + 1, c + n + 1);
for (int i = 1; i <= m; ++i) dp[i] = -INF;
for (int i = 1; i <= n; ++i) {
if (c[i].b > c[i].a) for (int j = m; j >= c[i].b; --j) smax(dp[j], dp[j + c[i].a - c[i].b] + c[i].len);
else for (int j = c[i].b; j <= m; ++j) smax(dp[j], dp[j + c[i].a - c[i].b] + c[i].len);
}
printf("%d\n", dp[0]);
} inline void init() {
read(n);
for (int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
int a = 0, b = 0;
for (int j = 1; s[j]; ++j) {
if (s[j] == '(') ++b;
else {
if (b) --b;
else ++a;
}
++c[i].len;
}
c[i].a = a, c[i].b = b, m += std::max(0, b - a);
}
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj4922 [Lydsy1706月赛]Karp-de-Chant Number 贪心+背包的更多相关文章

  1. 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp

    题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...

  2. bzoj 4919 [Lydsy1706月赛]大根堆 set启发式合并+LIS

    4919: [Lydsy1706月赛]大根堆 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 599  Solved: 260[Submit][Stat ...

  3. BZOJ4917: [Lydsy1706月赛]Hash Killer IV(模拟)

    4917: [Lydsy1706月赛]Hash Killer IV Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 327  Solved: 140[Su ...

  4. BZOJ4920: [Lydsy1706月赛]薄饼切割

    BZOJ4920: [Lydsy1706月赛]薄饼切割 Description 有一天,tangjz送给了quailty一张薄饼,tangjz将它放在了水平桌面上,从上面看下去,薄饼形成了一个H*W的 ...

  5. [Lydsy1706月赛]大根堆

    4919: [Lydsy1706月赛]大根堆 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 358  Solved: 150[Submit][Stat ...

  6. 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划

    [BZOJ4922][Lydsy六月月赛]Karp-de-Chant Number Description 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令人捉摸不透,有时候会让水平很 ...

  7. bzoj 4922: [Lydsy1706月赛]Karp-de-Chant Number 贪心+dp

    题意:给定 $n$ 个括号序,让你从中选取一些括号序按照任意顺序拼接,最终生成一个合法的括号序列,求这个合法序列长度最大值. 题解:假设括号序列相对顺序固定,而我们要做的只是判断选还是不选的话可以转化 ...

  8. @bzoj - 4922@ [Lydsy1706月赛]Karp-de-Chant Number

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令 ...

  9. ZOJ 2132 The Most Frequent Number (贪心)

    题意:给定一个序列,里面有一个数字出现了超过 n / 2,问你是哪个数字,但是内存只有 1 M. 析:首先不能开数组,其实也是可以的了,后台数据没有那么大,每次申请内存就可以过了.正解应该是贪心,模拟 ...

随机推荐

  1. Python3解leetcode Min Cost Climbing Stairs

    问题描述: On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you ...

  2. Fckeditor实现WORD粘贴图片自动上传

    在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...

  3. curry柯里化

    Function.prototype.method = function(name,func){ if(!this.prototype[name]){ this.prototype[name] = f ...

  4. Cef 重写alert与confirm弹窗

    在使用form内嵌cef浏览本地页面的时候,如果出现alert弹窗,会在标题栏显示页面所在目录.所以想起来重写alert的样式,通过MessageBox进行提示,或者自己写一个弹窗. 以下代码基于 3 ...

  5. JNI-java native interface(java本地接口)

    什么是JNI java native interface(java本地接口) ABI: application binary interface (应用程序二进制接口) 为什么要使用JNI * 复用很 ...

  6. 所有硬币组合问题——动态规划hdu2069

    Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cen ...

  7. SEC8 - MySQL 查询语句--------------进阶4:常见的函数

    # 进阶4:常见的函数 /* 概念:将一组逻辑语句封装在方法体中,对外暴露方法名 好处:1.隐藏了实现细节 2.提高代码的复用性 调用: select 函数名() [from 表]; 特点: (1)叫 ...

  8. SEC6 - MySQL 查询语句--------------进阶2:条件查询

    # 进阶2:条件查询 /* 语法: select 查询列表 from 表名 where 筛选条件; 分类: 一.按照条件表达式筛选 条件运算符:> < = !=(等价于<>) ...

  9. ash: export: `/usr/lib/jvm/jdk1.8.0_201/lib/dt.jar': 不是有效的标识符

    ash: export: `/usr/lib/jvm/jdk1.8.0_201/lib/dt.jar': 不是有效的标识符 ps: 如果有任何问题可以评论留言,我看到后会及时解答,评论或关注,您的鼓励 ...

  10. ToString()的简单介绍

    1.在某一个类中重写该类的toString()方法,是为了方便打印该类实例中的内容.