CF1511C Yet Another Card Deck

Posted 诸葛阵御

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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, \\underline3, 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, \\underline2, 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, \\underline1, 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 [\\underline1, 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, \\underline4, 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;

以上是关于CF1511C Yet Another Card Deck的主要内容,如果未能解决你的问题,请参考以下文章

CF1511C Yet Another Card Deck

CF1511C Yet Another Card Deck

CF-1359 D. Yet Another Yet Another Task ST表+单调队列

CF-1359 D. Yet Another Yet Another Task ST表+单调队列

CF868F Yet Another Minimization Problem

codeforces CF903G Yet Another Maxflow Problem 线段树