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#如何加载程序运行目录外的程序集 (转)
---恢复内容开始--- 尼玛,为了这个问题,纠结到差点吐出干血,赶紧记下来! 源地址:http://blog.csdn.net/dyllove98/article/details/9391325 我 ...
- java8中stream常用方法详解
map: 用作类型转换 如把集合里面的字符串转为大写,或者一个对象的集合取几个字段转为新的对象集合filter: 过滤 符合条件的集合元素保存下来,不符合条件的去掉flatMap:合并集合,比如Lis ...
- @ImportResource导入的xml配置里的Bean能够使用@PropertySource导入的属性值吗?
每篇一句 大师都是偏执的,偏执才能产生力量,妥协是没有力量的.你对全世界妥协了你就是空气.所以若没有偏见,哪来的大师呢 相关阅读 [小家Spring]详解PropertyPlaceholderConf ...
- java:选择排序法对数组排序
最近想练一练Java的算法,然后碰到LeetCode上一道从排序数组删除重复项的小题,刚开始没看到是从排序数组中,就乱写,其实要是排序树组,就比乱序的感觉上好写多了.然后就想回顾下冒泡法对数组排序,凭 ...
- Hadoop之WritableComprale 排序
Hadoop之WritableComprale 排序 Hadoop只对key进行排序 排序是 MapReduce 框架中最重要的操作之一.Map Task 和 Reduce Task 均会对数据(按照 ...
- git,github,gitlab,码云的区别
git 是版本控制工具. github https://github.com/和gitlab https://about.gitlab.com/都是基于git仓库的web开发流程代码托管平台.两者的区 ...
- nginx(二)
nginx rewrite Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向.rewrite只能放在server{},location{},if{}中,并且 ...
- GStreamer基础教程05 - 播放时间控制
简介 在多媒体应用中,我们通常需要查询媒体文件的总时间.当前播放位置,以及跳转到指定的时间点.GStreamer提供了相应的接口来实现此功能,在本文中,我们将通过示例了解如何查询时间信息,以及如何进行 ...
- python正则表达式与re模块-02
正则表达式 正则表达式与python的关系 # 正则表达式不是Python独有的,它是一门独立的技术,所有的编程语言都可以使用正则 # 但要在python中使用正则表达式,就必须依赖于python内置 ...
- MyEclipse 2016 Stable 1.0破解教程
一.下载所需文件 1. Windows最新版: MyEclipse 2016 Stable 1.0离线安装包(文件大小:1.52GB)--完整安装包,无需在线下载http://pan.baidu.co ...