1588: [HNOI2002]营业额统计 (splay tree)
2013-08-24 22:08
330 查看
1588: [HNOI2002]营业额统计
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859
[Submit][Status]
Description
营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。 输入输出要求Input
第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个正整数 ,表示第i天公司的营业额。Output
输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。Sample Input
65
1
2
5
4
6
Sample Output
12HINT
结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12Source
[Submit][Status]HOME Back
/* *********************************************** Author :kuangbin Created Time :2013/8/24 10:57:20 File Name :F:\2013ACM练习\专题学习\splay_tree_2\营业额统计.cpp ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; #define Key_value ch[ch[root][1]][0] const int MAXN = 1000010; int pre[MAXN],ch[MAXN][2],key[MAXN]; int root,tot1; int s[MAXN],tot2;//内存池 void NewNode(int &r,int father,int k) { if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就要是++tot2 else r = ++tot1;//这个必须是++tot1 pre[r] = father; ch[r][0] = ch[r][1] = 0; key[r] = k; } //初始化 void Init() { root = tot1 = tot2 = 0; ch[root][0] = ch[root][1] = key[root] = pre[root] = 0; } //旋转,0为左旋,1为右旋 void Rotate(int x,int kind) { int y = pre[x]; ch[y][!kind] = ch[x][kind]; pre[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; } //Splay调整,将r结点调整到goal下面 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(y,kind); Rotate(r,kind); } } } if(goal == 0)root = r; } void Insert(int k)//插入一个值为k的结点(有重复插入) { int r = root; if(r == 0) { NewNode(root,0,k); return; } while(ch[r][key[r]<k]) r = ch[r][key[r]<k]; NewNode(ch[r][key[r]<k],r,k); Splay(ch[r][key[r]<k],0);//转到根部 } //找前驱 int Get_pre(int r) { if(ch[r][0] == 0)return -1;//不存在 r = ch[r][0]; while(ch[r][1])r = ch[r][1]; return r; } //找后继 int Get_next(int r) { if(ch[r][1] == 0)return -1; r = ch[r][1]; while(ch[r][0])r = ch[r][0]; return r; } const int INF = 0x3f3f3f3f; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; while(scanf("%d",&n) == 1) { Init(); int a; int ans = 0; for(int i = 0;i < n;i++) { if(scanf("%d",&a) == EOF) a= 0; Insert(a); if(i == 0) ans += a; else { int tmp = INF; int t1 = Get_pre(root); if(t1 != -1) tmp = min(tmp,key[root] - key[t1]); int t2 = Get_next(root); if(t2 != -1) tmp = min(tmp,key[t2] - key[root]); ans += tmp; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- BZOJ 题目1588: [HNOI2002]营业额统计(Splay Tree 求前驱后继)
- bzoj 1588 [HNOI2002]营业额统计 splay tree
- [HNOI2002]营业额统计 Splay tree
- [Bzoj1588][HNOI2002]营业额统计 (Treap|Splay)
- BZOJ 1588 [HNOI2002]营业额统计
- [BZOJ 1588] [HNOI 2002] 营业额统计
- bzoj 1588: [HNOI2002]营业额统计 treap
- BZOJ 1588([HNOI2002]营业额统计-set)
- [BZOJ 1588][HNOI2002]营业额统计
- [HNOI2002]营业额统计 Splay tree入门题
- [BZOJ 1588] HNOI 2002 营业额统计
- BZOJ1588: [HNOI2002]营业额统计
- bzoj-1588 1588: [HNOI2002]营业额统计(BST)
- BZOJ 1588: [HNOI2002]营业额统计
- bzoj 1588 [HNOI2002]营业额统计 splay
- [HNOI2002]营业额统计 Splay tree
- [BZOJ1588][HNOI2002]营业额统计
- BZOJ 1588 HNOI2002 营业额统计 裸Treap .
- BZOJ 1588 [HNOI2002]营业额统计
- bzoj 1588: [HNOI2002]营业额统计