1279C. Stack of Presents

Stack of Presents

维护最深深度即可
比如先拿一个第5层的,这时最深深度就是5,接下来如果拿第4层的就可以认为它刚刚已经被移到了最上面,如果要拿第7层的,就需要把上面的移出,然后更新深度

Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, mid, rt << 1
#define rrt mid + 1, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
    t = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        t = t * 10 + ch - '0';
        ch = getchar();
    }
    t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x7f7f7f7f;
const double eps = 1e-5;
const double Pi = acos(-1);
const int maxn = 1e5 + 5;

map<int, int> mp;
int an[maxn];

int main(void) {
    int T;
    read(T);
    while (T--) {
        mp.clear();
        int n, m;
        read(n), read(m);
        for (int i = 1; i <= n; i++) {
            int x;
            read(x);
            mp[x] = i;
        }
        for (int i = 1; i <= m; i++)
            read(an[i]);
        ll ans = 0;
        int maxx = 1;
        for (int i = 1; i <= m; i++) {
            if (mp[an[i]] > maxx) {
                ans += (ll)(mp[an[i]] - i) * 2 + 1;
                maxx = mp[an[i]];
            } else 
                ans++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值