## 1112: [POI2008]砖块Klo

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1664  Solved: 583
[Submit][Status][Discuss]

## Description

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

5 3
3
9
2
3
1

2

## Source

[Submit][Status][Discuss]

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 1E5 + 10;
typedef long long LL;

int n,k,cnt,Root,a[maxn],ch[maxn][2],key[maxn],va[maxn],siz[maxn];
LL Ans = 1E18,sum[maxn];

LL Sum(int x,int tot)
{
if (!tot) return 0;
if (siz[x] <= tot) return sum[x];
int Left = siz[ch[x][0]] + 1;
if (Left > tot) return Sum(ch[x][0],tot);
else return sum[ch[x][0]] + 1LL*va[x] + Sum(ch[x][1],tot - Left);
}

int Rank(int x,int tot)
{
int Left = siz[ch[x][0]] + 1;
if (Left == tot) return va[x];
else if (Left > tot) return Rank(ch[x][0],tot);
else return Rank(ch[x][1],tot - Left);
}

void Solve(LL Now,int mid1,int mid2)
{
LL z = mid1,sum = Sum(Root,mid1);
LL tot = Now*z - sum;
sum = Sum(Root,k) - Sum(Root,mid1);
tot += (sum - Now*z);
Ans = min(Ans,tot);
}

void maintain(int x)
{
sum[x] = va[x];
siz[x] = 1;
for (int i = 0; i < 2; i++)
if (ch[x][i]) {
siz[x] += siz[ch[x][i]];
sum[x] += sum[ch[x][i]];
}
}

void rotate(int &x,int d)
{
int y = ch[x][d];
ch[x][d] = ch[y][d^1];
maintain(x);
ch[y][d^1] = x;
x = y; maintain(x);
}

int cmp(int x,int w)
{
if (va[x] == w) return -1;
if (va[x] < w) return 1;
return 0;
}

int New(int w)
{
int ret = ++cnt;
ch[ret][0] = ch[ret][1] = 0;
key[ret] = rand();
va[ret] = sum[ret] = w;
siz[ret] = 1;
return ret;
}

void Insert(int &x,int w)
{
if (!x) {
x = New(w);
return;
}
int d = cmp(x,w);
d = max(d,0);
Insert(ch[x][d],w);
maintain(x);
if (key[ch[x][d]] > key[x]) rotate(x,d);
}

void Remove(int &x,int w)
{
int d = cmp(x,w);
if (d == -1) {
if (!ch[x][0] && !ch[x][1]) x = 0;
else if (!ch[x][0] || !ch[x][1])
x = ch[x][0]?ch[x][0]:ch[x][1];
else {
int d2 = key[ch[x][0]] > key[ch[x][1]]?0:1;
rotate(x,d2);
Remove(ch[x][d2^1],w);
maintain(x);
if (key[ch[x][d2^1]] > key[x])
rotate(x,d2^1);
}
return;
}
Remove(ch[x][d],w);
maintain(x);
if (key[ch[x][d]] > key[x])
rotate(x,d);
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif

cin >> n >> k;
if (k == 1) {
cout << 0;
return 0;
}
for (int i = 1; i <= n; i++)
scanf("%d",&a[i]);
for (int i = 1; i <= k; i++)
Insert(Root,a[i]);
for (int i = k; i <= n; i++) {
if (i > k) {
Remove(Root,a[i-k]);
Insert(Root,a[i]);
}
if (k&1) {
int mid = (k + 1) >> 1;
LL Now = Rank(Root,mid),tot = 0;
LL z = mid-1,sum = Sum(Root,mid-1);
tot += (z*Now - sum);
sum = Sum(Root,k) - Sum(Root,mid);
tot += (sum - z*Now);
Ans = min(Ans,tot);
}
else {
int mid1 = k>>1,mid2 = mid1+1;
int Now1 = Rank(Root,mid1);
int Now2 = Rank(Root,mid2);
int Now = (Now1 + Now2) >> 1;
Solve(Now,mid1,mid2);
if (Now1 != Now2)
Solve(Now + 1,mid1,mid2);
}
}
cout << Ans;
return 0;
}


#### Bzoj1112:[POI2008]砖块Klo:splay

2016-04-08 10:07:59

#### bzoj1112【poi2008】砖块klo

2015-11-24 23:54:17

#### [POI2008]砖块Klo

2012-01-10 19:42:17

#### bzoj-1112 砖块Klo

2015-09-15 07:42:17

#### bzoj1112 POI2008 砖块Klo 树状数组

2017-04-27 09:25:44

#### BZOJ 1112: [POI2008]砖块Klo 线段树维护区间中位数

2016-11-04 16:09:48

#### bzoj 1112 [POI2008]砖块Klo

2016-11-04 17:36:02

#### 【BZOJ 1112】 [POI2008]砖块Klo

2015-03-22 13:07:40

#### 1112: [POI2008]砖块Klo Splay+中位数

2016-01-12 08:56:26

#### 【BZOJ 1112】Poi2008砖块Klo treap

2017-02-15 22:08:49

## 不良信息举报

1112: [POI2008]砖块Klo