题意:不断把规定的牌号移动到第一个去,最后输出移动结果;
这道题,用双端队列+map很容易就可以实现;因为我把原数组的元素不变,只把规定的牌号移动到双端队列的前部,最后输出时先输出队列前面的同时用map记录,然后再输出原数组元素,同时用map判断是否前面出现过,然后这样就不会有重复的了;.
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<sstream>
#include<fstream>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
//ll gcd(ll a,ll b){
// return b?gcd(b,a%b):a;
//}
//ll QP(ll x,ll n){
// ll res=1;
// while(n){
// if(n&1){
// res=(res*x);
// }
// x=(x*x);
// n>>=1;
// }
// return res;
//}
map<int,int> mm;
int n,m;
int a[maxn];
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",a+i);
deque<int> de;
int t;
while(m--){
scanf("%d",&t);
de.push_front(a[t]);//放到头部
}
while(de.size()){
int tt=de.front();de.pop_front();
if(mm[tt]==0){//取出来,并且如果没出现就标记为1
printf("%d ",tt);
mm[tt]=1;
}
}
for(int i=1;i<=n;i++){//输出原数组中元素
if(mm[a[i]]==0){//如果没有出现过就输出
printf("%d ",a[i]);
mm[a[i]]=1;//标记
}
}
return 0;
}