2528: 【普及/提高-】【P1160】队列安排

内存限制:256 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:19 解决:3

题目描述

一个学校里老师要将班上 lns="http://www.w3.org/1998/Math/MathML"> 个同学排成一列,同学被编号为 lns="http://www.w3.org/1998/Math/MathML">1,他采取如下的方法:

  1. 先将 lns="http://www.w3.org/1998/Math/MathML">1 号同学安排进队列,这时队列中只有他一个人;

  2. lns="http://www.w3.org/1998/Math/MathML">2 号同学依次入列,编号为 lns="http://www.w3.org/1998/Math/MathML"> 的同学入列方式为:老师指定编号为 lns="http://www.w3.org/1998/Math/MathML"> 的同学站在编号为 lns="http://www.w3.org/1998/Math/MathML">1(1) 中某位同学(即之前已经入列的同学)的左边或右边;

  3. 从队列中去掉 lns="http://www.w3.org/1998/Math/MathML"> 个同学,其他同学位置顺序不变。

在所有同学按照上述方法队列排列完毕后,老师想知道从左到右所有同学的编号。

输入

第一行一个整数 lns="http://www.w3.org/1998/Math/MathML">,表示了有 lns="http://www.w3.org/1998/Math/MathML"> 个同学。

第 lns="http://www.w3.org/1998/Math/MathML">2 行,第 lns="http://www.w3.org/1998/Math/MathML"> 行包含两个整数 lns="http://www.w3.org/1998/Math/MathML">,,其中 lns="http://www.w3.org/1998/Math/MathML"> 为小于 lns="http://www.w3.org/1998/Math/MathML"> 的正整数,lns="http://www.w3.org/1998/Math/MathML"> 为 lns="http://www.w3.org/1998/Math/MathML">0 或者 lns="http://www.w3.org/1998/Math/MathML">1。若 lns="http://www.w3.org/1998/Math/MathML"> 为 lns="http://www.w3.org/1998/Math/MathML">0,则表示将 lns="http://www.w3.org/1998/Math/MathML"> 号同学插入到 lns="http://www.w3.org/1998/Math/MathML"> 号同学的左边,lns="http://www.w3.org/1998/Math/MathML"> 为 lns="http://www.w3.org/1998/Math/MathML">1 则表示插入到右边。

第 lns="http://www.w3.org/1998/Math/MathML">+1 行为一个整数 lns="http://www.w3.org/1998/Math/MathML">,表示去掉的同学数目。

接下来 lns="http://www.w3.org/1998/Math/MathML"> 行,每行一个正整数 lns="http://www.w3.org/1998/Math/MathML">,表示将 lns="http://www.w3.org/1998/Math/MathML"> 号同学从队列中移去,如果 lns="http://www.w3.org/1998/Math/MathML"> 号同学已经不在队列中则忽略这一条指令。

输出

一行,包含最多 lns="http://www.w3.org/1998/Math/MathML"> 个空格隔开的整数,表示了队列从左到右所有同学的编号。

样例输入 复制

4
1 0
2 1
1 0
2
3
3

样例输出 复制

2 4 1

提示

【样例解释】

将同学 lns="http://www.w3.org/1998/Math/MathML">2 插入至同学 lns="http://www.w3.org/1998/Math/MathML">1 左边,此时队列为:

2 1

将同学 lns="http://www.w3.org/1998/Math/MathML">3 插入至同学 lns="http://www.w3.org/1998/Math/MathML">2 右边,此时队列为:

2 3 1

将同学 lns="http://www.w3.org/1998/Math/MathML">4 插入至同学 lns="http://www.w3.org/1998/Math/MathML">1 左边,此时队列为:

2 3 4 1

将同学 lns="http://www.w3.org/1998/Math/MathML">3 从队列中移出,此时队列为:

2 4 1

同学 lns="http://www.w3.org/1998/Math/MathML">3 已经不在队列中,忽略最后一条指令

最终队列:

2 4 1

【数据范围】

对于 lns="http://www.w3.org/1998/Math/MathML">20% 的数据,lns="http://www.w3.org/1998/Math/MathML">110

对于 lns="http://www.w3.org/1998/Math/MathML">40% 的数据,lns="http://www.w3.org/1998/Math/MathML">11000

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,lns="http://www.w3.org/1998/Math/MathML">1<105


#include<bits/stdc++.h>
using namespace std;
int n,i,k,p,m,x;
list <int> a;
int main(){
    scanf("%d",&n);
    a.push_front(1);
    list<int>::iterator it;
    for(i = 2;i <= n;i++) {
    	scanf("%d%d",&k,&p);
    	it=find(a.begin(),a.end(),k);
    	if (p==0) {
    		a.insert(it,i);
		}else {
			a.insert(++it,i);
		}
	}
	scanf("%d",&m);
	while(m--) {
		scanf("%d",&x);
		it=find(a.begin(),a.end(),x);
		if(it!=a.end()) {
			a.erase(it);
		}
	}
	for(it = a.begin();it != a.end();it++) {
		printf("%d ",(*it)); 
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k,p,x,m,mark[100005]={0};
list<int> l;
list<int>::iterator it[100005],tmpit;
int main(){
    cin>>n;
    l.push_front(1);
    it[1]=l.begin();
    for(int i=2;i<=n;i++) {
    	cin>>k>>p;
    	if(p==0) {
    		it[i]=l.insert(it[k],i);
		}else{
			if(it[k]==l.end()) {
				l.push_back(i);
				it[i]=l.end();
			}else{
				tmpit=it[k];
				tmpit++;
				it[i]=l.insert(tmpit,i);
			}
		}
	}
	cin>>m;
	for(int i=1;i<=m;i++) {
		cin>>x;
		if(mark[x]==1) continue;
		mark[x]=1;
		l.erase(it[x]);
	}
	for(tmpit=l.begin();tmpit!=l.end();tmpit++) {
		cout<<*tmpit<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct node {
	int key,pre,nxt;
	node(int _key=0,int _pre=0,int _nxt=0) {
		key=_key;pre=_pre;nxt=_nxt; 
	}
};
node s[100005];
int n,m,k,p,x,idx[100005]={0},tot=0,now;

void ins_back(int x,int y) {
	int now=idx[x];
	s[++tot]=node(y,now,s[now].nxt);
	s[s[now].nxt].pre=tot;
	s[now].nxt=tot;
	idx[tot]=tot;
}
void ins_front(int x,int y) {
	int now=idx[x];
	s[++tot]=node(y,s[now].pre,now);
	s[s[now].pre].nxt=tot;
	s[now].pre=tot;
	idx[tot]=tot;
}

void del(int x) {
	int now=idx[x];
	int le=s[now].pre,rt=s[now].nxt;
	s[le].nxt=rt;
	s[rt].pre=le;
	idx[x]=0;
}

int main(){
	cin>>n;
	s[0]=node();
	ins_back(0,1);
	for (int i=2;i<=n;i++) {
		cin>>k>>p;
		p?ins_back(k,i):ins_front(k,i);		
	}
	cin>>m;
	for (int i=1;i<=m;i++) {
		cin>>x;
		if (idx[x]) del(x);
	}
	
	now=s[0].nxt;
	while (now) {
		cout<<s[now].key<<" ";
		now=s[now].nxt;
	}
	return 0;
}