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

首先我们考虑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. Tornaod框架

    Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了能有效 ...

  2. 【SpringMVC】SpringMVC系列14之SpringMVC国际化

    14.SpringMVC国际化 14.1.概述 14.2.用户切换选择语言

  3. Plus One Linked List

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  4. Eclipse常用快捷键与代码模板

    Eclipse常用快捷键汇总 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键.1. [ALT+/]此快捷键为用户编 ...

  5. UEditor去除复制样式实现无格式粘贴

    UEditor内置了无格式粘贴的功能,只需要简单的配置即可. 1.修改ueditor.config.js,开启retainOnlyLabelPasted,并设置为true 2.开启pasteplain ...

  6. cocos2d-x的Android工程开启c++0x特性

    首先一定要确定你所安装NDK支持c++0x(我安装的android-ndk-r8) 文本打开 项目目录/proj.android/jni/Application.mk 在APP_CPPFLAGS那一行 ...

  7. Java for LeetCode 058 Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. Greedy:Radar Installation(POJ 1328)

    装雷达 题目大意,就是令在海岸线的(直线)一边是海(y>0),另一边是陆地(y<=0),在海岸线上装雷达,雷达可以覆盖的范围为d,海上有岛,(x,y),问你应该怎么装雷达,才能做到技能雷达 ...

  9. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  10. AJAX,JSON用户名校验

    效果 开发结构 1,src部分有两个包dao和servlet 1.1dao包下有两个数据库工具类文件 SqlHelper.java package org.guangsoft.dao; import ...