首页 > 其他分享 >题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]

题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]

时间:2024-07-14 13:52:02浏览次数:7  
标签:index le deck int 题解 Deck CodeForces color card

CodeForces 1511C

C. Yet Another Card Deck

Description

You have a card deck of \(n\) cards, numbered from top to bottom, i. e. the top card has index \(1\) and bottom card — index \(n\). Each card has its color: the \(i\)-th card has color \(a_i\).

You should process \(q\) queries. The \(j\)-th query is described by integer \(t_j\). For each query you should:

  • find the highest card in the deck with color \(t_j\), 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 \le n \le 3 \cdot 10^5\); \(1 \le q \le 3 \cdot 10^5\)) — the number of cards in the deck and the number of queries.

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 50\)) — the colors of cards.

The third line contains \(q\) integers \(t_1, t_2, \dots, t_q\) (\(1 \le t_j \le 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

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

output

5 2 3 1 5 

Note

Description of the sample:

  1. the deck is \([2, 1, 1, 4, \underline{3}, 3, 1]\) and the first card with color \(t_1 = 3\) has position \(5\);
  2. the deck is \([3, \underline{2}, 1, 1, 4, 3, 1]\) and the first card with color \(t_2 = 2\) has position \(2\);
  3. the deck is \([2, 3, \underline{1}, 1, 4, 3, 1]\) and the first card with color \(t_3 = 1\) has position \(3\);
  4. the deck is \([\underline{1}, 2, 3, 1, 4, 3, 1]\) and the first card with color \(t_4 = 1\) has position \(1\);
  5. the deck is \([1, 2, 3, 1, \underline{4}, 3, 1]\) and the first card with color \(t_5 = 4\) has position \(5\).

我的解法

我的做法是:
首先把数组反置(这样的话数组的前面就是原来的后面,现在的后面就是原来的前面)
这样做的话我就可以直接在后面放置数字(实际上是放在数组的前面)
再使用一个数据结构

std::map<int,std::priority_queue<int> >

map的first是存储一种数字,second是存储这种数字对应的下标,默认是从大到小排
应该能把优先队列优化掉的,但是这样做也行(得到下标最大)
然后再放记录一个位置是否为空的前缀和数组,每次移动都维护一次
算位置的时候就计算:移动后位置 - 移动前位置 - 移动前位置所在下标的前缀和数组的内容和末尾内容的差值
就得到这个数字移动前的位置了

我的代码

#include <iostream>
#include <queue>
#include <map>
#include <algorithm>

#define int long long

int t;
const int N = 1e7 + 10;
int a[N];
int kong[N];

void solve()
{
    std::map<int,std::priority_queue<int> > mp;

    int n,q;
    std::cin >> n >> q;

    for(int i = n ; i > 0 ; i --)
    {
        std::cin >> a[i];
        mp[a[i]].push(i);
    }

    int index = n+1;
    while(q--)
    {
        kong[index] = kong[index-1];

        int txt;
        std::cin >> txt;

        int yuan = mp[txt].top();
        mp[txt].pop();
        mp[txt].push(index);
        a[index] = txt;

        int temp = yuan;
        while(temp <= index) kong[temp]++,temp++;

        std::cout << index - yuan - (kong[index] - kong[yuan]) << " ";

        index ++;
    }
}

signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    t = 1;
    //std::cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}

标签:index,le,deck,int,题解,Deck,CodeForces,color,card
From: https://www.cnblogs.com/jiejiejiang2004/p/18301469

相关文章

  • 「ABC217F」Make Pair 的题解
    题目大意一共\(2N\)个学生站成一排,其中有\(M\)对朋友关系。老师每次从队列中挑出两个相邻的学生作为同桌。为了关系和睦,每次选出的两个学生必须是朋友关系。选出的两个学生离开队列,空出来的位置左右合拢。请问老师有多少种方式选完所有学生?对于两种选人的方案,即使同桌关系相......
  • [COCI2015-2016#6] PAROVI 的题解
    题意选择一些\(n\)一下互质的二元组\(\{a,b\}\),求对于任意\(x\in\big[2,n\big]\)都不满足\(a,b<x\)和\(a,b\gex\)的个数。简化题意因为无解的情况只发生在所有的\(\{a,b\}\)之间没有多余的位置用于放置\(x\),所以题意可以抽象成这样:选择一些区间互质的区间\([a......
  • [COCI2006-2007#4] ZBRKA 的题解
    题目大意在一个长度为\(n\)的排列中找出逆序对数量恰好为\(c\)的排列总数,其中\(1\len\le10^3,1\lec\le10^4\)。思路考虑将\(1\)到\(n\)这些数从小到大一次填进去,因为每一次填入的数多是最大的,所以逆序对增加的数量只与其所在的位置相关,所以设计\(f_{i,j}\)表......
  • Snow Walking Robot 的题解
    题目大意给你一个机器人和机器人的\(n\)个运动,要求你在给出的运动路径的基础上设计一种不会走重复的路径的方法,注意只能减少原来的步数而不能增加,其中\(1\len\le10^5\)。思路因为这道题目可以自由的配置路径并且要求机器人在最后回到原来的位置,那么就应该要到一种适合所有......
  • [EGOI2021] Luna likes Love 的题解
    题目大意有\(2\timesn\)个人站成一排,然后给每个人分配一个\(1\)至\(n\)之间的数字,每种数字出现\(2\)次。现在,你可以进行两种操作:删除操作,将数字相同且相邻的两人删除,删除后两端剩下的队列合并。交换操作,交换相邻两个人的位置。每次,问至少操作多少次能够删除所有人......
  • Unusual Minesweeper 题解
    题目大意给你\(n\)个炸弹,第\(i\)个炸弹在\((x_i,y_i)\)的位置,可以将这一行与这一列的距离小于\(k\)的其他所有炸弹引爆,而且连锁的引爆不需要时间。每一秒你可以引爆一个炸弹,其中第\(0\)秒也可以引爆,并且第\(i\)个炸弹在第\(timer_i\)的时候会自己爆炸。要求输出引......
  • [ARC115B] Plus Matrix 的题解
    题目大意给你一个\(n\timesn\)的数组\(C\),\(c_{i,j}=a_i+b_j\),求\(a\)数组与\(b\)数组,不保证有解,其中\(1\len\le500,1\lec_{i,j}\le10^9\),而且\(a_i,b_i\)都是非负整数。\[\begin{bmatrix}a_1+b_1&a_1+b_2&\cdots&a_1+b_{n-1}&a_1+b_n\\a_2+b_......
  • P2188 小Z的 k 紧凑数 题解
    题目传送门前置知识数位DP|记忆化搜索解法基础数位DP,与luoguP2657[SCOI2009]windy数类似,记录当前位置、上一位填的数码,接着记忆化搜索即可。需要注意的是有前导零时,此时不需要管相邻两位数字差的绝对值不超过\(k\)的限制。代码#include<bits/stdc++.h>usingn......
  • SP14887 GOODA - Good Travels 题解
    题目传送门前置知识Tarjan算法|最短路解法缩点后原图就成为了一个有向无环图,此时每个点最多被经过一次,故在求最长路的过程中可以将点权和边权混着转移。上篇题解用拓扑实现查找两点间最长路的做法正确性不会证,遂写了份Dijkstra求最长路。代码#include<bits/stdc++.h......
  • Codeforces 956 Div2
    期末考试结束,开始训练A.ArrayDivisibility----------------------------------题解----------------------------简单的构造题,要让数组a里面的下表为1<=k<=n的数以及下表为(k的因数)的数加起来的和能被K整除,那我们只需要让每一个k的因数都能被k整除就行了,直接让每一个编号i......