wikioi 1396 伸展樹(兩個模板),wikioi1396
題目描述
Description
Tiger最近被公司升任為營業部經理,他上任後接受公司交給的第一項任務便是統計並分析公司成立以來的營業情況。
Tiger拿出了公司的賬本,賬本上記錄了公司成立以來每天的營業額。分析營業情況是一項相當複雜的工作。由於節假日,大減價或者是其他情況的時候,營業額會出現一定的波動,當然一定的波動是能夠接受的,但是在某些時候營業額突變得很高或是很低,這就證明公司此時的經營狀況出現了問題。經濟管理學上定義了一種最小波動值來衡量這種情況:
該天的最小波動值 = min{|該天以前某一天的營業額-該天營業額|}
當最小波動值越大時,就說明營業情況越不穩定。
而分析整個公司的從成立到現在營業情況是否穩定,只需要把每一天的最小波動值加起來就可以了。你的任務就是編寫一個程式協助Tiger來計算這一個值。
第一天的最小波動值為第一天的營業額。
輸入描述
Input Description
第一行為正整數n(n<=32767),表示該公司從成立一直到現在的天數,接下來的n行每行有一個正整數ai(ai<=1000000),表示第i天公司的營業額。
輸出描述
Output Description
輸出檔案僅有一個正整數,即每天最小波動值之和,小於231
範例輸入
Sample Input
6
5
1
2
5
4
6
範例輸出
Sample Output
12
資料範圍及提示
Data Size & Hint
結果說明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
思路:伸展樹從下午開始看,以前看過了,覺得太麻煩了,然後資料結構老師講過之後,我學明白了,考試都秒過,然後現在了才開始做題。因為在資料結構實現中,本人比較喜歡數組替代指標,所以尼瑪找了好久的模板才找到這個讓自己覺得很爽又很能理解的。其他都是指標來實現,代碼又長,煩死了。
因為找了一下午加一晚上,實在找不到數組實現的伸展樹了,就查了這個入門題的解題報告,終於讓我發現了這個好代碼,嘿嘿。
參考代碼部落格:http://blog.csdn.net/rowanhaoa/article/details/24436703
說明:伸展樹是建立在排序二叉樹上的,所以左子樹所有的值都比根小,根又都比右子樹所有的值小。因為伸展樹是平衡二叉樹,所以具備平衡二叉樹的特點,所有功能的平攤時間複雜度都是O(log n),所以在資料結構中用得非常爽,代碼又是可以接受的複雜度和長度。而且每個結點也可以是區間,可以代替線段樹,你說爽不爽。伸展樹最大的優點是分裂和合并都是log n,所以比別的當然很快,在處理很多問題上自然有很多優點。從此,愛上伸展樹……
模板一:
這個伸展樹的結點是用資料寫的,是參照某個部落格的,比較方便,不過有時候有點慢,因為是數組,所以debug不出來的幾率比較高。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<cmath>#include<bitset>#define mem(a,b) memset(a,b,sizeof(a))#define INF 1000000007#define maxn 100010using namespace std;typedef long long ll;typedef unsigned long long ull;struct splaytree //伸展樹{ int pre[maxn];//前驅 int key[maxn];//索引值 int ch[maxn][2];//左右孩子,0為左孩子,1為右孩子 int root,tot;//根節點,節點數量 splaytree() { tot=root=0; mem(ch,0); mem(pre,0); mem(key,0); } void newnode(int &r,int father,int k) {//在r位置,父親為father,建立一個值點。 r=++tot; pre[r]=father; key[r]=k; ch[r][0]=ch[r][1]=0; } void rotate(int x,int kind)//kind=1表示右旋,kind=0表示左旋 { int y=pre[x]; ch[y][!kind]=ch[x][kind]; pre[ch[x][kind]]=y; ch[x][kind]=y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; pre[x]=pre[y]; pre[y]=x; } void splay(int x,int goal) //把x伸展到目標位置 { while(pre[x]!=goal) { if(pre[pre[x]]==goal) rotate(x,ch[pre[x]][0]==x); else { int y=pre[x]; int kind=ch[pre[y]][0]==y; if(ch[y][kind]==x) rotate(x,!kind),rotate(x,kind); else rotate(y,kind),rotate(x,kind); } } if(!goal) root=x;//把root更新成x } int insert(int x)//插入值x { int r=root; while(ch[r][key[r]<x]) { if(key[r]==x) {splay(r,0);return 0;} r=ch[r][key[r]<x]; } newnode(ch[r][x>key[r]],r,x); splay(ch[r][x>key[r]],0); return 1; } int find(int x)//尋找索引值為x的結點編號 { int r=root; if(!r) return -INF;//樹未建,未找到 if(key[r]==x) return r; while(ch[r][x>key[r]]) { r=ch[r][x>key[r]]; if(key[r]==x) return r; } return -INF;//未找到 } int get_pre(int x)//尋找節點x的前驅的值,未找到則返回INF { int r=ch[x][0]; if(!r) return -INF; while(ch[r][1]) r=ch[r][1]; return key[r]; } int get_next(int x) { int r=ch[x][1]; if(!r) return INF; while(ch[r][0]) r=ch[r][0]; return key[r]; }};int main(){ int sum=0,a,n; splaytree tree; cin>>n; for(int i=0;i<n;i++) { scanf("%d",&a); if(!i) { sum+=a; tree.newnode(tree.root,0,a); continue; } if(!tree.insert(a)) continue; //因為既然前面已經有a,那這天的相減為0,所以下面就不用繼續了 int x=tree.get_pre(tree.root);//因為insert之後根root即為當前a的 int y=tree.get_next(tree.root);//故可從根尋找 sum+=min(a-x,y-a); } cout<<sum<<endl; return 0;}
模板二:結構體結點
這個是參照百度百科上的代碼寫的,剛開始寫的時候運行這題的資料出錯,然後debug了好久,還以為代碼中的splay和旋轉錯了,都已經放棄然後去找別的模板了。但是後面還是輸出其中過程的資料才知道錯哪了。因為和數組的模板不一樣,所以是自己傳參的時候傳錯了,本來是傳結點編號的,我傳成值了。
為什麼上面的模板不用,又找了這個呢。是因為我用上面的模板寫一道題的時候嚴重逾時,然後改又改不了了,然後自己又找了這個模板,比較好理解。數組類的模板寫法太飄逸了,咱等平常人一時難以接受,自己寫的刪除結點函數又不太會,所以只能暫時放棄數組的模板了。用下面這個比較好理解,又好寫,雖然代碼是長了點。
這個模板不足之處就是insert的時候沒有判斷是否已經存在本值,size就直接加了。這個在有些題面前改的東西就多了,上面數組模板就有優勢了。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<cmath>#include<bitset>#define mem(a,b) memset(a,b,Sizeof(a))#define INF 1000000007#define maxn 100010using namespace std;typedef long long ll;typedef unsigned long long ull;struct splayNode{ int l, r, fa, val, size;};struct splaytree{ splayNode t[maxn]; int root, tot; void create() { root = 1, tot = 2; t[1].val = -INF; t[2].val = INF; t[1].r = t[1].size = 2; t[2].fa = t[2].size = 1; } void dfs(int x) { if(x) { dfs(t[x].l); printf("結點%2d: 左兒子 %2d 右兒子 %2d 父結點 %2d 索引值為:%2d\n",x,t[x].l,t[x].r,t[x].fa,t[x].val); dfs(t[x].r); } } void debug() { printf("根為:%d\n",root); dfs(root); } void update(int x) { t[x].size=t[t[x].l].size+t[t[x].r].size+1; } void left(int x) { int fa = t[x].fa; t[x].fa = t[fa].fa; if (t[t[fa].fa].l==fa) t[t[fa].fa].l=x; if (t[t[fa].fa].r==fa) t[t[fa].fa].r=x; t[fa].fa = x; t[fa].r = t[x].l; t[t[x].l].fa = fa; t[x].l = fa; update(fa); } void right(int x) { int fa = t[x].fa; t[x].fa = t[fa].fa; if (t[t[fa].fa].l==fa) t[t[fa].fa].l=x; if (t[t[fa].fa].r==fa) t[t[fa].fa].r=x; t[fa].fa = x; t[fa].l = t[x].r; t[t[x].r].fa = fa; t[x].r = fa; update(fa); } void splay(int x, int FA = 0) { while (t[x].fa != FA) { int fa = t[x].fa; if (t[fa].fa == FA) { if (t[fa].l==x) right(x); else left(x); } else if (t[t[fa].fa].l==fa) { if(t[fa].l==x) right(fa),right(x); else left(x), right(x); } else if (t[fa].l==x) right(x),left(x); else left(fa), left(x); } update(x); if (!FA) root = x; } int lower_bound(int val) { int ans = 0, la = 0; for (int x = root; x;) { la = x; if (t[x].val>=val) ans=x,x=t[x].l; else x = t[x].r; } splay(la); return ans; } void insert(int val) { for (int x = root;;) { ++t[x].size; if (t[x].val >= val) { if (t[x].l) x = t[x].l; else { t[x].l = ++tot; t[tot].size = 1; t[tot].fa = x; t[tot].val = val; splay(tot); return; } } else if (t[x].r) x = t[x].r; else { t[x].r = ++tot; t[tot].size = 1; t[tot].fa = x; t[tot].val = val; splay(tot); return; } } } int get_lower(int a) { splay(a); for (a = t[a].l; t[a].r; a = t[a].r); return t[a].val; } int get_upper(int a) { splay(a); for (a = t[a].r; t[a].l; a = t[a].l); return t[a].val; } int get_rank(int a) { splay(a); return t[t[a].l].size; } void del(int l, int r) { l = get_lower(l); r = get_upper(r); splay(l); splay(r, l); t[r].l = 0; update(r); update(l); } int get_kth(int k) { ++k; for (int x=root;;) { if (t[t[x].l].size==k-1) return x; if (t[t[x].l].size>=k) x=t[x].l; else k-=t[t[x].l].size+1,x=t[x].r; } }}tree;map<int,int>Map;int main(){ int n,a,sum=0; tree.create(); cin>>n; for(int i=0;i<n;i++) { scanf("%d",&a); //tree.debug(); if(!i) sum+=a,tree.insert(a),Map[a]=1; else { if(Map[a]) continue; Map[a]=1; tree.insert(a); //tree.debug(); int x=tree.get_lower(tree.root); int y=tree.get_upper(tree.root); //cout<<"+++"<<x<<' '<<y<<endl; sum+=min(a-x,y-a); } } cout<<sum<<endl; return 0;}