1112: [POI2008]砖块Klo

1112: [POI2008]砖块Klo

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 1664   Solved: 583
[ 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

[ Submit][ Status][ Discuss]

提问:给定一维坐标系上的k个点,找一个点,使得它到其它点的距离和最小
找中位数啊。。。。
维护一个treap就行
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值