CF1511C Yet Another Card Deck

题目描述

You have a card deck of nn cards, numbered from top to bottom, i. e. the top card has index 11 and bottom card — index nn . Each card has its color: the ii -th card has color a_iai​ .

You should process qq queries. The jj -th query is described by integer t_jtj​ . For each query you should:

  • find the highest card in the deck with color t_jtj​ , 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.

输入格式

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 a.1​,a.2​,…,a.n​ ( 1≤ai​≤50 ) — the colors of cards.

The third line contains q integers t.1​,t.2​,…,t.q​ ( 1≤t.j​≤50 ) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.

输出格式

Print qq integers — the answers for each query.

题意翻译

有 n 张卡片从高到低叠放在桌子上,最上面的编号为 1,最下面的编号为 n。第 i 张卡片的颜色为 a_i。

现在你需要执行 q 次操作,第 i 次操作对应的操作码为 t_i。需要执行三步操作:

  • 找到最高的颜色为 t_i​ 的卡片(即编号最小的)
  • 输出这张卡片当前编号
  • 将其抽出来放到所有卡片的最上面(这一步后有些卡片的编号可能会发生改变)

数据范围见原题面

输入输出样例

输入 #1复制

7 5
2 1 1 4 3 3 1
3 2 1 1 4

输出 #1复制

5 2 3 1 5

说明/提示

Description of the sample:

  1. the deck is [2, 1, 1, 4, \underline{3}, 3, 1][2,1,1,4,3​,3,1] and the first card with color t_1 = 3t1​=3 has position 55 ;
  2. the deck is [3, \underline{2}, 1, 1, 4, 3, 1][3,2​,1,1,4,3,1] and the first card with color t_2 = 2t2​=2 has position 22 ;
  3. the deck is [2, 3, \underline{1}, 1, 4, 3, 1][2,3,1​,1,4,3,1] and the first card with color t_3 = 1t3​=1 has position 33 ;
  4. the deck is [\underline{1}, 2, 3, 1, 4, 3, 1][1​,2,3,1,4,3,1] and the first card with color t_4 = 1t4​=1 has position 11 ;
  5. the deck is [1, 2, 3, 1, \underline{4}, 3, 1][1,2,3,1,4​,3,1] and the first card with color t_5 = 4t5​=4 has position 55 .

思路:

只需按照题q次操作即可

代码

#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
		int n,q;
		cin>>n>>q;
		int a[n+3];
		int t[q+3];
		for (int i=1;i<=n;i++) {
			cin>>a[i];
		}
		for (int i=1;i<=q;i++) {
			cin>>t[i];
		}
		for (int i=1;i<=q;i++) {
			for (int j=1;j<=n;j++) {
				if (t[i]==a[j]) {	//操作1 
					cout<<j;	//操作2 
					for (int m=j;m>=2;m--) {    //将当前卡牌都向后移一位
						a[m]=a[m-1];
					}
					a[1]=t[i];    //置顶卡牌
					break;
				}			
			}
			if (i!=q) {
				cout<<' ';
			}
		}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值