讓敵人成為隊友ARPG《亡靈之劍:亡靈者》公開
阿新 • • 發佈:2022-12-07
P1160 佇列安排
題目簡述
將N個同學依次插入隊伍中,再刪除m個同學,求最終的隊伍順序
思路
這題是個很好的練習連結串列的題目,要注意的是在連結串列的頭和尾要搞一個Head和Tail指標,不然超出邊界會出錯
程式碼
#include<bits/stdc++.h> using namespace std; const int N=1e5+5; int Map[N]; struct Node{ int id; struct Node *head; struct Node *next; }s[N]; int main(){ int n; cin>>n; for(int i=0;i<=n;i++){ s[i].id=i; s[i].head=NULL; s[i].next=NULL; } struct Node *Head=&s[1]; struct Node *Next=&s[1]; for(int i=2;i<=n;i++){ int k,p; cin>>k>>p; switch(p){ case 0:{ struct Node *x=&s[k]; struct Node *y=&s[i]; if((Head->id)==k){ x->head=y; y->next=x; Head=y; } else { y->next=x; y->head=x->head; x->head=y; y->head->next=y; } break; } case 1:{ struct Node *x=&s[k]; struct Node *y=&s[i]; if((Next->id)==k){ x->next=y; y->head=x; Next=y; } else { y->head=x; y->next=x->next; x->next=y; y->next->head=y; } break; } } } int m; cin>>m; while(m--){ int p; cin>>p; if(Map[p])continue; Map[p]=1; if(Head->id==p)Head=Head->next; else { if(Next->id==p){ s[p].head->next=s[p].next; Next=Next->head; } else { s[p].head->next=s[p].next; s[p].next->head=s[p].head; } } } while(Head!=NULL){ cout<<Head->id<<' '; Head=Head->next; } return 0; }