刷题计划——水题

C. Yet Another Card Deck

题目概述:
一个长度为 n n n 的队列,每个元素有一个颜色,现在给出 q q q 个询问。每次询问一个颜色,输出队列中为该颜色的元素的最小下标,然后把这个元素删除,再将这个元素放入放入队头。

思路:
简单的一个 l i s t list list 的操作
一个List 容器,从 l i s t list list 开头开始找,找到的第一个元素就是下标最小,然后 p u s h f r o n t ( ) push_front() pushfront() 即可,
要注意不能使用 r e m o v e ( k ) remove(k) remove(k) 函数 该函数会删除所有等于 k k k l i s t list list 中的的元素, 应当使用 e a r s e ( ) earse() earse() 删除指定位置元素。
代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10;

int main() {
   ios::sync_with_stdio(false); 
   cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
   freopen("D:/Cpp/program/Test.in", "r", stdin);
   freopen("D:/Cpp/program/Test.out", "w", stdout);
#endif
    int n, q;
    cin >> n >> q;
    list<int> lt;
    for(int i = 1; i <= n; i ++) {
        int a;
        cin >> a;
        lt.emplace_back(a);
    }

    while(q --) {
        int color;
        cin >> color;
        int cnt = 0;
        list<int>::iterator it;
        for(it = lt.begin(); it != lt.end(); it ++) {
            cnt ++;
            if(*it == color) {
                lt.erase(it);
                lt.emplace_front(color);
                cout << cnt << ' ';
                break;
            }
        }
    }
    cout << '\n';
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值