Bzoj 1588: [HNOI2002] turnover statistics

Source: Internet
Author: User

1588: [HNOI2002] turnover statistics time limit:5 Sec Memory limit:162 MB
submit:14396 solved:5521
[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 of the minimum fluctuation is the first day of 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


The problem data bug has been fixed.----2016.5.15

Source
#include <cstdio>#include<iostream>using namespacestd;Const intn=4e4+Ten;intn,rt,size,fa[n],c[n][2],num[n];intAns,tp,ts;voidRotateintXint&k) {    inty=fa[x],z=Fa[y],l,r; L= (c[y][1]==X); r=l^1; if(k==y) k=x; Elsec[z][c[z][1]==y]=x; FA[X]=z;fa[y]=x;fa[c[x][r]]=y; C[Y][L]=c[x][r];c[x][r]=y;}voidSplay (intXint&k) {     while(x!=k) {        inty=fa[x],z=Fa[y]; if(y!=k) {            if((c[y][0]==x) ^ (c[z][0]==y)) rotate (x,k); Elserotate (y,k);    } rotate (x,k); }}voidInsertintXint&k,intLast ) {    if(!k) {k=++size;num[k]=x;fa[k]=last;splay (K,RT);return ; } x<num[k]?insert (x,c[k][0],K): Insert (x,c[k][1],k);}voidPredint&k,intx) {    if(!k)return ; if(num[k]<=x) {TP=Num[k]; Pred (c[k][1],x); }    ElsePred (c[k][0],x);}voidSuccint&k,intx) {    if(!k)return ; if(num[k]>=x) {TS=Num[k]; SUCC (c[k][0],x); }    ElseSUCC (c[k][1],x);}intMain () {scanf ("%d",&N);  for(intI=0, x;i<n;i++) {scanf ("%d",&x); if(!RT) ans+=x; Else{TP=ts=-1;            Pred (rt,x); succ (rt,x); if(ts==-1) ans+= (xTP); Else if(tp==-1) ans+= (ts-x); ElseAns+=min (X-TP), (ts-x)); } insert (X,rt,0); } printf ("%d", ans); return 0;}

Bzoj 1588: [HNOI2002] turnover statistics

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.