HDUOJ 6707 Shuffle Card
HDUOJ 6707 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.
Input
The first line of input contains two positive integers n and m.(1<=n,m<=1e5)
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 3Sample Output
3 4 1 2 5思維題,在線洗牌肯定會 T,因為沒有一個很好的數據結構可以完成此題。我們不妨換一種思維,把洗牌順序記錄下來,不難發現,最后一次洗的牌一定在第一張,倒數第二次在第二張,以此類推,我們標記一下洗過的牌,最后沒洗的保持原順序插入答案數組即可,AC代碼如下:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5; int n,m,a[N],op[N],vis[N]; vector<int>ans; int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&a[i]);}for(int i=1;i<=m;i++) scanf("%d",&op[i]);for(int i=m;i>=1;i--){if(!vis[op[i]]){ans.push_back(op[i]);vis[op[i]]=1;}}for(int i=1;i<=n;i++){if(!vis[a[i]]) ans.push_back(a[i]);}for(auto i:ans) printf("%d ",i); }總結
以上是生活随笔為你收集整理的HDUOJ 6707 Shuffle Card的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: In-class Initializat
- 下一篇: TensorRT报Cuda initia