题面

Description

Input

Output

一个整数R

Sample Input

7
9
4
8
20
14
15
18

Sample Output

13

Hint

所求的Z序列为6,7,8,13,14,15,18.

R=13

Solution

我们首先来考虑另一个问题: 给定一个数列\(\{a_n\}\), 求一个单调不下降的\(\{b_n\}\), 使得\(\sum |b_n - a_n|\)最小.

考虑两种较为特殊情况:

  • \(a_1 \le a_2 \le ... \le a_n\), 此时\(b_n = a_n\)
  • \(a_1 \ge a_2 \ge ... \ge a_n\), 此时\(b_n = \{a_n\}的中位数\)

不难发现, 假如我们把\(\{a_n\}\)单调不下降的情况看作是一个数一段, 则它与\(\{a_n\}\)单调不上升的情况是等价的.

因此, 这道题目的做法就是: 从前往后分段, 对于\(a_n\)这一个数, 开始时我们把它单独作为一段, 假如这一段的中位数比上一段要小, 则把当前一段和上一段合并. 直至当前\(a_n\)所在段的中位数大于等于上一段的中位数或只剩下一段. 然后考虑数列上的下一个数.

回到原题, 由于原题要求\(z_n < z_{n + 1}\), 我们把读入的\(t_n\)变成\(b_n - n\), 再按照上述方法求解即可.

考虑如何动态维护中位数: 比较简单的想法就是启发式合并平衡树, 时间复杂度\(O(n \log^2 n)\), 会TLE. 下面是代码.

#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cmath> namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1;
char c; while(! isdigit(c = getchar()))
if(c == '-')
sgn *= -1; while(isdigit(c))
a = a * 10 + c - '0', c = getchar(); return a * sgn;
}
} const int N = (int)1e6;
int a[N], cnt; struct section
{
int L, R, w;
}sec[N]; struct splayTrees
{
struct node
{
int suc[2], pre, sz, w;
}nd[N]; int rt[N]; inline void newNode(int u, int w)
{
nd[u].w = w, nd[u].suc[0] = nd[u].suc[1] = nd[u].pre = -1, nd[u].sz = 1;
} inline void update(int u)
{
nd[u].sz = 1; for(int i = 0; i < 2; ++ i)
if(~ nd[u].suc[i])
nd[u].sz += nd[nd[u].suc[i]].sz;
} inline int getRelation(int u)
{
if(! (~ nd[u].pre))
return -1; return u == nd[nd[u].pre].suc[1];
} inline void rotate(int u)
{
int pre = nd[u].pre, prepre = nd[pre].pre, k = getRelation(u); if(~ nd[u].suc[k ^ 1])
nd[nd[u].suc[k ^ 1]].pre = pre; nd[pre].suc[k] = nd[u].suc[k ^ 1];
nd[u].suc[k ^ 1] = pre;
nd[u].pre = nd[pre].pre; if(~ prepre)
nd[prepre].suc[getRelation(pre)] = u; nd[pre].pre = u;
update(pre), update(u);
} inline void splay(int a, int u)
{
while(~ nd[u].pre)
{
int pre = nd[u].pre; if(~ nd[pre].pre)
rotate(getRelation(pre) == getRelation(u) ? pre : u); rotate(u);
} rt[a] = u;
} void _insert(int a, int u, int v)
{
++ nd[u].sz; if(nd[v].w < nd[u].w)
{
if(! (~ nd[u].suc[0]))
{
newNode(v, nd[v].w);
nd[v].pre = u;
nd[u].suc[0] = v;
splay(a, v);
}
else
_insert(a, nd[u].suc[0], v);
}
else
{
if(! (~ nd[u].suc[1]))
{
newNode(v, nd[v].w);
nd[v].pre = u;
nd[u].suc[1] = v;
splay(a, v);
}
else
_insert(a, nd[u].suc[1], v);
}
} inline void insert(int a, int u)
{
_insert(a, rt[a], u);
} void _merge(int a, int u)
{
int suc[2] = {nd[u].suc[0], nd[u].suc[1]};
insert(a, u); for(int i = 0; i < 2; ++ i)
if(~ suc[i])
_merge(a, suc[i]);
} inline int merge(int a, int b)
{
if(nd[rt[a]].sz < nd[rt[b]].sz)
std::swap(rt[a], rt[b]); _merge(a, rt[b]);
} int get(int u, int k)
{
if(~ nd[u].suc[0])
{
if(nd[nd[u].suc[0]].sz > k)
return get(nd[u].suc[0], k);
else
k -= nd[nd[u].suc[0]].sz;
} if(! k)
return nd[u].w; return get(nd[u].suc[1], k - 1);
} inline int getMedian(int a)
{
return get(rt[a], nd[rt[a]].sz >> 1);
}
}trees; int main()
{
#ifndef ONLINE_JUDGE
freopen("BZOJ1367.in", "r", stdin);
freopen("BZOJ1367.out", "w", stdout);
#endif using namespace Zeonfai; int n = getInt(); for(int i = 0; i < n; ++ i)
trees.newNode(i, a[i] = getInt() - i); cnt = 0; for(int i = 0; i < n; ++ i)
{
trees.rt[cnt] = i;
sec[cnt].w = a[i], sec[cnt].L = i, sec[cnt ++].R = i; for(; cnt > 1 && sec[cnt - 2].w > sec[cnt - 1].w; -- cnt)
trees.merge(cnt - 2, cnt - 1), sec[cnt - 2].R = sec[cnt - 1].R, sec[cnt - 2].w = trees.getMedian(cnt - 2);
} long long ans = 0; for(int i = 0; i < cnt; ++ i)
for(int j = sec[i].L; j <= sec[i].R; ++ j)
ans += abs(a[j] - sec[i].w); printf("%lld", ans);
}

正解是左偏树维护中位数, \(O(n \log n)\)

因为我们合并的前提是:中位数(i)>中位数(i+1),那么对于合并后的i而言,中位数肯定是不升的

根据这个性质我们又可以用可并堆了,堆顶元素表示该序列中的中位数

当堆的元素个数*2>序列长度+1的时候就可以弹出堆顶

懒得写代码了.

BZOJ1367【Baltic2004】sequence的更多相关文章

  1. 【BZOJ1367】【Baltic2004】sequence - 可合并堆

    题意: 题解: 其实这是道水题啦……只不过我没做过而已 先考虑构造不严格递增序列,考虑原序列中的一段下降区间,显然区间中的$z$全取中位数最优: 那么可以把原序列拆成很多个下降序列,从头到尾加入原序列 ...

  2. 【XSY2985】【BZOJ1367】【Baltic2004】sequence

    考虑两种情况: 1.\(a_1\)<\(a_2\)<\(a_3\)<\(a_4\)...<\(a_n\) 直接令\(b_i\)=\(a_i\),最小. 2.\(a_1\)> ...

  3. 【dfs】Sequence Decoding

    Sequence Decoding 题目描述 The amino acids in proteins are classified into two types of elements, hydrop ...

  4. 【XSY2564】sequence

    Description [题目描述] 给定一个长度为n的由['0'..'9']组成的字符串s,v[i,j]表示由字符串s第i到第j位组成的十进制数字. 将它的某一个上升序列定义为:将这个字符串切割成m ...

  5. 【测试题】sequence

    题目 给定一个长度为n(n<=5000)的由['0'..'9']组成的字符串s,v[i,j]表示由字符串s第i到第j位组成的十进制数字. 将它的某一个上升序列定义为:将这个字符串切割成m段不含前 ...

  6. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

  7. 【LeetCode】拓扑排序 topological-sort(共5题)

    [207]Course Schedule [210]Course Schedule II [269]Alien Dictionary [329]Longest Increasing Path in a ...

  8. 【LeetCode】拓扑排序

    [207] Course Schedule 排课问题,n门课排课,有的课程必须在另外一些课程之前上,问能不能排出来顺序. 题解:裸的拓扑排序.参考代码见算法竞赛入门指南这本书. class Solut ...

  9. 论文阅读(Xiang Bai——【PAMI2017】An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition)

    白翔的CRNN论文阅读 1.  论文题目 Xiang Bai--[PAMI2017]An End-to-End Trainable Neural Network for Image-based Seq ...

随机推荐

  1. QT入门学习笔记1:为什么要选QT及QT软件下载

    为什么选择QT? Qt突出的优势: ◆ Qt 是基于 C++ 的一种语言扩展(Extention) C/C++ 目前还是一种很多人都在学习的语言. Qt的好处就在于Qt本身可以被称作是一种 C++ 的 ...

  2. 创建Django项目并将其部署在腾讯云上

    这段时间在做scrapy爬虫,对爬出来的数据基于Django做了统计与可视化,本想部署在腾讯云上玩玩,但是因为以前没有经验遇到了一些问题,在这里记录一下: 首先说下Django的创建与配置: 1. 创 ...

  3. Artwork Gym - 101550A 离线并查集

    题目:题目链接 思路:每个空白区域当作一个并查集,因为正着使用并查集分割的话dfs会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...

  4. 15年多校第一场七题hdu5294

    要做这题,先要明白图的割,说白了就是 为了让原点无法到汇点要删几条边(之所以叫割,就是在图面上切一刀,减掉最小的边是原点和汇点成为两个集合),想到了割先放着一会用. 题中说只有沿最短路走才有可能追上, ...

  5. 详解Python中的相对导入和绝对导入

    Python 相对导入与绝对导入,这两个概念是相对于包内导入而言的.包内导入即是包内的模块导入包内部的模块. Python import 的搜索路径 在当前目录下搜索该模块 在环境变量 PYTHONP ...

  6. WIN 备份 重装

    title: WIN 备份 重装 date: 2018-09-01 22:35:31 updated: tags: [windows,记录,折腾] description: keywords: com ...

  7. df和du显示的磁盘空间使用情况不一致问题

    背景介绍: dba同事删除了mysql /datao目录下的文件,通过du –sh查看空间使用700G,df -h查看空间使用1T,没有重启mysql服务. 另一个表现出du与df命令不同之处的例子如 ...

  8. django demo --blog

    详情,请看虫师博客http://www.cnblogs.com/fnng/p/3737964.html 和https://my.oschina.net/matrixchan/blog/184445  ...

  9. Educational Codeforces Round 20 D. Magazine Ad

    The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...

  10. [译] Pandas中根据列的值选取多行数据

    # 选取等于某些值的行记录 用 == df.loc[df['column_name'] == some_value] # 选取某列是否是某一类型的数值 用 isin df.loc[df['column ...