HDU - 1890 Robotic Sort(Splay-区间翻转+删除根节点)
題目鏈接:點擊查看
題目大意:給出一個序列,初始時是亂序的,要求根據規則排序,規則是,遍歷每一個點 i ,使得區間 [ i , a[ i ] ] 內的數翻轉,a[ i ] 表示的是數列中第 i 大的數,可以看出每次翻轉都會使得第 i 大的數字到達第 i 個位置上去,題目要求輸出每次第 i 大的數的位置
題目分析:一個題拖了好久。。說白了還是太懶了,一道比較經典的伸展樹進階題,做完之后還是收獲了不少東西的,首先分析題意,因為每次翻轉之后,第 i 個數在后續就沒有任何作用了,所以每次我們可以直接在翻轉后刪掉第 i 個數,而且初始時給出的數值我們可以轉換為相對大小,我們需要的只是其相對位置,所以可以排序,這樣就可以獲得相對位置了,也就是第 i 大的數的初始下標編號,從而在建樹的時候,我們可以直接按照區間的下標建樹,這樣做的好處是,在splay中編號為 i 的結點對應的值就是 i ,方便后續的修改等操作,對于每一次操作,每次把第 i 個位置下標的結點旋轉到根,我們就可以直接將結點 i 旋轉到根,每次操作后splay的中序遍歷就是當前維護的數列了(其實每個 a[ i ] 在這里是第 i 大的數的位置,可能有點繞,但盡量去理解吧),因為我們這個題目主要是圍繞著相對大小展開的,所以答案也就取決于當前 a[ i ] 左邊有多少個數字,也就是?i + sum[ ch[ root ][ 0 ] ] 了,注意這里是 a[ i ] 左邊有多少個數字在 a[ i ] 的左邊,而不是比 a[ i ] 小的有多少個數字,這里一定要理解,因為翻轉之后區間的絕對大小關系是比較混亂的,但是相對大小關系是比較明確的,查詢完后記得翻轉+刪除
到這里可能只是知道了如何解決這個題,下面的實現才是真正的難點,簡單總結一下這個題目需要讓我們完成的操作就是,循環?n 次,每次找到第 i 大的數,輸出 i + sum[ ch[ root ][ 0 ] ] ,翻轉根的左子樹,刪除根
首先找到第 i 大的數并不難,因為我們上述的相對關系的轉換,第 i 大的數,其位置也就是我們排序后獲得的 pos 了,這里如果不懂先看代碼理解一下吧,總之就是可以直接獲得 pos 的位置,不需要再用find或rank函數尋找,其次直接輸出答案,而對于翻轉而言,因為之前在洛谷做的那個文藝平衡樹,是對于 m 次翻轉后,一次更新得到答案,那個題對于pushdown的要求比較低,隨便水水就過去了,但這個題不一樣,這個題對于每一次操作,都必須要實時更新,不能拖到最后一次更新,這就意味著每次向上rotate或向下find時,都必須要時刻跟著pushdown,不然會因為沒有及時更新導致答案錯誤,對于pushdown的位置,我暫且是先記住的:
find/rank里的pushdown并不難想,因為向下走那么肯定得pushdown啊,但splay和rotate里的pushdown我還沒琢磨清楚,先背過吧,而對于根的刪除,也是有技巧的,我們分為兩種情況:
當然,上述操作對于右子樹同樣成立,看個人喜好吧
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;class Splay//存儲規則:小左大右,重復節點記錄 {#define root e[0].ch[1] //該樹的根節點private:class node{public:int lazy;int v,father;//節點值,父級節點 int ch[2];//左孩子=0,右孩子=1int sum;//自己+自己下級有多少節點。在根節點為1。int recy;//記錄自己被重復了幾次};node e[N];//Splay樹主體int n,points;//使用存儲數,元素數void update(int x){e[x].sum=e[e[x].ch[0]].sum+e[e[x].ch[1]].sum+e[x].recy;}int identify(int x){return e[e[x].father].ch[0]==x?0:1;}void connect(int x,int f,int son)//連接函數。用法:connect(son,father,1/0){e[x].father=f;e[f].ch[son]=x;}//作用:使得x的father=f,f的son=x.void rotate(int x){int y=e[x].father;pushdown(y);pushdown(x);int mroot=e[y].father;int mrootson=identify(y);int yson=identify(x);int B=e[x].ch[yson^1];connect(B,y,yson);connect(y,x,(yson^1));connect(x,mroot,mrootson);update(y);update(x);}void splay(int at,int to)//將at位置的節點移動到to位置{pushdown(at);to=e[to].father;while(e[at].father!=to){int up=e[at].father;int ff=e[up].father;pushdown(ff);pushdown(up);pushdown(at);if(e[up].father==to) rotate(at);else if(identify(up)==identify(at)){rotate(up);rotate(at);}else{rotate(at);rotate(at);}}}int crepoint(int v,int father){n++;e[n].lazy=0;e[n].v=v;e[n].father=father;e[n].sum=e[n].recy=1;e[n].ch[0]=e[n].ch[1]=0;return n;}void destroy(int x)//pop后摧毀節點 {e[x].v=e[x].ch[0]=e[x].ch[1]=e[x].sum=e[x].father=e[x].recy=0;if(x==n) n--;//最大限度優化}public:void init(){points=n=root=0;e[root].v=e[root].father=e[root].sum=e[root].recy=e[root].ch[0]=e[root].ch[1]=0;}int getroot(){return root;}int find(int v)//用于外部的find調用{int now=root;while(true){if(e[now].v==v){splay(now,root);return now;}int next=v<e[now].v?0:1;if(!e[now].ch[next]) return 0;now=e[now].ch[next];}}int build(int v)//內部調用的插入函數,沒有splay {points++;if(points==1)//特判無點狀態 {root=n+1;crepoint(v,0);}else{int now=root;while(true)//向下找到一個空節點 {e[now].sum++;//自己的下級肯定增加了一個節點 if(v==e[now].v){e[now].recy++;return now;}int next=v<e[now].v?0:1;if(!e[now].ch[next]){crepoint(v,now);e[now].ch[next]=n;return n;}now=e[now].ch[next];}}return 0;}void push(int v)//插入元素時,先添加節點,再進行伸展 {int add=build(v);splay(add,root);}void pop(int v)//刪除節點 {int deal=find(v);if(!deal) return;points--;if(e[deal].recy>1){e[deal].recy--;e[deal].sum--;return;}if(!e[deal].ch[0]){root=e[deal].ch[1];e[root].father=0;}else{int lef=e[deal].ch[0];while(e[lef].ch[1]) lef=e[lef].ch[1];splay(lef,e[deal].ch[0]);int rig=e[deal].ch[1];connect(rig,lef,1);connect(lef,0,1);update(lef);}destroy(deal);}int rank(int v)//獲取值為v的元素在這棵樹里是第幾小 {int ans=0,now=root;while(true){if(e[now].v==v) {ans+=e[e[now].ch[0]].sum;splay(now,root);return ans+1;}if(now==0) return 0;if(v<e[now].v) now=e[now].ch[0];else{ans=ans+e[e[now].ch[0]].sum+e[now].recy;now=e[now].ch[1];}}return 0;}int atrank(int x,int rt)//獲取第x小的元素的值 {if(x>points) return -inf;int now=rt;while(true){pushdown(now);int minused=e[now].sum-e[e[now].ch[1]].sum;if(x>e[e[now].ch[0]].sum&&x<=minused) break;if(x<minused) now=e[now].ch[0];else{x=x-minused;now=e[now].ch[1];}}splay(now,root);return e[now].v;}int upper(int v)//尋找該值對應的一個最近的上界值 {int now=root;int result=inf;while(now){if(e[now].v>=v&&e[now].v<result) result=e[now].v;if(v<e[now].v) now=e[now].ch[0];else now=e[now].ch[1];}return result;}int lower(int v)//尋找該值對應的一個最近的下界值 {int now=root;int result=-inf;while(now){if(e[now].v<=v&&e[now].v>result) result=e[now].v;if(v>e[now].v) now=e[now].ch[1];else now=e[now].ch[0];}return result;}void reverse(int pos,int i){splay(pos,root);//把第pos小的點旋到根e[e[root].ch[0]].lazy^=1;printf("%d ",i+e[e[root].ch[0]].sum);remove();}void pushdown(int k){if(k&&e[k].lazy){swap(e[k].ch[0],e[k].ch[1]);e[e[k].ch[0]].lazy^=1;e[e[k].ch[1]].lazy^=1;e[k].lazy=0;}}void remove(){int t=root;if(e[root].ch[1]){atrank(1,e[root].ch[1]);e[root].ch[0]=e[t].ch[0];if(e[t].ch[0]) e[e[t].ch[0]].father=root;}elseroot=e[root].ch[0];e[root].father=0;update(root);}#undef root }tree;struct Node {int pos,val;bool operator<(const Node& a)const{if(val!=a.val)return val<a.val;return pos<a.pos;} }a[N];int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;while(scanf("%d",&n)!=EOF&&n){tree.init();for(int i=1;i<=n;i++){scanf("%d",&a[i].val);tree.push(i);a[i].pos=i;}sort(a+1,a+1+n);for(int i=1;i<n;i++){int pos=a[i].pos;tree.reverse(pos,i);}printf("%d\n",n);}return 0; }?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的HDU - 1890 Robotic Sort(Splay-区间翻转+删除根节点)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1324F M
- 下一篇: CodeForces - 1325D E