标签:bool amp ted als 就是 should another ant eof
You have a card deck of n cards, numbered from top to bottom, i. e. the top card has index 1and bottom card — index n. Each card has its color: the ??i-th card has color ai.
You should process q queries. The j-th query is described by integer tj. For each query you should:
Input
The first line contains two integers n and q (2≤n≤3?10^5; 1≤q≤3?10^5) — the number of cards in the deck and the number of queries.
The second line contains n integers a1,a2,…,an (1≤ai≤50) — the colors of cards.
The third line contains q integers t1,t2,…,tq (1≤tj≤50) — the query colors. It‘s guaranteed that queries ask only colors that are present in the deck.
Output
Print q integers — the answers for each query.
Example
input
Copy
7 5
2 1 1 4 3 3 1
3 2 1 1 4
output
Copy
5 2 3 1 5
假数据结构真思维/暴力。不要被3e5吓到,注意到ai的范围其实很小,同时又注意到对于每类颜色,每次操作的永远都是第一个数(每类颜色位置的相对性是不会改变的)。因此我们可以直接暴力,一开始如果一个颜色第一次被询问,可能需要遍历整个数组(当然平摊到每个颜色上肯定达不到),但是当所有颜色都被询问过后实际上每次遍历的就是前50个数左右了。
#include <bits/stdc++.h>
using namespace std;
int n, q, a[300005];
bool vis[55];
void move(int pos) {
int tmp = a[pos];
for(int i = pos - 1; i >= 1; i--) a[i + 1] = a[i];
a[1] = tmp;
}
int main() {
//对于每个数,在最前面的永远在最前面
ios::sync_with_stdio(false);
memset(vis, 0, sizeof(vis));
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= q; i++) {
int nq;
cin >> nq;
int pos = 0;
for(int i = 1; i <= n; i++) {
if(a[i] == nq) {
pos = i;
break;
}
}
cout << pos << ‘ ‘;
move(pos);
}
return 0;
}
Educational Codeforces Round 107 (Rated for Div. 2) C. Yet Another Card Deck(暴力/思维)
标签:bool amp ted als 就是 should another ant eof
原文地址:https://www.cnblogs.com/lipoicyclic/p/14657408.html