Codeforces 436D Pudding Monsters
题意简述
开始有无限长的一段格子,有n个格子种有布丁怪兽,一开始连续的布丁怪兽算一个布丁怪兽。
每回合你可以将一个布丁怪兽向左或右移动,他会在碰到第一个布丁怪兽时停下,并与其合并。
有m个特殊格子,询问最终你最多可以让几个特殊的格子上被布丁覆盖。
题解思路
dp
f[i]表示前i个布丁最多可覆盖的特殊格子数
g[i]表示前i个布丁,第i个不动的情况下最多可覆盖的特殊格子数
可得转移方程:
g[i] = max(g[i], f[l[i - len] - 1] + sum(b[j], a[i]));
f[r[i + len]] = max(f[r[i + len]], g[i] + sum(a[i] + 1, b[j]));
其中
l[i],r[i]表示第i个布丁所属的怪兽的最左边格子编号和最右边格子编号
len 是枚举特殊格子位置与i的距离
sum(a, b) 表示a,b之间的特殊格子数
代码
#include <cstdio>
#include <algorithm>
const int Maxn = 100010;
int n, m, ll, len;
int a[Maxn], b[Maxn], s[Maxn << 1], l[Maxn], r[Maxn];
int f[Maxn], g[Maxn];
inline void mmax(int& x, const int& y) { if (y > x) x = y; }
inline int sum(const int& x, const int& y) {return s[y] - s[x - 1]; }
int main()
{
scanf("%d%d", &n, &m);
for (register int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (register int i = 1; i <= m; ++i)
{
scanf("%d", &b[i]);
++s[b[i]];
}
std::sort(a + 1, a + n + 1);
std::sort(b + 1, b + m + 1);
a[0] = -300000; a[n + 1] = 300000;
for (register int i = 1; i <= n; ++i)
if (a[i] == a[i - 1] + 1) l[i] = l[i - 1];
else l[i] = i;
for (register int i = n; i; --i)
if (a[i] == a[i + 1] - 1) r[i] = r[i + 1];
else r[i] = i;
for (register int i = 1; i <= 200000; ++i) s[i] += s[i - 1];
for (register int i = 1; i <= n; ++i)
{
mmax(f[i], f[i - 1] + sum(a[i], a[i]));
mmax(g[i], f[i - 1] + sum(a[i], a[i]));
for (register int j = 1; j <= m && (len = a[i] - b[j]) > 0; ++j)
if (len < i)
mmax(g[i], f[l[i - len] - 1] + sum(b[j], a[i]));
mmax(f[i], g[i]);
for (register int j = m; j && (len = b[j] - a[i]) >= 0; --j)
if (len <= n - i)
mmax(f[r[i + len]], g[i] + sum(a[i] + 1, b[j]));
}
printf("%d\n", f[n]);
}
Codeforces 436D Pudding Monsters的更多相关文章
- Codeforces 436D - Pudding Monsters(dp)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 这题数据范围有点迷惑啊--乍一看 \(\mathcal O(nm)\) 过不去,还以为是正解是 \(\mathcal O(n+m ...
- Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- Codeforces 526F Pudding Monsters
先把题目抽象一下: 有一个静态的数组,求有多少个区间[i,j]满足:j-i==max{ai,...,aj}-min{ai,...,aj} 也就是要求max-min+i-j==0的区间数 所以肿么做呢? ...
- 奇袭 CodeForces 526F Pudding Monsters 题解
考场上没有认真审题,没有看到该题目的特殊之处: 保证每一行和每一列都恰有一只军队,即每一个Xi和每一个Yi都是不一样 的. 于是无论如何也想不到复杂度小于$O(n^3)$的算法, 只好打一个二维前缀和 ...
- 【CF526F】Pudding Monsters cdq分治
[CF526F]Pudding Monsters 题意:给你一个排列$p_i$,问你有对少个区间的值域段是连续的. $n\le 3\times 10^5$ 题解:bzoj3745 Norma 的弱化版 ...
- [Codeforces526F]Pudding Monsters 分治
F. Pudding Monsters time limit per test 2 seconds memory limit per test 256 megabytes In this proble ...
- CodeForces526F:Pudding Monsters (分治)
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- CF526F Pudding Monsters
CF526F Pudding Monsters 题目大意:给出一个\(n* n\)的棋盘,其中有\(n\)个格子包含棋子. 每行每列恰有一个棋子. 求\(k*k\)的恰好包含\(k\)枚棋子的子矩形个 ...
- 「CF526F」 Pudding Monsters
CF526F Pudding Monsters 传送门 模型转换:对于一个 \(n\times n\) 的棋盘,若每行每列仅有一个棋子,令 \(a_x=y\),则 \(a\) 为一个排列. 转换成排列 ...
随机推荐
- C++学习书籍推荐《Effective C++ 第三版》下载
百度云及其他网盘下载地址:点我 编辑推荐 <Effective C++:改善程序与设计的55个具体做法(第3版)(中文版)(双色)>前两个版本抓住了全世界无数程序员的目光.原因十分明显:S ...
- String到底在内存中是如何存储的
String会出现在哪些地方 方法内的局部string 类内的字段String static string 容器中存储的string String数组 那么String的位置会影响其存储方式吗? 显然 ...
- Running Code on a Thread Pool Thread_翻译
The previous lesson showed you how to define a class that manages thread pools and the tasks that ru ...
- hive merge into 批量更新测试
一.使用条件 hive2.2.0及之后的版本支持使用merge into 语法,使用源表数据批量目标表的数据.使用该功能还需做如下配置 1.参数配置 set hive.support.concurre ...
- python爬虫笔记之re.match匹配,与search、findall区别
为什么re.match匹配不到?re.match匹配规则怎样?(捕一下seo) re.match(pattern, string[, flags]) pattern为匹配规则,即输入正则表达式. st ...
- Fedora dnf配置
1.在配置文件/etc/dnf/dnf.conf中加入: fastestmirror=true keepcache=true 这样下载安装软件就快多了. 2.dnf常用命令 检查并升级可用软件包: $ ...
- cordova把我搞晕了
天啦,搞了几十次,这次求你成功好吗?
- java练习---12
public class L1106 { public static void main(String[] args) { // TODO Auto-generated method stub Tes ...
- 转 python - Python在终端通过pip安装好包以后,在Pycharm中依然无法使用的解决办法
转 https://blog.csdn.net/kouyi5627/article/details/80531442 在终端通过pip装好包以后,在pycharm中导入包时,依然会报错.新手不知道具体 ...
- webgl图库研究(包括BabylonJS、Threejs、LayaboxJS、SceneJS、ThingJS等框架的特性、适用范围、支持格式、优缺点、相关网址)
3D图库框架范围与示例 摘要: 为实现企业80%以上的生产数据进行智能转化,在烟草.造纸.能源.电力.机床.化肥等行业,赢得领袖企业青睐,助力企业构建AI赋能中心,实现智能化转型升级.“远舢文龙数据处 ...