bzoj1112: [POI2008]砖块Klo

链接

  http://www.lydsy.com/JudgeOnline/problem.php?id=1112

题解

  看一下解的形式。
  你要选一段数,把他们都变成一个数字,假设变成的数字是 x ,那么你的代价是

i=lr|aix|

  这显然就是让你在数轴上找个点,到其它所有点的距离之和最小。这不是一个高中数学套路题吗?
  直接取中位数不就好了。
  那这个问题就成了查一个区间的中位数,还问你这个区间的每个数减去一个数的绝对值之和。
  这不就是主席树吗?

代码

//主席树
#include <cstdio>
#include <algorithm>
#define maxn 100005
#define ll long long
using namespace std;
int N, ndtot, ans, K, tmp[maxn], h[maxn], lim=100000;
struct segtree
{int l, r, size;ll sum;segtree *ch[2];}pool[maxn*20], *root[maxn];
void ins(segtree *pre, segtree *now, int pos)
{
    int mid=(pre->l+pre->r)>>1;
    *now=*pre;
    now->size++, now->sum+=tmp[pos];
    if(now->l==now->r)return;
    if(pos<=mid)ins(pre->ch[0],now->ch[0]=pool+ ++ndtot,pos);
    else ins(pre->ch[1],now->ch[1]=pool+ ++ndtot,pos);
}
int kth(segtree *pre, segtree *now, int k)
{
    if(now->l==now->r)return now->l;
    int s=now->ch[0]->size-pre->ch[0]->size;
    if(k<=s)return kth(pre->ch[0],now->ch[0],k);
    else return kth(pre->ch[1],now->ch[1],k-s);
}
ll segsum(segtree *pre, segtree *now, int l, int r)
{
    if(l>r)return 0;
    int mid=(pre->l+pre->r)>>1;
    ll s=0;
    if(l<=pre->l and r>=pre->r)return now->sum-pre->sum;
    if(l<=mid)s+=segsum(pre->ch[0],now->ch[0],l,r);
    if(r>mid)s+=segsum(pre->ch[1],now->ch[1],l,r);
    return s;
}
int getsz(segtree *pre, segtree *now, int l, int r)
{
    if(l>r)return 0; 
    int mid=(pre->l+pre->r)>>1, s=0;
    if(l<=pre->l and r>=pre->r)return now->size-pre->size;
    if(l<=mid)s+=getsz(pre->ch[0],now->ch[0],l,r);
    if(r>mid)s+=getsz(pre->ch[1],now->ch[1],l,r);
    return s;
}
void build(segtree *p, int l, int r)
{
    int mid=(l+r)>>1;
    p->l=l, p->r=r;
    if(l==r)return;
    build(p->ch[0]=pool+ ++ndtot,l,mid);
    build(p->ch[1]=pool+ ++ndtot,mid+1,r);
}
inline int read(int x=0)
{
    char c=getchar();
    while(c<48 or c>57)c=getchar();
    while(c>=48 and c<=57)x=(x<<1)+(x<<3)+c-48,c=getchar();
    return x;
}
void init()
{
    int i;
    N=read(), K=read();
    build(root[0]=pool+ ++ndtot,1,lim);
    for(i=1;i<=N;i++)tmp[i]=h[i]=read();
    sort(tmp+1,tmp+N+1);
    for(i=1;i<=N;i++)h[i]=lower_bound(tmp+1,tmp+N+1,h[i])-tmp;
    for(i=1;i<=N;i++)ins(root[i-1],root[i]=pool+ ++ndtot,h[i]);
}
void work()
{
    ll ans=(ll)1<<60, t;
    int i, x;
    for(i=K;i<=N;i++)
    {
        x=kth(root[i-K],root[i],K+1>>1);
        t=tmp[x]*getsz(root[i-K],root[i],1,x-1)-segsum(root[i-K],root[i],1,x-1)
         +segsum(root[i-K],root[i],x+1,lim)-tmp[x]*getsz(root[i-K],root[i],x+1,lim);
        ans=min(ans,t);
    }
    printf("%lld",ans);
}
int main()
{
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值