Bzoj topic 1588: [HNOI2002] turnover statistics (splay Tree to seek the predecessor successor)

Source: Internet
Author: User

1588: [HNOI2002] turnover statistics time limit: 5 Sec Memory Limit: 162 MB
Submit: 10828 Solved: 3771
[Submit] [Status] [Discuss] 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 and output requirements

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 Input6
5
1
2
5
4
6Sample Output12
HINT

Result Description: 5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12


This problem data, see the discussion version http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=1588

Source

Should be a lot of balance tree can do, splay first question, but feel splay slow point, when practiced hand

AC Code

/**************************************************************problem:1588user:kxh1995language:c++result: acceptedtime:1128 msmemory:2836 kb****************************************************************/#include < stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define MIN (b) (a>b?b:a ) #define N 100005#define INF 0x3f3f3f3fusing namespace Std;int pre[n],key[n],ch[n][2],root,tot1;int n;void newnode (int & Amp;r,int fa,intk) {r=++tot1;pre[r]=fa;key[r]=k;ch[r][0]=ch[r][1]=0;} void rotate (int x,int kind) {int y=pre[x];ch[y][!kind]=ch[x][kind];p re[ch[x][kind]]=y;if (Pre[y]) {ch[pre[y]][ch[pre[ Y]][1]==y]=x;} Pre[x]=pre[y];ch[x][kind]=y;pre[y]=x;} void splay (int r,int goal) {while (pre[r]!=goal) {if (pre[pre[r]]==goal) rotate (r,ch[pre[r]][0]==r); Else{int Y=pre[r]; int Kind=ch[pre[y]][0]==y;if (ch[y][kind]==r) {rotate (r,!kind); rotate (r,kind);} Else{rotate (r,kind); rotate (r,kind);}} if (goal==0) root=r;} int insert (int k) {int r=root;while (ch[r][key[r]<k]) {if (key[r]==k) {splay (r,0); return 0;} R=CH[R][KEY[R]&LT;K];} NewNode (ch[r][key[r]<k],r,k); splay (ch[r][key[r]<k],0); return 1;} int get_pre (int x) {int temp=ch[x][0];if (temp==0) return Inf;while (Ch[temp][1]) temp=ch[temp][1];//printf ("++++++%d%d \ n ", key[temp],key[x]); return key[x]-key[temp];} int get_next (int x) {int temp=ch[x][1];if (temp==0) return Inf;while (Ch[temp][0]) temp=ch[temp][0];//printf ("******%d% D\n ", Key[temp],key[x]); return key[temp]-key[x];} int main () {int n;while (scanf ("%d", &n)!=eof) {root=tot1=0;int ans=0;for (int i=1;i<=n;i++) {int num;if (scanf ("%d") ", &num) ==eof)//perverted ah, not so write will wanum=0;//scanf ("%d ", &num); if (i==1) {ans+=num;newnode (root,0,num); continue;} if (!insert (num)) Continue;int a=get_pre (root); int b=get_next (root); Ans+=min (A, b);} printf ("%d\n", ans);}?


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj topic 1588: [HNOI2002] turnover statistics (splay Tree to seek the predecessor successor)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.