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

首先我们考虑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. error splicing file: file too large解决方法

    FAT32格式的usb最大支持4G的文件,拷贝超过4G的文件需要把usb换成NTFS格式.

  2. SpringMVC请求处理流程

    从web.xml中 servlet的配置开始, 根据servlet拦截的url-parttern,来进行请求转发   Spring MVC工作流程图   图一   图二    Spring工作流程描述 ...

  3. java面试总结-(hibernate ibatis struts2 spring)

    说说Hibernate对象的三种状态 Hibernate对象有三种状态,分别是:临时态(Transient). 持久态(Persistent).游离态(Detached). 临时状态:是指从对象通过n ...

  4. 22.整数二进制表示中1的个数[Get1BitCount]

    [题目] 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. [分析] 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数 ...

  5. Light OJ 1393 Crazy Calendar (尼姆博弈)

    C - Crazy Calendar Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Su ...

  6. iOS通过设置info.plist参数使用iTunes导入导出Documents目录下的文件

    参考网址: http://my.oschina.net/hmj/blog/112592 http://www.cnblogs.com/taintain1984/archive/2013/05/27/3 ...

  7. css3学习总结4--CSS3背景

    css3背景 1. background-size 2. background-origin 3. background-clip 示例: className { background:url(bg_ ...

  8. Interger 与 int

    int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...

  9. spring的IOC和AOP

     spring的IOC和AOP 1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是 ...

  10. Java中Json用法

    首先导入json.jar,非常简单看过代码就明白怎么用了 package cn.mylucene; import java.util.HashMap; import java.util.Map; im ...