zoukankan      html  css  js  c++  java
  • bzoj1112 [POI2008]砖块Klo

    [POI2008]砖块Klo

    Time Limit: 10 Sec Memory Limit: 162 MB

    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

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


    初中老师教我们肯定选中位数。。。
    所以我们要维护这个东西。。。
    那就平衡树呗。。。
    然鹅我打板不是很6
    一直TLE。。。用血的教训证明了 Splay 真的要看你和你的父亲是不是弯的啥的。。。
    真的不能无脑转到根啊。。。

    
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5 + 5, L = 0, R = 1;
    const long long INF = 1e11 + 5;
    struct lpl{
    	int size, tim, fa, son[2];
    	long long data, sum;
    }node[maxn];
    int n, k, lin, cnt, root, ini[maxn];
    long long ans = INF, sum1, sum2;
    
    inline void update(int t)
    {
    	node[t].size = node[node[t].son[L]].size + node[node[t].son[R]].size + node[t].tim;
    	node[t].sum = node[node[t].son[L]].sum + node[node[t].son[R]].sum + node[t].data * node[t].tim; 
    }
    
    inline void rotate(int t)
    {
    	int fa = node[t].fa, grdfa = node[fa].fa, which = (node[fa].son[R] == t);
    	node[grdfa].son[node[grdfa].son[R] == fa] = t; node[t].fa = grdfa;
    	node[fa].son[which] = node[t].son[which ^ 1]; node[node[t].son[which ^ 1]].fa = fa;
    	node[t].son[which ^ 1] = fa; node[fa].fa = t;
    	update(fa); update(t);
    }
    
    inline void Splay(int t, int k)
    {
    	while(node[t].fa != k){
    		int fa = node[t].fa, grdfa = node[fa].fa;
    		if(grdfa != k){
    			if((node[fa].son[R] == t) ^ (node[grdfa].son[R] == fa)) rotate(t);
    			else rotate(fa);
    		}
    		rotate(t);
    	}
    	if(!k) root = t;
    }
    
    inline void insert(long long t)
    {
    	int now = root, fa = 0;
    	while(1){
    		if(!now) break;
    		if(node[now].data == t) break;
    		fa = now;
    		if(t < node[now].data) now = node[now].son[L];
    		else now = node[now].son[R];
    	}
    	if(now){
    		node[now].size++; node[now].tim++; Splay(now, 0); return;
    	}
    	node[fa].son[t > node[fa].data] = ++cnt;
    	node[cnt].fa = fa; node[cnt].data = t; node[cnt].tim = node[cnt].size = 1;
    	Splay(cnt, 0);
    }
    
    inline void delet(int t)
    {
    	int now = root;
    	while(t != node[now].data && node[now].son[t > node[now].data] != 0) now = node[now].son[t > node[now].data];
    	Splay(now, 0);
    	if(node[now].tim > 1){node[now].tim--; update(now); return;}
    	if(!node[now].son[L]){root = node[now].son[R]; node[root].fa = 0; return;}
    	int pre = node[root].son[L];
    	while(node[pre].son[R] != 0) pre = node[pre].son[R];
    	Splay(pre, root);
    	node[node[root].son[R]].fa = pre; node[pre].fa = 0;  node[pre].son[R] = node[root].son[R]; 
    	root = pre; update(root);		
    }
    
    void find(int t, int rank)
    {	
    	if(!t) return;
    	if(rank > node[node[t].son[L]].size && rank <= node[node[t].son[L]].size + node[t].tim){
    		sum1 += node[node[t].son[L]].sum + (rank - node[node[t].son[L]].size - 1) * node[t].data;
    		sum2 += node[node[t].son[R]].sum + (node[node[t].son[L]].size + node[t].tim - rank) * node[t].data;
    		lin = node[t].data;
    	}
    	else if(rank <= node[node[t].son[L]].size){
    		sum2 += (node[node[t].son[R]].sum + node[t].tim * node[t].data);
    		find(node[t].son[L], rank);
    	}
    	else{
    		sum1 += (node[t].data * node[t].tim + node[node[t].son[L]].sum);
    		find(node[t].son[R], rank - node[node[t].son[L]].size - node[t].tim);
    	}
    }
    
    inline void workk()
    {
    	sum1 = sum2 = 0;
    	int t = (k + 1) / 2;
    	find(root, t); //sum1 += INF; sum2 -= INF;
    	long long w = (t - 1) * lin - sum1 + sum2 - (k - t) * lin;
    	ans = min(ans, w);
    }
    
    int main()
    {
    	//freopen("data.in", "r", stdin);
    	//freopen("lpl.out", "w", stdout);
    	scanf("%d%d", &n, &k);
    	for(int i = 1; i <= n; ++i) scanf("%d", &ini[i]);
    	//insert(INF); insert(-INF);
    	for(int i = 1; i < k; ++i) insert(ini[i]);
    	for(int i = k; i <= n; ++i){
    		insert(ini[i]); 
    		workk(); 
    		delet(ini[i - k + 1]);
    	}
    	cout << ans;
    	return 0;
    } 
    
    
    心如花木,向阳而生。
  • 相关阅读:
    LCA最近公共祖先Tarjan(离线)
    51nod 1135 原根
    51nod 1134最长递增子序列
    51nod 1130 斯特林公式
    51nod 1186 Miller-Rabin素数测试
    51Nod 1257 背包问题 V3
    另类求组合数
    Gym
    msp430项目编程45
    msp430项目编程44
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9202232.html
Copyright © 2011-2022 走看看