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
题目意思,它给出的牌是让你把那张牌取到队首(**这里不是第几张牌,而是特指那一张**),可用栈来操作,从后往前操作,要是遇到相同的以最后一次操作为主。
代码:
#include <stdio.h>
#include <stack>
#include <string.h>
using namespace std;
stack<int>s;
int main()
{
int n,m,a[100001],i,k;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=n;i>=1;i--)
{
s.push(a[i]);
}
while(m--)
{
scanf("%d",&k);
s.push(k);
}
while(!s.empty())
{
k=s.top();
s.pop();
if(a[k]!=0)
{
printf("%d",k);
a[k]=0;
}
}
return 0;
}