有n个软件,以及软件相应的编号和位置。 一个屏幕可以存放t个软件。每次滑动屏幕,就是一个动作,点击软件,是一个动作。 每次点开一个软件,就会回到初始的屏幕。点开的软件,会和它位置的前一个互换。 给你软件的编号和相应位置,以及点开软件的顺序,问总共要做多少次动作。
模拟题,题面长,炸int== 一个数组记录软件当前位置,一个数组记录软件序列。 每次读入一个软件,就先询问其位置,然后交换,求和即可。
#if __cplusplus >= 201103L #pragma comment(linker, "/STACK:102400000,102400000") #pragma GCC optimize(3, "Ofast", "inline") #include <bits/stdc++.h> #else #include <algorithm> #include <bitset> #include <cmath> #include <iostream> #include <string.h> #include <vector> #endif using namespace std; #define inf __INT_MAX__ #define enf INT_MIN #define INF LLONG_MAX #define ENF LLONG_MIN const int MAXN = 6e5 + 10; const double pi = acos(-1.0); const double eps = 1e-12; typedef long long ll; typedef unsigned long long ull; #define zhengfu(x) ((x > eps) - (x < -eps)) int pos[MAXN], a[MAXN]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) { cin >> a[i]; pos[a[i]] = i; } ll sum = 0; while (m--) { int tmp; cin >> tmp; int pos1 = pos[tmp]; int x = pos1 / k + 1, pos2 = pos1 - 1; sum += x; if (pos2 == -1) continue; swap(a[pos1], a[pos2]); swap(pos[a[pos1]], pos[a[pos2]]); } cout << sum << endl; }