C. Anya and Smartphone (思维?乱搞)

C. Anya and Smartphone
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).

Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t - 1 gestures (if the icon is on the screen t), and then make another gesture — press the icon of the required application exactly once to launch it.

After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.

All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.

Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order?

Note that one application may be launched multiple times.

Input

The first line of the input contains three numbers n, m, k (1 ≤ n, m, k ≤ 105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.

The next line contains n integers, permutation a1, a2, ..., an — the initial order of icons from left to right in the menu (from the first to the last one), ai —  is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among ai.

The third line contains m integers b1, b2, ..., bm(1 ≤ bi ≤ n) — the ids of the launched applications in the planned order. One application may be launched multiple times.

Output

Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.

思路就是利用两个数组 ,一个val[],一个id[],Id数组里存储  id[i]  数字 i当前的位置   val[i] 代表 位置i 的数字

 1 #include <algorithm>
 2 #include <stack>
 3 #include <istream>
 4 #include <stdio.h>
 5 #include <map>
 6 #include <math.h>
 7 #include <vector>
 8 #include <iostream>
 9 #include <queue>
10 #include <string.h>
11 #include <set>
12 #include <cstdio>
13 #define FR(i,n) for(int i=0;i<n;i++)
14 #define MAX 2005
15 #define mkp pair <int,int>
16 using namespace std;
17 #include <bits/stdc++.h>
18 const int maxn = 5e5 + 40;
19 typedef long long ll;
20 const int  inf = 0x3fffff;
21 void read(ll &x) {
22     char ch; bool flag = 0;
23     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
24     for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
25     x *= 1 - 2 * flag;
26 }
27 
28 ll val[maxn],id[maxn];
29 
30 
31 
32 
33 int main() {
34     ll n,m,k;
35     read(n),read(m),read(k);
36     for(int i=0;i<n;i++){
37         read(val[i]);
38         id[val[i]]=i+1;
39     }
40     ll res=0;
41     for(int i=0;i<m;i++){
42         ll pos;
43         read(pos);
44         if(id[pos]==1)res++;
45         else {
46             res+=id[pos]/k;
47             if(id[pos]%k==0)res--;
48             res++;
49             int tmp=id[pos]-1-1;
50             swap(val[tmp],val[tmp+1]);
51             id[pos]=tmp+1;
52             id[val[tmp+1]]=tmp+1+1;
53         }
54     }
55     cout<<res<<endl;
56     return 0;
57 }

猜你喜欢

转载自www.cnblogs.com/DreamKill/p/9452642.html