C. Yet Another Card Deck
题目概述:
一个长度为
n
n
n 的队列,每个元素有一个颜色,现在给出
q
q
q 个询问。每次询问一个颜色,输出队列中为该颜色的元素的最小下标,然后把这个元素删除,再将这个元素放入放入队头。
思路:
简单的一个
l
i
s
t
list
list 的操作
一个List 容器,从
l
i
s
t
list
list 开头开始找,找到的第一个元素就是下标最小,然后
p
u
s
h
f
r
o
n
t
(
)
push_front()
pushfront() 即可,
要注意不能使用
r
e
m
o
v
e
(
k
)
remove(k)
remove(k) 函数 该函数会删除所有等于
k
k
k 的
l
i
s
t
list
list 中的的元素, 应当使用
e
a
r
s
e
(
)
earse()
earse() 删除指定位置元素。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("D:/Cpp/program/Test.in", "r", stdin);
freopen("D:/Cpp/program/Test.out", "w", stdout);
#endif
int n, q;
cin >> n >> q;
list<int> lt;
for(int i = 1; i <= n; i ++) {
int a;
cin >> a;
lt.emplace_back(a);
}
while(q --) {
int color;
cin >> color;
int cnt = 0;
list<int>::iterator it;
for(it = lt.begin(); it != lt.end(); it ++) {
cnt ++;
if(*it == color) {
lt.erase(it);
lt.emplace_front(color);
cout << cnt << ' ';
break;
}
}
}
cout << '\n';
return 0;
}