luogu P2234 [HNOI2002]营业额统计

二次联通门 : luogu P2234 [HNOI2002]营业额统计

/*
    luogu P2234 [HNOI2002]营业额统计

    splay
    每次插入一个数
    查询它的前驱与后继

    有两点需要注意
    1.大部分的数据有误。。即输入的数据不够。。
    2.判断前驱后继是否存在
    3.注意判断该数是否已出现过
*/
#include <cstdio>

#define Max 50000
#define INF 1e8

namespace Z
{
    inline int min (int a, int b)
    {
        return a < b ? a : b;
    }

    inline int abs (int x)
    {
        return x > 0 ? x : -x;
    }

    void read (int &now)
    {
        now = 0;
        register char word = getchar ();
        bool temp = false;
        while (word < ‘0‘ || word > ‘9‘)
        {
            if (word == ‘-‘)
                temp = true;
            word = getchar ();
        }
        while (word >= ‘0‘ && word <= ‘9‘)
        {
            now = now * 10 + word - ‘0‘;
            word = getchar ();
        }
        if (temp)
            now = -now;
    }
}

class Splay_Tree_Type
{
    private :

        struct Splay_Tree_Date
        {
            int key;
            int weigth;
            int size;
            int father;
            int child[2];
        }
        tree[Max];

        int Count;
        int Root;

        inline int Get_Son (int now)
        {
            return tree[tree[now].father].child[1] == now;
        }

        inline void Rotate (int now)
        {
            int father = tree[now].father;
            int Grand = tree[father].father;
            int pos = Get_Son (now);
            tree[father].child[pos] = tree[now].child[pos ^ 1];
            tree[tree[father].child[pos]].father = father;
            tree[now].child[pos ^ 1] = father;
            tree[father].father = now;
            tree[now].father = Grand;
            if (Grand)
                tree[Grand].child[tree[Grand].child[1] == father] = now;
            Update (father);
            Update (now);
        }

        inline void Update (int now)
        {
            tree[now].size = tree[now].weigth;
            if (tree[now].child[0])
                tree[now].size += tree[tree[now].child[0]].size;
            if (tree[now].child[1])
                tree[now].size += tree[tree[now].child[1]].size;
        }

        inline void Splay (int now)
        {
            for (int father; father = tree[now].father; Rotate (now))
                if (tree[father].father)
                    Rotate (Get_Son (now) == Get_Son (father) ? father : now);
            Root = now;
        }

    public :

        void Insert (int x)
        {
            if (!Root)
            {
                Count++;
                tree[Count].key = x;
                tree[Count].size = 1;
                tree[Count].weigth = 1;
                Root = Count;
                return ;
            }
            int now = Root, father = 0;
            while (true)
            {
                if (tree[now].key == x)
                {
                    tree[now].size++;
                    tree[now].weigth++;
                    Splay (now);
                    return ;
                }
                father = now;
                now = tree[now].child[x > tree[now].key];
                if (!now)
                {
                    Count++;
                    tree[father].child[x > tree[father].key] = Count;
                    tree[Count].key = x;
                    tree[Count].father = father;
                    tree[Count].size = 1;
                    tree[Count].weigth = 1;
                    Splay (Count);
                    return ;
                }
            }
        }

        int Get_Prefix ()
        {
            if (tree[Root].weigth > 1)
                return tree[Root].key;
            int now = tree[Root].child[0];
            if (now == 0)
                return INF;
            while (tree[now].child[1])
                now = tree[now].child[1];
            return tree[now].key;
        }

        int Get_Suffix ()
        {
            if (tree[Root].weigth > 1)
                return tree[Root].key;
            int now = tree[Root].child[1];
            if (now == 0)
                return INF;
            while (tree[now].child[0])
                now = tree[now].child[0];
            return tree[now].key;
        }
};

Splay_Tree_Type Make;

int N;

int main (int argc, char *argv[])
{
    Z :: read (N);
    int x;
    int Prefix, Suffix;
    Z :: read (x);
    Make.Insert (x);
    int Answer =  Z :: abs (x);
    for (int i = 1; i < N; i++)
    {
        if (scanf ("%d", &x) == EOF)
            x = 0;
        Make.Insert (x);
        Prefix = Make.Get_Prefix ();
        Suffix = Make.Get_Suffix ();
        Answer += Z :: min (Z :: abs (x - Prefix), Z :: abs (x - Suffix));
    }
    printf ("%d", Answer);
    return 0;
}
时间: 2025-01-01 02:20:06

luogu P2234 [HNOI2002]营业额统计的相关文章

P2234 [HNOI2002]营业额统计 (权值线段树)

P2234 [HNOI2002]营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是一项相当复杂的工作.由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题.经济管理学上定义了一种最小波动值来衡量这种情况: 当最小波动值越大

洛谷P2234 [HNOI2002]营业额统计

题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是一项相当复杂的工作.由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题.经济管理学上定义了一种最小波动值来衡量这种情况: 当最小波动值越大时,就说明营业情况越不稳定. 而分析整个公司

洛谷:P2234 [HNOI2002]营业额统计

原题地址:https://www.luogu.org/problemnew/show/P2234 题目简述 给定一个序列,对于每一个数都要查询:序列中在这个数前与这个数最接近的数是什么?然后将最接近的数字与这个数字的差累加.(序列第一个数字直接加自己) 思路 查询在这个数之前与这个数最接近的数,我们很容易想到用二叉搜索树(BST)来做. 虽然数据略水暴力排序每次查询从一个数往左右找也能过. 每次插入一个数字,然后查询,我用Treap实现(还是弱化版的,只有插入查询). Treap的核心其实就是打

P2234 [HNOI2002]营业额统计(50分。。。。)

50 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 50007 #define inf 0x7fffffff using namespace std; int n,a[N],d[N],ans; struct Data { int x,ord; }data[N]; struct tree { int l,r,mn,mx,sum; }tr[N

BZOJ 1588: [HNOI2002]营业额统计

1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14396  Solved: 5521[Submit][Status][Discuss] Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是一项相当复杂的工作.由于节假日,大减价或者是其

HNOI2002营业额统计(平衡树)

标准的平衡树. 贴个splay吧 var v,l,r,fa:array[0..100000] of longint; root,x,i,n,ans:longint; procedure zig(x:longint); var y,z:longint; begin y:=fa[x];z:=fa[y]; if root=y then root:=x; l[y]:=r[x]; if r[x]<>0 then fa[r[x]]:=y; r[x]:=y; fa[y]:=x; fa[x]:=z; if z

BZOJ 题目1588: [HNOI2002]营业额统计(Splay Tree 求前驱后继)

1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 10828  Solved: 3771 [Submit][Status][Discuss] Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是一项相当复杂的工作.由于节假日,大减价或者

BZOJ1588: [HNOI2002]营业额统计[BST]

1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Status][Discuss] Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是一项相当复杂的工作.由于节假日,大减价或者是其

BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap

1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是一项相当复杂的工作.由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题.经济管理学上定义了一种最小波动值来衡量这