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.
(防止读者不明,添上注解
一副牌由n张牌组成。每张卡片都是不同的,编号从1到n。起初,卡片被命令从1到n。我们在以下方式完成洗牌过程,在每一个操作,我们将画一张卡片,把它放在第一张牌的位置,并对m乘以重复此操作。
请在m次操作后输出卡的顺序。)
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
思路
看似很复杂,其实捋一捋思路就能看出,最开始给出一个排列,然后m次操作,把a移动到最前面,求最后的排列。模拟栈,每次把给出的数放到栈顶,输出的时候记录一下如果已经输出过就不用再输出。比较坑的就是末尾不能输出换,否则PE。
#include <stdio.h>
#include <string.h>
#include <stack>
const int N = 1e5+10;
using namespace std;
bool book[N];
int a[N];
stack<int>s;
int main()
{
int n, m, u;
while(scanf("%d%d", &n, &m)!=EOF)
{
for(int i=1; i<=n; i++)
scanf("%d", &a[i]);
for(int i=n; i>=1; i--)
s.push(a[i]);
while(m--)
{
scanf("%d", &u);
s.push(u);
}
memset(book, false, sizeof(book));
while(!s.empty())
{
u=s.top();
s.pop();
if(book[u]==false)
{
printf("%d ", u);
book[u]=true;
}
}
}
return 0;
}