现学的左偏树。。。这可是道可并堆的好题目。

首先我们考虑z不减的情况:

我们发现对于一个区间[l, r],里面是递增的,则对于此区间最优解为z[i] = t[i];

如果里面是递减的,z[l] = z[l + 1] = ... = z[r] = 这段数的中位数,不妨叫做w。(此处我们定义中位数为第(r - l + 1) / 2大的数,因为这并不影响结果)

而其实递增可以转化为每一段只有一个点,就等价于递减了。

那么我们把原数列分段,每段都是递减的,而每一段的z都是那段的中位数w。这样就找到了最优解。(证略)

这样就有了解法:

(1)新加入一个数至数列末端,先把它当成单独一段

(2)每次看最后一段的w[tot]和前一段的w[tot - 1],若w[tot] < w[tot - 1],则说明合并可以更优,合并这两段。

(3)最后计算ans

这之中还有一个问题:z[i]不是不减而是递增。有个巧妙地办法:让z[i](新) = z[i](老) - i即可,这样就保证了z的递增性质。

 /**************************************************************
Problem: 1367
User: rausen
Language: C++
Result: Accepted
Time:5284 ms
Memory:59400 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = ; struct heap{
int v, l, r, dep;
}h[N];
int l[N], r[N], cnt[N], num[N], root[N];
int z[N];
int n, tot, Cnt; inline int read(){
int x = , sgn = ;
char ch = getchar();
while (ch < '' || ch > ''){
if (ch == '-') sgn = -;
ch = getchar();
}
while (ch >= '' && ch <= ''){
x = x * + ch - '';
ch = getchar();
}
return sgn * x;
} int new_heap(int x){
h[++Cnt].v = x;
h[Cnt].l = h[Cnt].r = h[Cnt].dep = ;
return Cnt;
} int Merge(int x, int y){
if (!x || !y) return x + y;
if (h[x].v < h[y].v)
swap(x, y);
h[x].r = Merge(h[x].r, y);
if (h[h[x].l].dep < h[h[x].r].dep)
swap(h[x].l, h[x].r);
h[x].dep = h[h[x].r].dep + ;
return x;
} int Top(int x){
return h[x].v;
} int Pop(int x){
return Merge(h[x].l, h[x].r);
} int main(){
n = read();
for (int i = ; i <= n; ++i)
z[i] = read() - i; for (int i = ; i <= n; ++i){
++tot;
root[tot] = new_heap(z[i]);
cnt[tot] = , num[tot] = ;
l[tot] = i, r[tot] = i; while (tot > && Top(root[tot]) < Top(root[tot - ])){
--tot;
root[tot] = Merge(root[tot], root[tot + ]);
num[tot] += num[tot + ], cnt[tot] += cnt[tot + ], r[tot] = r[tot + ];
for(; cnt[tot] * > num[tot] + ; --cnt[tot])
root[tot] = Pop(root[tot]);
}
} long long ans = ;
for (int i = ; i <= tot; ++i)
for (int j = l[i], w = Top(root[i]); j <= r[i]; ++j)
ans += abs(z[j] - w);
printf("%lld\n", ans);
return ;
}

(p.s. 真是写的矬死了。。。越优化又慢,都醉了)

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

  1. BZOJ1367 [Baltic2004]sequence 堆 左偏树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1367 题意概括 Description Input Output 一个整数R 题解 http:// ...

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

    题目链接 BZOJ1367 题解 又是一道神题,, 我们考虑一些简单的情况: 我们先假设\(b_i\)单调不降,而不是递增 对于递增序列\(\{a_i\}\),显然答案\(\{b_i\}\)满足\(b ...

  3. BZOJ1367——[Baltic2004]sequence

    1.题目大意:给一个序列t,然后求一个序列z,使得$|z1-t1|+|z2-t2|+...+|zn-tn|$的值最小,我们只需要求出这个值就可以了,并且z序列是递增的 2.分析:这道题z序列是递增的, ...

  4. 可并堆试水--BZOJ1367: [Baltic2004]sequence

    n<=1e6个数,把他们修改成递增序列需把每个数增加或减少的总量最小是多少? 方法一:可以证明最后修改的每个数一定是原序列中的数!于是$n^2$DP(逃) 方法二:把$A_i$改成$A_i-i$ ...

  5. BZOJ1367: [Baltic2004]sequence(左偏树)

    Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 解题思路: 有趣的数学题. 首先确定序 ...

  6. bzoj1367 [Baltic2004]sequence 左偏树+贪心

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1367 题解 先考虑条件为要求不下降序列(不是递增)的情况. 那么考虑一段数值相同的子段,这一段 ...

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

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

  8. 【bzoj1367】[Baltic2004]sequence

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

  9. 【BZOJ-1367】sequence 可并堆+中位数

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 932  Solved: 348[Submit][S ...

随机推荐

  1. Nth to Last Node in List

    Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Exam ...

  2. PYTHON实现HTTP基本认证(BASIC AUTHENTICATION)

    参考: http://www.voidspace.org.uk/python/articles/authentication.shtml#id20 http://zh.wikipedia.org/wi ...

  3. kettle job通过javascript进行循环控制

    任何一种编程语言都少不了循环,kettle中的job也一样.那么kettle中的job是怎么通过JavaScript来达到类似于编程语言中的for循环呢? var max = parent_job.g ...

  4. 在Android上使用fontAwesome

    再也不用做那些讨厌的小图标了! 从网上找了些资料,总结下在android上使用fontAwesome的方法. 1.到官网上下载资源包,找到其中的字体文件fontawesome-webfont.ttf, ...

  5. iOS 中NSOperationQueue,Grand Central Dispatch , Thread的上下关系和区别

    In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central D ...

  6. Ubuntu 13.04安装搜狗输入法

    Ubuntu 13.04安装搜狗输入法 [日期:2013-07-08] 来源:Linux公社  作者:LinuxIDC.com [字体:大 中 小]     目标:在Ubuntu 13.04以及基于U ...

  7. JavaEE面试题库

    Java EE软件工程师 认证考试 面试题大全 目  录 第一部分  HTML/CSS/JavaScript 1 1.              HTML含义和版本变化... 1 2.         ...

  8. PA

    [题目描述] 汉诺塔升级了:现在我们有?个圆盘和?个柱子,每个圆盘大小都不一样, 大的圆盘不能放在小的圆盘上面,?个柱子从左到右排成一排.每次你可以将一 个柱子上的最上面的圆盘移动到右边或者左边的柱子 ...

  9. 编译预处理命令--define和ifdef的使用

    这里将对常用的预处理命令进行学习. 一.宏定义  ·defined 格式:`defined     宏名      数值 或者 `define      宏名 注意:后面没有‘;‘,和单片机不一样: ...

  10. Nginx开启Gzip压缩大幅提高页面加载速度

    [root@12 conf]# vi nginx.conf gzip on;#开启GZIP gzip_min_length 1k; #不压缩临界值,大于1K的才压缩,一般不用改 gzip_buffer ...