Bzoj 1588: [HNOI2002] turnover statistics time limit:5 Sec Memory limit:512 MB
submit:9619 solved:3287 Topic Connection
http://www.lydsy.com/JudgeOnline/problem.php?id=1588
Description turnover Statistics Tiger was recently 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 output requires input first positive integer, which indicates the number of days from the time the company was established, and the next n rows have an integer (possibly negative) on each line, representing the turnover of the company. Outputthe 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
Exercises
The mark is stretching the tree, but what will not do? That can be like me as the water over the past is good ah, is thrown into an array inside, sort, and then make Ah, find the precursor and successor, constantly updated on the specific look at the code, code is very clever ~ ~\ (≧▽≦)/~ la La
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 400001#defineMoD 10007#defineEPS 1e-9//const int INF=0X7FFFFFFF; //infinitely LargeConst intinf=0x3f3f3f3f;/**///**************************************************************************************structnode{intP,v,left,right;} A[MAXN];intN,ra[maxn];inlineBOOLCMP (node A,node b) {returna.v<B.V;}intMain () {CIN>>N; for(intI=1; i<=n;i++) {cin>>a[i].v; A[I].P=i; } sort (A+1, a+n+1, CMP); for(intI=1; i<=n;i++) {RA[A[I].P]=i; A[i].left=i-1; A[i].right=i+1; } a[n].right=0; intans=a[ra[1]].v; for(intI=n;i>1; i--) { intx=Ra[i]; if(a[x].left!=0&&a[x].right!=0) {ans+=min (a[x].v-a[a[x].left].v,a[a[x].right].v-a[x].v); A[a[x].left].right=A[x].right; A[a[x].right].left=A[x].left; } Else if(a[x].left==0) {ans+=a[a[x].right].v-a[x].v; A[a[x].right].left=0; } Else{ans+=a[x].v-a[a[x].left].v; A[a[x].left].right=0; }} cout<<ans<<Endl; return 0;}
Bzoj 1588: [HNOI2002] turnover statistics doubly linked list