1112: [POI2008]砖块Klo

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1245  Solved: 426
[Submit][Status][Discuss]

Description

N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另一柱.仓库无限大. 现在希望用最小次数的动作完成任务.

Input

第一行给出N,K. (1 ≤ k ≤ n ≤ 100000), 下面N行,每行代表这柱砖的高度.0 ≤ hi ≤ 1000000

Output

最小的动作次数

Sample Input

5 3
3
9
2
3
1

Sample Output

2

HINT

原题还要求输出结束状态时,每柱砖的高度.本题略去.

Source

题解:(呵呵呵我会说我逗到了用Splay做这题的地步么= =)

首先,看这道题,对于某个高度序列而言,很明显将所有高度都变为中位数代价最小

我觉得任何一个数竞党都该知道怎么样可以让下列式子值最小——

\( \sum_{i=1}^{N} \left | x-x_i \right | \)
其中,\( x_1 \leq x_2 \leq x_3 ... \leq x_N \)
显然,当N为奇数时, \( x=x_\frac{N+1}{2} \)
当N为偶数时, \( x_\frac{N}{2} \leq x \leq x_\frac{N+2}{2} \)

然后接下来讨论怎么实现——我们需要一棵平衡树,可以加入和删除值,可以查询指定排名的数位置(用来找中位数),还得可以快速求出当前序列中比中位数大的各个数之和,以及比它小的之和,这样子就很明显需要维护子树大小(用于排名),然后还得维护子树和,然后我为了偷懒就直接来了个splay,每次将中位数splay上来,然后两边的不就是我们要的两边和嘛,然后该怎么办怎么办,别的没了

 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ var
i,j,k,m,n,head:longint;
l,ans:int64;
lef,rig,b:array[..] of longint;
a,c:array[..] of int64;
procedure rt(var x:longint);inline;
var f,l:longint;
begin
if (x=) or (lef[x]=) then exit;
b[lef[x]]:=b[x];b[x]:=b[rig[lef[x]]]+b[rig[x]]+;
c[lef[x]]:=c[x];c[x]:=c[rig[lef[x]]]+c[rig[x]]+a[x];
f:=x;l:=lef[x];
lef[f]:=rig[l];
rig[l]:=f;
x:=l;
end;
procedure lt(var x:longint);inline;
var f,r:longint;
begin
if (x=) or (rig[x]=) then exit;
b[rig[x]]:=b[x];b[x]:=b[lef[rig[x]]]+b[lef[x]]+;
c[rig[x]]:=c[x];c[x]:=c[lef[rig[x]]]+c[lef[x]]+a[x];
f:=x;r:=rig[x];
rig[f]:=lef[r];
lef[r]:=f;
x:=r;
end;
procedure splay(var x:longint;y:longint);inline;
begin
if (x=) or (y=) then exit;
if y=(b[lef[x]]+) then exit;
if y<(b[lef[x]]+) then
begin
if (b[lef[lef[x]]]+)=y then rt(x) else
if y<(b[lef[lef[x]]]+) then
begin
splay(lef[lef[x]],y);
rt(x);rt(x);
end
else
begin
splay(rig[lef[x]],y-b[lef[lef[x]]]-);
lt(lef[x]);rt(x);
end;
end
else
begin
y:=y--b[lef[x]];
if y=(b[lef[rig[x]]]+) then lt(x) else
if y<(b[lef[rig[x]]]+) then
begin
splay(lef[rig[x]],y);
rt(rig[x]);lt(x);
end
else
begin
splay(rig[rig[x]],y--b[lef[rig[x]]]);
lt(x);lt(x);
end;
end;
end;
procedure ins(var x:longint;y:longint);inline;
begin
if x= then
begin
x:=y;
exit;
end;
if a[y]<=a[x] then
begin
ins(lef[x],y);
c[x]:=c[lef[x]]+c[rig[x]]+a[x];
b[x]:=b[lef[x]]+b[rig[x]]+;
end
else
begin
ins(rig[x],y);
c[x]:=c[lef[x]]+c[rig[x]]+a[x];
b[x]:=b[lef[x]]+b[rig[x]]+;
end;
end;
function getrank(x,y:longint):longint;inline;
begin
if x= then exit(-);
if a[x]=y then exit(b[lef[x]]+);
if y<a[x] then exit(getrank(lef[x],y)) else exit(b[lef[x]]++getrank(rig[x],y));
end;
procedure init(x:longint);inline;
begin
ins(head,x);
splay(head,random(b[head])+);
end;
procedure kill(x:longint);inline;
begin
if x= then
begin
splay(head,);
dec(c[head],c[lef[head]]);
dec(b[head]);
lef[head]:=;
end
else if x=b[head] then
begin
splay(head,b[head]-);
dec(c[head],c[rig[head]]);
dec(b[head]);
rig[head]:=;
end
else begin
splay(head,x+);
splay(lef[head],x-);
dec(c[head],c[rig[lef[head]]]);dec(b[head]);
dec(c[lef[head]],c[rig[lef[head]]]);dec(b[lef[head]]);
rig[lef[head]]:=;
end;
end;
begin
readln(n,m);randomize;
if m<= then
begin
writeln();
halt;
end;
for i:= to n do
begin
readln(a[i]);
c[i]:=a[i];b[i]:=;
lef[i]:=;rig[i]:=;
end;
head:=;ans:=maxlongint*maxlongint;
for i:= to m do init(i);
for i:= to n-m+ do
begin
splay(head,(m+) div );
l:=;
if lef[head]<> then inc(l,a[head]*b[lef[head]]-c[lef[head]]);
if rig[head]<> then inc(l,c[rig[head]]-a[head]*b[rig[head]]);
if l<ans then ans:=l;
if i=(n-m+) then break;
kill(getrank(head,a[i]));
init(i+m);
end;
writeln(ans);
readln;
end.

1112: [POI2008]砖块Klo的更多相关文章

  1. BZOJ 1112: [POI2008]砖块Klo

    1112: [POI2008]砖块Klo Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1736  Solved: 606[Submit][Statu ...

  2. [BZOJ 1112] [POI2008] 砖块Klo 【区间K大】

    题目链接:BZOJ - 1112 题目分析 枚举每一个长度为k的连续区间,求出这个区间的最优答案,更新全局答案. 可以发现,这个区间的所有柱子最终都变成这k个数的中位数时最优,那么我们就需要查询这个区 ...

  3. 线段树 || BZOJ 1112: [POI2008]砖块Klo

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1112 题解: 希望有连续K柱的高度是一样的,就先把1~K的数扔进线段树(线段树的下标就是数值 ...

  4. BZOJ 1112 [POI2008]砖块Klo(可持久化线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1112 [题目大意] 给出一个数列,对于一个操作,你可以对一个数+1,或者一个数-1, ...

  5. bzoj 1112: [POI2008]砖块Klo【对顶堆】

    priority_queue实现的对顶堆,细节超级多WA了十几次--但是理论上是最简便的orz其实是我已经不会写平衡树了 枚举左端点,显然要把这一段的高度搞成(l,l+k-1)的高度中位数,所以需要一 ...

  6. BZOJ 1112: [POI2008]砖块Klo Splay + 性质分析

    Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in",& ...

  7. BZOJ 1112: [POI2008]砖块Klo1112( BST )

    枚举每个长度为k的区间, 然后用平衡树找中位数进行判断, 时间复杂度O(nlogn). 早上起来精神状态不太好...连平衡树都不太会写了...果断去看了会儿番然后就A了哈哈哈 ------------ ...

  8. [Bzoj1112][POI2008]砖块Klo(splay)

    1112: [POI2008]砖块Klo Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2353  Solved: 831[Submit][Statu ...

  9. [BZOJ1112][POI2008]砖块Klo

    [BZOJ1112][POI2008]砖块Klo 试题描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另 ...

随机推荐

  1. mysql数据库update时只更新部分数据方法

    需求:更新url中最一个字符的'-1'改为'-5',前面的内容保持不变 url列的内容如下:http://h5game.ecs.cedarmg.com/a/captal/dispther.do?dev ...

  2. datatable 使用详细说明

    要注意的是,要被dataTable处理的table对象,必须有thead与tbody,而且,结构要规整(数据不一定要完整),这样才能正确处理.以下是在进行dataTable绑定处理时候可以附加的参数: ...

  3. 变形transform的副作用

    前面的话   变形transform本来是一个用来处理移动.旋转.缩放和倾斜等基本操作的CSS3属性,但该属性除了完成其本职工作之后,还对普通元素造成了意想不到的影响,本文将详细介绍transform ...

  4. 机器学习实战笔记(1)——k-近邻算法

    机器学习实战笔记(1) 1. 写在前面 近来感觉机器学习,深度学习神马的是越来越火了,从AlphaGo到Master,所谓的人工智能越来越NB,而我又是一个热爱新潮事物的人,于是也来凑个热闹学习学习. ...

  5. Socket通信流程

    Socket通信流程 HTTP 底层就是通过socket建立连接通信管道,实现数据传输 HTTP是一个TCP的传输协议(方式),它是一个可靠,安全的协议

  6. Spring集成MyBatis框架

    Java在写数据库查询时,我接触过四种方式: 1.纯Java代码,引用对应的数据库驱动包,自己写连接与释放逻辑(可以用连接池) 这种模式实际上性能是非常不错的,但是使用起来并不是非常方便:一是要手工为 ...

  7. Array方法归类总结

    数组的转换方法 valueOf()方法,数组调用该方法后返回的还是原来的数组. toString()方法,数组调用该方法后会调用每一项的toStirng()方法,之后将每一项拼接成一个以逗号分割的字符 ...

  8. Kickstart Practice Round 2017 Google

    Problem B. Vote A and B are the only two candidates competing in a certain election. We know from po ...

  9. C++程序员的阅读清单

    link:http://www.who1753.com/must-read-c-book-list/ 多读一些优秀的书籍,对于开发者稳固编程基础.提高编程技能有很大帮助.但是,大多时候,初学者不知道应 ...

  10. Omi实战-QQ附近用户列表Web页

    原文地址https://github.com/AlloyTeam/omi/blob/master/docs/cn_pr_nearby.md 写在前面 Omi很适合大型复杂的Web页面开发,例如一些We ...