2019中国大学生程序设计竞赛(CCPC)-网络选拔赛-第七题Shuffle Card

1.大赛题目

Shuffle Card
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6695 Accepted Submission(s): 2061

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

2.中文翻译

洗牌。
时间限制:2000/1000毫秒(Java /其他)内存限制:65536/65536 K(Java /其他)
总提交量:6695份接受的提交量:2061份

问题

一副牌由N张牌组成。每一张牌都是不同的,编号从1到N。首先,牌是从1到N排列的。我们按照以下方式完成洗牌过程,在每一个操作中,我们将画一张牌并将其放在第一张牌的位置上,然后重复操作m次。

请在M操作后输出卡的顺序。

输入

输入的第一行包含两个正整数n和m。

输入文件的第二行有n个数字,从1到n的序列。

接下来有m行,每行都有一个正整数si,表示第i个操作提取的卡号。

输出

请在M操作后输出卡的顺序。(每个数字后面应该有一个空格。)

样本输入

5 3
1 2 3 4 5
3
4
3

样本输出

3 4 1 2 5

3.代码案例

public class sixtest {
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		//n=5
		int n = scanner.nextInt();
		//m=3
		int m = scanner.nextInt();
		int A[] = new int[n];
		int out[] = new int[n];
		int B[] = new int[m];
		//输入A
		for(int i = 0;i<A.length;i++) {
			A[i] = scanner.nextInt();
		}
		//输入B
		for(int i = 0;i<B.length;i++) {
			B[i] = scanner.nextInt();
		}
		//数组创造空间之后,默认每一个下标的值为0
		int Ex[] = new int [100010];
		int count = 0;
		//把要抽选的牌按照从后到前的顺序装进数组out中,重复的略过,然后再把剩余的装进之后的数组下标
		for(int i = B.length-1; i >= 0; i--) {
			if(Ex[B[i]]==0) {
				out[count++] = B[i];
				//这里Ex[B[i]]等于几都可以,只要不等于0
				Ex[B[i]] = 1;
			}
		}
		for(int i = 0;i<A.length;i++) {
			if(Ex[A[i]]==0) {
				out[count++] = A[i];
			}
		}
		for(int i = 0;i<out.length;i++) {
			System.out.print(out[i]+" ");
		}
		scanner.close();
	}
}

4.解题思路

像下面这样把要抽取的牌一张一张的放到最前面,后面的依次往后,可以抽相同的牌
->12345
->32145
->43125
->34125
把要抽选的牌按照从后到前的顺序装进数组out中,重复的略过,然后再把剩余的装进剩余的数组下标

4.1代码举例

for(int i = B.length-1; i >= 0; i--) {
	if(Ex[B[i]]==0) {
		out[count++] = B[i];
		//这里Ex[B[i]]等于几都可以,只要不等于0
		Ex[B[i]] = 1;
	}
}

Ex[3] = 0->out[1] = 3->Ex[3] = 1
Ex[4] = 0->out[2] = 4->Ex[4] = 1
Ex[3] = 1 ->结束循环

for(int i = 0;i<A.length;i++) {
	if(Ex[A[i]]==0) {
		out[count++] = A[i];
	}
}

Ex[1] = 0->out[3] = 1
Ex[2] = 0->out[4] = 2
Ex[3] = 1
Ex[4] = 1
Ex[5] = 0->out[5] = 5

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁同学与Android

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值