题解 P2234 [HNOI2002]营业额统计

· · 个人记录

这个题目有很多种解了,包括还有人用排序过的厉害厉害 本文主要讲的就是利用伸展树的方式来写

题目分析:

你的任务就是编写一个程序帮助Tiger来计算把每一天的最小波动值加起来的最小值。

附:该天的最小波动值=min{|该天以前某一天的营业额-该天营业额|}。

Q: 既然要求最小值,那如何得到最小值呢?

A: 其实这个问题并不难

举个例子:(样例)

输入
6 
5 1 2 5 4 6
输出
12

样例解释:12=5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1

既然一天的最小波动值为:该天以前某一天的营业额-该天营业额**

那我们就让这个值尽可能的小。

首先,该天的营业额是个常量吧,所以我们要让该天以前某一天的营业额最接近该天的营业额的值,使得他们的差值尽可能小,所以就能令最小波动值更小

很容易理解吧……

Q:那如何找到该天以前某一天的营业额最接近该天的营业额的值呢?

A:其实这个也不难,我们先建立一个BST(二叉查找树),然后每个节点的权值就为营业额的值,在每次插入节点的时候,找该天以前某一天的营业额最接近该天的营业额的值——即为找这个节点的前驱和后继,然后如果有与该天的营业额相等的值,就用该值(可以加个变量same记重复)。最后计算即可

当然了,在找前驱和后继的时候可以用多种平衡树的算法,本人用的就是Splay算法。

PS:本题已完……

【参考程序】

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int maxn=200000+10;
int n,x,sum;

struct node
{
    int val,same,size;
    node *father;
    node *lson,*rson;//size是以该节点为根的孩子数,same是有多少个重复
};
node *root,tree[maxn],*null;
int cnt;

node* NewNode(int val)//新建一个节点 
{
    tree[++cnt].val=val;
    tree[cnt].size=1;
    tree[cnt].same=1;
    tree[cnt].lson=tree[cnt].rson=null;
    return tree+cnt;
}

void init()//初始化 
{
    cnt=0;
    null=tree+cnt;
    node *q=NewNode(100000000);
    root=NewNode(-100000000);//哨兵
    root->rson=q;
    q->father=root;
    root->size=2;
    root->father=null;
}

void update(node *x)//维护x的size 
{
    x->size=x->same;
    if(x->lson!=null)x->size+=x->lson->size;
    if(x->rson!=null)x->size+=x->rson->size;
}

void zig(node *x)//右旋 
{
    node *y=x->father,*z=y->father;
    y->lson=x->rson;
    if(x->rson!=null)x->rson->father=y;
    update(y);
    x->rson=y;
    y->father=x;
    update(x);
    x->father=z;
    if(z!=null)
    {
        if(z->lson==y)z->lson=x;
        else z->rson=x;
    }
}

void zag(node *x)//左旋 
{
    node *y=x->father,*z=y->father;
    y->rson=x->lson;
    if(x->lson!=null)x->lson->father=y;
    update(y);
    x->lson=y;
    y->father=x;
    update(x);
    x->father=z;
    if(z!=null)
    {
        if(z->lson==y)z->lson=x;
        else z->rson=x;
    }
}

void splay(node *x,node *fa)//伸展 
{
    while(x->father!=fa)
    {
        node *y=x->father,*z=y->father;
        if(z==fa)
        {
            if(x==y->lson)zig(x);
            else zag(x);
        }
        else
        {
            bool L=(x==y->lson),R=(y==z->lson);
            if(L&&R)zig(y),zig(x);
            else if(L&&!R)zig(x),zag(x);
            else if(!L&&R)zag(x),zig(x);
            else zag(y),zag(x);
        }
    }
    if(x->father==null)root=x;
}

node *pred(node *now,int val,node *optimal)//求k的前驱
{
    if(now==null)return optimal;
    if(val>now->val)return pred(now->rson,val,now);
    else return pred(now->lson,val,optimal);
}

node *succ(node *now,int val,node *optimal)//求k的后继 
{
    if(now==null)return optimal;
    if(val<now->val)return succ(now->lson,val,now);
    else return succ(now->rson,val,optimal);
}

node *insert(node *now,int val)//插入
{
    node *p=pred(root,val,null);
    splay(p,null);
    node *q=succ(root,val,null);
    splay(q,p);
    if(q->lson!=null)q->lson->same++,q->lson->size++;
    else
    {
        q->lson=NewNode(val);
        q->lson->father=q;
    }
    update(q);
    update(root);
    return q->lson;
}

int main()
{
    scanf("%d",&n);
    init();//初始化
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x); 
        node *now=insert(root,x);//插入一个节点
        splay(now,null);//伸展到根节点
        node *p=pred(root,x,null);//找前驱
        splay(p,null);
        node *q=succ(root,x,null);//找后继
        splay(q,p);
        if(i==1)sum+=x;
        else if(now->same==1)//只出现过一次(包括这一次插入)
            sum+=min(abs(x-(p->val)),abs((q->val)-x));
    }   
    cout<<sum;
}