1588: [HNOI2002] turnover statistics time limit: 5 Sec Memory Limit: 162 MB
Submit: 10371 Solved: 3597
[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
C + + Set invocation
Remember some data will be a few lines less
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime> #include <set>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (i,k,n) for (int i=k;i<=n;i++) # Define REP (I,n) for (int. i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (i,n) for (int i=n;i>=0;i--) # Define Forp (x) for (int p=pre[x];p, p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define Lson (x< <1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)); Define Memi (a) memset (A,128,sizeof (a)), #define INF (1ll<<32) #define F (100000007) typedef long Long Ll;ll Mul (ll a,l L b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} Set<ll> s;set<ll>::iterator S1;int Main () {//freopen ("Bzoj1588.in", "R", stdin)//freopen (". Out", "w", stdout); int n;cin>>n;if (n==0) {cout<< "0\n"; return 0;} ll Ans=0; {N--;int p;cin>>p; S.insert (P); ans=p;} while (n--) {int p=0;cin>>p;s1=s.lower_bound (p); ll t=inf;if (S1!=s.end ()) {T=min (T,abs ((*S1)-P));} if (S1!=s.begin ()) {T=min (T,abs ((*--S1)-P));} ans+=t; S.insert (P);} Cout<<ans<<endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 1588 ([HNOI2002] turnover statistics-set)