CCPC:Shuffle Card(栈模拟)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值