2924: turnover statistics
Time limit (Common/Java): 1000 ms/3000 Ms memory limit: 65536 Kbyte
Total submit: 416 accepted: 130
Description
Tiger was recently promoted to the sales department manager by the company. After taking office, he took over the company's first task, which is to collect statistics and analyze the company's business since its establishment. Tiger took out the company's account book, which records the company's daily turnover since its establishment. Analyzing business conditions is a complicated task. Due to holidays, large prices, or other situations, the turnover may fluctuate. Of course, certain fluctuations are acceptable, but in some cases, the turnover changes very high or very low, this proves that the company's current business situation has encountered problems. A minimum Fluctuation value is defined in economic management to measure the situation:
Minimum fluctuation value of the day = min {| turnover of the previous day of the day-turnover of the Day |}
The larger the minimum Fluctuation value, the more unstable the business situation. To analyze whether the company's business conditions have stabilized since its establishment, you only need to add up the minimum fluctuation values for each day. Your task is to write a program to help tiger calculate this value. The minimum Fluctuation value on the first day is the turnover on the first day.
Input
Multiple groups of test data. The first positive integer N (1 <= n <= 32767) in each group indicates the number of days since its establishment. there is an integer AI (AI <= 1000000) in each row of the next n rows, indicating the turnover on the I day. Process to EOF.
Output
Each group of data occupies one row, and each row outputs an integer. The sum of the minimum fluctuation values per day. Result less than 2 ^ 31
Sample Input
6512546
Sample output
12
Hint
5 + | 1-5 | + | 2-1 | + | 5-5 | + | 4-5 | + | 6-5 | = 5 + 4 + 1 + 0 + 1 + 1 = 12
Source
Hnoi 2002
You can also use STL, treap or something, =
# Include <iostream> # include <cstdio> # include <algorithm> using namespace STD; # define INF 0x7fffffff # define n 40000 struct splaytree {int ch [N] [2], pre [N], SZ [N], Val [N]; int root, top; inline void insert (int K) // insert by size. Do not forget to rotate {int r = root; If (r = 0) {newnode (root, 0, k); return ;} while (CH [r] [Val [R] <K]) {r = CH [r] [Val [R] <K];} newnode (CH [r] [Val [R] <K], R, k); splay (CH [r] [Val [R] <K], 0 );} inline void rotate (Int X, int c) {int y = pre [X]; ch [y] [! C] = CH [x] [c]; If (CH [x] [c]) Pre [CH [x] [c] = y; pre [x] = pre [y]; If (pre [y]) ch [pre [y] [CH [pre [y] [1] = y] = x; ch [x] [c] = y; pre [y] = x; If (y = root) root = x;} inline void splay (int x, int f) {While (pre [x]! = F) {If (pre [pre [x] = f) rotate (x, CH [pre [x] [0] = X ); else {int y = pre [X], Z = pre [y]; int c = (CH [Z] [0] = y ); if (CH [y] [c] = x) rotate (x ,! C), rotate (x, c); else rotate (Y, c), rotate (x, c) ;}} if (F = 0) root = x ;} inline int getmax (int x) // find the maximum value of the subtree with X as the root {While (CH [x] [1]) x = CH [x] [1]; return Val [X];} inline int getmin (int x) // find the minimum value of the subtree with X as the root {While (CH [x] [0]) X = CH [x] [0]; return Val [X];} inline void newnode (Int & X, int F, int K) {x = ++ top; pre [x] = f; Val [x] = K; ch [x] [0] = CH [x] [1] = 0;} void Init () {root = Top = 0; ch [root] [0] = CH [root] [1] = Val [root] = pre [Root] = 0 ;}} t; int main () {int N, I, ANS, X; while (scanf ("% d", & N )! = EOF) {ans = 0; T. init (); for (I = 1; I <= N; I ++) {scanf ("% d", & X); T. insert (x); if (I = 1) ans + = x; else {int TMP = inf; If (T. ch [T. root] [0]) TMP = min (TMP, T. val [T. root]-t. getmax (T. ch [T. root] [0]); If (T. ch [T. root] [1]) TMP = min (TMP, T. getmin (T. ch [T. root] [1])-t. val [T. root]); ans + = TMP;} printf ("% d \ n", ANS);} return 0 ;}
Splay exercise questions [Taizhou University 2924] turnover statistics