Test instructions: Given a phone, and then a total of n apps, tell you that each screen up to put K, now you run M app, each time from the first screen began to slide, each run one, it and the previous exchange position, the first will not change, now ask you how many times to slide.
Analysis: This problem, no algorithm, is analog Bai, but to pay attention to the time, can not tle, so we have to put all the positions in advance to save, so that the time to find an O (1), otherwise it will time out, you can use two arrays, you can use map, a deposit number, a storage location,
Then you can swap it out after you run it.
The code is as follows:
#include <iostream> #include <cstdio> #include <map> #include <cmath>using namespace std; typedef long LONG ll;const int MAXN = 1e5 + 5;map<int, int> mp;map<int, int> mps;int main () { int n, m, K, x; while (scanf ("%d%d", &n, &m, &k) = = 3) { for (int i = 0; i < n; ++i) { scanf ("%d", &x); MP[X] = i+1; MPS[I+1] = x; } LL ans = 0; for (int i = 0; i < m; ++i) { scanf ("%d", &x); int t = mp[x]; Ans + = (LL) ceil ((double) t/k); if (t = = 1) continue; Swap (Mp[mps[t]], mp[mps[t-1]); Swap (Mps[t], mps[t-1]); } printf ("%lld\n", ans); } return 0;}
Coderforces 518C Anya and Smartphone (analog)