码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 107 (Rated for Div. 2) C. Yet Another Card Deck(暴力/思维)

时间:2021-04-15 12:03:00      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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:

  • find the highest card in the deck with color tj, i. e. the card with minimum index;
  • print the position of the card you found;
  • take the card and place it on top of the deck.

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!