题目描述
一个学校里老师要将班上 N 个同学排成一列,同学被编号为 1∼N,他采取如下的方法:
-
先将 11 号同学安排进队列,这时队列中只有他一个人;
-
2∼N 号同学依次入列,编号为 i 的同学入列方式为:老师指定编号为 i 的同学站在编号为 1∼(i−1) 中某位同学(即之前已经入列的同学)的左边或右边;
-
从队列中去掉 M 个同学,其他同学位置顺序不变。
在所有同学按照上述方法队列排列完毕后,老师想知道从左到右所有同学的编号。
输入格式
第一行一个整数 N,表示了有 N 个同学。
第 2∼N 行,第 i 行包含两个整数 k,p,其中 k 为小于 i 的正整数,p 为 0 或者 1。若 p 为 0,则表示将 i 号同学插入到 k 号同学的左边,p 为 1 则表示插入到右边。
第 N+1 行为一个整数 M,表示去掉的同学数目。
接下来 M 行,每行一个正整数 x,表示将 x 号同学从队列中移去,如果 x 号同学已经不在队列中则忽略这一条指令。
输出格式
一行,包含最多 N 个空格隔开的整数,表示了队列从左到右所有同学的编号。
输入输出样例
输入
4 1 0 2 1 1 0 2 3 3
输出
2 4 1
说明/提示
【样例解释】
将同学 2 插入至同学 1 左边,此时队列为:
2 1
将同学 3 插入至同学 2 右边,此时队列为:
2 3 1
将同学 4 插入至同学 1 左边,此时队列为:
2 3 4 1
将同学 3 从队列中移出,此时队列为:
2 4 1
同学 3 已经不在队列中,忽略最后一条指令
最终队列:
2 4 1
【数据范围】
对于 20% 的数据,1≤N≤10。
对于 40% 的数据,1≤N≤1000。
对于 100% 的数据,1<M≤N≤105。
用结构体模拟链表
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod=1e9+7;
const int M=4e4+10;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
int minn=0x3f3f3f3f;
int maxn=0xc0c0c0c0;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n,m,k,p;
string s;
struct P{int l,r,del;
}a[N];
void solve()
{cin>>n;a[0].l=1,a[0].r=1;a[1].l=0,a[1].r=0;for(int i=2;i<=n;i++){cin>>k>>p;if(p){a[i].l=k;a[i].r=a[k].r;a[a[k].r].l=i;a[k].r=i;}else{a[i].r=k;a[i].l=a[k].l;a[a[k].l].r=i;a[k].l=i;}}cin>>m;while(m--){int x;cin>>x;a[x].del=1;}for(int i=a[0].r;i;i=a[i].r)if(!a[i].del) cout<<i<<" ";cout<<endl;
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);ll t=1;
// cin>>t;while(t--){ solve();}return 0;
}
用list
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod=1e9+7;
const int M=4e4+10;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
int minn=0x3f3f3f3f;
int maxn=0xc0c0c0c0;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n,m,k,p;
string s;
list<int>::iterator pos[N];
list<int> l;
bool vis[N];
void solve()
{cin>>n;l.push_front(1);pos[1]=l.begin();for(int i=2;i<=n;i++){cin>>k>>p;if(!p)pos[i]=l.insert(pos[k],i);else{auto nextt=next(pos[k]);pos[i]=l.insert(nextt,i);}}cin>>m;while(m--){int x;cin>>x;if(!vis[x])l.erase(pos[x]);vis[x]=true;}for(int x:l)cout<<x<<" ";cout<<endl;
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);ll t=1;
// cin>>t;while(t--){ solve();}return 0;
}