HDU 6707 Shuffle Card

Shuffle Card

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

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

题意:

刚开始题目给你1-n的有序序列,然后将一张牌放在牌首,重复m次,问最后的序列。

思路:

实际上比如说3,4,3那么就是将3放在首位,然后放4,然后放3,这样的话就会出现最后3之前的有多少个3都是没有用的,所以只要从后往前遍历输出就行了。

#include 
#include 
using namespace std;
const int maxn = 1e+5 + 10;
int main() {
    int n, m, cnt = 0, x;
    int a[maxn] = {0};
    bool book[maxn] = {false};
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", &x);
    for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
    for (int i = m; i >= 1; i--) {
        if (book[a[i]] == false) printf("%d ", a[i]);
        book[a[i]] = true;
    }
    for (int i = 1; i <= n; i++) {
        if (book[i] == false) printf("%d ", i);
    }
    return 0;
}

你可能感兴趣的:(HDU)