Shuffle Card
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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
题意:给你n张卡片,和q个要执行操作的序号;每张卡片都有一个序号1,2…n;操作:将序号为 i 的卡片放到第一位;问你q次操作后这一叠卡片的顺序;
做法:例如n = 3;初始顺序是 2 1 3;q = 3;分别是3 3 3;
那么只要执行最后一个3操作就好了,因为前2个操作相当于多余了;
所以,把 q 个操作序号 出现超过1次的,把前面重复的删掉,只保留最后一个;
然后答案就是:从后往前输出 q个操作序号,然后按顺序输出 初始序列中没有被执行操作的卡片序号
AC_Code
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5;
int a[maxn+10],q[maxn+10];
struct node {
bool used;
int preindex;
} use[maxn+10];
bool flagq[maxn+10];
int main() {
int n,qq;
cin >> n >> qq;
int i,j,k;
for(i = 0; i < n; i++) {
cin >> a[i];
use[a[i]].used = false;
}
memset(flagq,false,sizeof(flagq));
for(i = 0; i < qq; i++) {
cin >> q[i];
if(use[q[i]].used) { //yong le
flagq[use[q[i]].preindex] = false;
flagq[i] = true;
use[q[i]].preindex = i;
} else { //meiyong guo
use[q[i]].used = true;
flagq[i] = true;
use[q[i]].preindex = i;
}
}
//print()
bool title = true;
for(j = qq-1; j >= 0; j--) {
if(flagq[j]) {
if(title) {
cout << q[j];
title = false;
} else {
cout << " " << q[j];
}
}
}
for(k = 0; k < n; k++) {
if(!use[a[k]].used) {
if(title){
cout << a[k];
}else{
cout << " " << a[k];
}
}
}
cout << endl;
}