Shuffle Card
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 553 Accepted Submission(s): 275
Problem Description

A deck of card consists of n cards. Each card is different, numbered from 1 to n. At first, the cards were ordered from 1 to n. We complete the shuffle process in the following way, In each operation, we will draw a card and put it in the position of the first card, and repeat this operation for m times.

Please output the order of cards after m operations.

Input

The first line of input contains two positive integers n and m.(1<=n,m<=105)

The second line of the input file has n Numbers, a sequence of 1 through n.

Next there are m rows, each of which has a positive integer si, representing the card number extracted by the i-th operation.

Output

Please output the order of cards after m operations. (There should be one space after each number.)

Sample Input

5 3
1 2 3 4 5
3
4
3

Sample Output

3 4 1 2 5
Source

2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛
题意:每次从牌堆里抽取一张牌,放到最顶上,问最后的排序情况。
回忆:记得类似的题 大一时实验室学长给出过,但不知道什么是栈。
思路:用栈来解决,后进先出,做好标记即可.
代码如下

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
stack<int>s;
int a[100005];
bool vis[100005];
int main() {
int n, m, k;
while(cin >> n >> m) {
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = n; i >= 1; i--)
s.push(a[i]);
while(m--) {
cin >> k;
s.push(k);
}
memset(vis, 0, sizeof(vis));
while(!s.empty()) {
k = s.top();
s.pop();
if(!vis[k]) {
cout << k << ' ';
vis[k] = 1;
}
}
}
return 0;
}