浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1367

显然,如果给出的数组是递增的,那么答案就是\(0\)。

如果给出的数组是递减的,根据贪心的思想答案就是\(\sum\limits_{i=1}^{n}|x-t_i|\),\(x\)是\(t\)数组的中位数。

但是给出的数组是无序的。

我们可以把这个数组划成一段段的,每一段都选一个\(x\)去当做中位数。简单的来讲,最优的方案是\(z_i=t_i\)的,但是为了保证\(z\)数组的递增性我们必须要把比前一位小的\(z\)和前一位合并到一段里去,形成新的一段,然后这一段的\(z\)就是这一段\(t\)的中位数。

但是这样出现了重复的\(z\),不满足严格递增的性质。但是我们只需要把\(t_i=t_i-i\)就行了。这样就默认是严格递增的了。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; const int maxn=1e6+5; ll ans;
int n,tot,cnt;
int l[maxn],r[maxn];
int fa[maxn],dist[maxn],son[maxn][2];
int a[maxn],rt[maxn],val[maxn],siz[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} int newnode(int v) {
val[++tot]=v;
siz[tot]=1;return tot;
} int merge(int a,int b) {
if(!a||!b)return a+b;
if(val[a]<val[b])swap(a,b);
son[a][1]=merge(son[a][1],b);
if(dist[son[a][1]]>dist[son[a][0]])
swap(son[a][1],son[a][0]);
dist[a]=dist[son[a][1]]+1;
siz[a]=siz[son[a][0]]+1+siz[son[a][1]];
return a;
} int pop(int u) {
int res=merge(son[u][0],son[u][1]);
son[u][0]=son[u][1]=0;
return res;
} int main() {
n=read(),dist[0]=-1;
for(int i=1;i<=n;i++)
a[i]=read()-i;
for(int i=1;i<=n;i++) {
rt[++cnt]=newnode(a[i]);l[cnt]=r[cnt]=i;
while(cnt>1&&val[rt[cnt]]<val[rt[cnt-1]]) {
rt[cnt-1]=merge(rt[cnt-1],rt[cnt]);r[--cnt]=i;
while(siz[rt[cnt]]>(r[cnt]-l[cnt]+2)/2)rt[cnt]=pop(rt[cnt]);
}
}
for(int i=1;i<=cnt;i++)
for(int j=l[i];j<=r[i];j++)
ans+=abs(val[rt[i]]-a[j]);
printf("%lld\n",ans);
return 0;
}

BZOJ1367:[Baltic2004]sequence的更多相关文章

  1. 【BZOJ1367】[Baltic2004]sequence 左偏树

    [BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...

  2. 【bzoj1367】[Baltic2004]sequence

    2016-05-31 17:31:26 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1367 题解:http://www.cnblogs.co ...

  3. 【bzoj1367】[Baltic2004]sequence 可并堆

    题目描述 输入 输出 一个整数R 样例输入 7 9 4 8 20 14 15 18 样例输出 13 题解 可并堆,黄源河<左偏树的特点及其应用>Page 13例题原题 #include & ...

  4. 【BZOJ 1367】 1367: [Baltic2004]sequence (可并堆-左偏树)

    1367: [Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Ou ...

  5. BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1111  Solved: 439[Submit][ ...

  6. BZOJ 1367 [Baltic2004]sequence 解题报告

    BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...

  7. bzoj 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Description Input Output 一个整数R Sa ...

  8. 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Submit: 1090  Solved: 432 [Submit ...

  9. 【九度OJ】题目1442:A sequence of numbers 解题报告

    [九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...

随机推荐

  1. iPhone快速获取UUID

    1.一张图解决不懂iPhone手机的小白获取UDID的方式

  2. phpstorm+xdebug, 实现断点调试: xdebug如何配置

    [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" xdebug.trace_output_dir=" ...

  3. Django 基于Ajax & form 简单实现文件上传

    前端实现 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="U ...

  4. Django导出excel中文乱码解决方案

    Django官方文档有关于怎么生成csv文件的方法 import csv from django.http import HttpResponse def some_view(request): # ...

  5. LeetCode:存在重复元素【217】

    LeetCode:存在重复元素[217] 题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 ...

  6. gsub! 和 gsub

    ruby中带“!"和不带"!"的方法的最大的区别就是带”!"的会改变调用对象本身了.比方说str.gsub(/a/, 'b'),不会改变str本身,只会返回一个 ...

  7. HackerRank - angry-children 【排序】

    题意 求一个序列当中 其 长度为 K 的子序列 中的 最大值 - 最小值 求 这个值 最小是多少 思路 先将序列排序 然后 I = 0, J = K - 1 然后 往下遍历 如果 arr[j] - a ...

  8. Data Structure Linked List: Write a function to get the intersection point of two Linked Lists.

    http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/ 第一第 ...

  9. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  10. linux下扩展root分区

    1 查看当前磁盘情况 fdisk -l /dev/sda1 2048 6143 2048 83 Linux /dev/sda2 * 6144 1054719 524288 83 Linux /dev/ ...