Educational Codeforces Round 107 (Rated for Div. 2)
C. Yet Another Card Deck
题目大意:
就是先给你一个序列,然后多次询问,每次询问就把那个数处于序列的位置输出,并把那个数字提到最前面。
简单的模拟。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;
int a[maxn];
signed main() {
ios::sync_with_stdio(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (!a[x])a[x] = i;
//存下来第一个出现的位置
}
while (q--) {
int x;
cin >> x;
cout << a[x] << ' ';
for (int i = 1; i <= 50; i++) {
if (i == x || !a[i])continue;
//如果这数字之前就不是第一个,之后也用不到,直接换下一个;同样的如果这就是那个询问的数字,也不用次序往后推。
if (a[i] < a[x])a[i]++;
}
a[x] = 1;
//询问那个数字变成第一个。
}
}