splay Tree Extension trees The basic operation and application of extensional tree
Description
Turnover statistics Tiger has recently been promoted to Sales manager, the first task he received after taking office was to count and analyze the company's business since its inception. Tiger took out the company's ledger, which recorded the daily turnover of the company since its inception. Analysis of the business situation is a rather complicated task. Due to holidays, big sale or other circumstances, the turnover will be a certain fluctuation, of course, certain fluctuations are acceptable, but at some point the turnover is very high or low, which proves that the company at this time the operation of the situation has been a problem. The economic management defines a minimum fluctuation value to measure the situation: the greater the minimum fluctuation of the day, the greater the worth, the more unstable the business situation. And the analysis of the entire company from the establishment to the current business is stable, only need to put the minimum fluctuation of each day to add up on it. Your task is to write a program to help Tiger calculate this value. The first day's minimum fluctuation value is the first day's turnover. Input/output requirement input
The first act is a positive integer representing the number of days from the time the company was established to the present, and the next n rows have an integer (possibly negative) on each line, representing the turnover of the company for the day I. Output
The output file has only a positive integer, which is sigma (the minimum daily fluctuation value). The result is less than 2^31. Sample Input 6
5
1
2
5
4
6 Sample Output 12
HINT
Result Description: 5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
The problem data bug has been fixed.----2016.5.15
Source
[Submit] [Status] [Discuss] online find the code reference knocking the address http://spaceq.is-programmer.com/posts/39746.html Accode:
#include <bits/stdc++.h> #define MAXN 1000010 #define INF 1E9 #define LC (X) ch[(x)][0] #define min (x, y) > (y)? (
Y):(x) using namespace std;
int fa[maxn],ch[maxn][2],k[maxn],root,ind=1;
inline void rotate (int p) {int q=fa[p],y=fa[q],x=ch[q][1]==p;
ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;
Ch[p][x^1]=q;fa[q]=p;
Fa[p]=y;
if (y) {if (ch[y][0]==q) ch[y][0]=p;
else if (ch[y][1]==q) ch[y][1]=p; }}-inline void splay (int x) {for (int y;y=fa[x];rotate (x)) if (Fa[y]) rotate ((X==LC (y)) = = (Y==LC (FA [y]))?
Y:X);
Root=x;
} inline void Insert (int x,int v) {int y;
while (true) {y=ch[x][k[x]<v];
if (y==0) {y=++ind;
K[y]=v;
ch[y][0]=ch[y][1]=0;
Fa[y]=x;
ch[x][k[x]<v]=y;
Break
} x=y;
}splay (y);
} inline int pre (int x) {int tmp=ch[x][0];
while (Ch[tmp][1]) tmp=ch[tmp][1];
return k[tmp];
} inline int suc (int x) { int tmp=ch[x][1];
while (Ch[tmp][0]) tmp=ch[tmp][0];
return k[tmp];
} int n,t,ans=0;
inline void init () {root=1;ans=k[root]=t;
fa[root]=ch[root][0]=ch[root][1]=0;
Insert (Root,inf); insert (Root,-inf);
} int main () {Ios::sync_with_stdio (false);
while (cin>>n) {cin>>t; init ();
for (int i=2;i<=n;i++) {cin>>t; insert (root,t);
Ans+=min (T-pre (Root), suc (root)-T);
} cout<<ans<< ' \12 ';
} return 0; }