treap-- turnover statistics

Turnover statistics

Tiger has recently been promoted to sales manager for the company, he received the first task assigned by the company after he took office is the statistics and analysis of business cases since its establishment.

Tiger out of the company's books, records a daily turnover since the company was founded on the books.

Analysis of business conditions is a very complex task.

Due to the holidays, sale or other situations when the turnover there will be some fluctuation, of course, a certain degree of fluctuation is acceptable, but at some point mutations turnover is very low or very high, which proves the company At this time, the operating conditions there is a problem.

It defines a minimum value fluctuations on economic management to measure this.

Provided turnover day i ai, the day i (i ≧ 2) the minimum value of the fluctuation is defined as fi:

f i = m i n 1 j < i a i a j f_i=min_1≤j<i|a_i−a_j|
When the greater volatility minimum value, it means that the business situation more unstable.

The analysis of the entire company from inception to the present business situation is stable, fluctuating just need a minimum value for each day add up to it.

Your task is to write a program to help Tiger to calculate this value.

The first day is the first day of a minimum fluctuation turnover a1.

The input format
of the first behavior positive integer n, the number of the company formed from a day until now.

The next n lines each have an integer ai (there may be a negative number), represents the i day of the company's turnover.

Output format
output a positive integer, and indicates the minimum value of the fluctuation.

data range
n 32767 , a i 106 n≤32767,a_i≤106
Input Sample:
. 6
. 5
. 1
2
. 5
. 4
. 6
Output Sample:
12
Sample explained
in the examples, 5 + | 1-5 | + |5 | + | 6-5 | = 5

Wrote a question on a lot better to write this question, this question so that the minimum target, obviously is to do the absolute value of the difference between the two numbers is closest to x. They are the minimum, maximum and minimum of the maximum values. That is the precursor and successor.

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=33010,inf=1e7;
struct Node{
    int l,r;
    int key,val;
}tr[N];
int root,idx;
int get_node(int key)
{
    tr[++idx].key=key;
    tr[idx].val=rand();
    return idx;
}
void build()
{
    get_node(-inf),get_node(inf);
    root=1,tr[root].r=2;
}
int zig(int &p)
{
    int q=tr[p].l;
    tr[p].l=tr[q].r,tr[q].r=p,p=q;
}
int zag(int &p)
{
    int q=tr[p].r;
    tr[p].r=tr[q].l,tr[q].l=p,p=q;
}
void insert(int &p,int key)
{
    if(!p) p=get_node(key);
    else if(tr[p].key==key) return;
    else if(tr[p].key>key){
        insert(tr[p].l,key);
        if(tr[tr[p].l].val>tr[tr[p].r].val) zig(p);
    }else{
        insert(tr[p].r,key);
        if(tr[tr[p].r].val>tr[tr[p].l].val) zag(p);
    }
}
int get_prev(int p,int key)
{
    if(!p) return -inf;
    else if(tr[p].key>key){
        return get_prev(tr[p].l,key);
    }
    return max(tr[p].key,get_prev(tr[p].r,key));
}
int get_next(int p,int key)
{
    if(!p) return inf;
    else if(tr[p].key<key) return get_next(tr[p].r,key);
    return min(tr[p].key,get_next(tr[p].l,key));
}
signed main()
{
    build();
    int n; scanf("%lld",&n);
    int res=0;
    for(int i=1;i<=n;i++){
        int x; scanf("%lld",&x);
        if(i==1) res+=x;
        else res+=min(x-get_prev(root,x),get_next(root,x)-x);
        insert(root,x);
    }
    cout<<res<<endl;
}
Published 181 original articles · won praise 10 · views 5066

Guess you like

Origin blog.csdn.net/weixin_42979819/article/details/104216226