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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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;
}

你可能感兴趣的:(treap)