CodeForces 518C Anya and Smartphone

Description

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.

Sample Input

Input

8 3 31 2 3 4 5 6 7 87 8 1

Output

7

Input

5 4 23 1 5 2 44 4 4 4

Output

8

Hint

In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8.

After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures.

After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures.

After application 1 is launched, the arrangement of icons in the menu doesn‘t change. To launch it Anya makes 1 gesture.

In total, Anya makes 7 gestures.

 1 #include<cstring>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<string>
 6
 7 using namespace std;
 8
 9 int n,m,k;
10 int s[100005];
11 int h[100006];
12 int main()
13 {
14     while(scanf("%d%d%d",&n,&m,&k)!=EOF)
15     {
16         for(int i=1;i<=n;i++)
17         {
18             int a;
19             scanf("%d",&a);
20             s[a]=i;
21             h[i]=a;
22         }
23
24         long long sum=0;
25         for(int i=0;i<m;i++)
26         {
27             int a;
28             scanf("%d",&a);
29             sum+=s[a]%k==0?s[a]/k:s[a]/k+1;
30             if(s[a]!=1){
31
32             int num=h[s[a]-1];
33             swap(h[s[a]],h[s[a]-1]);
34             swap(s[a],s[num]);
35
36             }
37         }
38         printf("%I64d\n",sum);
39     }
40 }
时间: 2024-08-09 14:42:46

CodeForces 518C Anya and Smartphone的相关文章

CoderForces 518C Anya and Smartphone (模拟)

题意:给定一个手机,然后一共有 n 个app,告诉你每个屏幕最多放 k 个,现在要你运行 m 个app,每次都从第一个屏幕开始滑动,每运行一个,它就和前一个交换位置,第一个就不换了,现在问你要滑动多少次. 析:这个题,没什么算法,就是模拟呗,不过要注意时间,不能TLE,所以我们就得提前把所有的位置都存下来,让查找的时间变成 O(1),否则就会超时,可以用两个数组,也可以用map,一个存编号,一个存位置, 然后运行完后再交换就行了. 代码如下: #include <iostream> #incl

CodeForces 508C Anya and Ghosts

Anya and Ghosts Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 508C Description Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts toni

Anya and Smartphone

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

Codeforces 525E Anya and Cubes 中途相遇法

题目链接:点击打开链接 题意: 给定n个数,k个感叹号,常数S 下面给出这n个数. 目标: 任意给其中一些数变成阶乘,至多变k个. 再任意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制状压,中途查找. #include <stdio.h> #include <vector> #include <algorithm> #include <iostream> #include <cmath> #include <map>

Codeforces 525E Anya and Cubes

题意:给你n(25)个数,任选几个数 ,你最多可以对任选的几个数中的 K个数进行操作,操作是将 这个数变为它的阶乘,你选出来的数经过操作以后 等于 s的种类数有多少. 解题思路:看到这题的时候没什么思路,想想水一发dp  ,dp[i][j][k]<map>,就想到了这个四维的DP,但是显然这是会挂掉的.因为情况太多了,最多有 3^n种. 那么需要是什么方法呢.那就是折半了,因为我们只需要找到一个值, 而全部DP的话会产生太多的值了,而这大部分值都不是我们需要的.所以就把n个数分成两堆去处理,然

codeforces E - Anya and Cubes 分块处理 暴力搜索

说的是给了n个立方体,立方体从1标号到n,每个立方体上有一个数字, 你有 k 个机会 使得其中 k个数位他们自己的阶乘,(自然使用可以少于k次机会,每个立方体最多被使用1次) ,那么求出你从这n个立方体重选出任意个立方体使得 他们的和为S n<25 可以知道直接使用n去枚举自然受不了, 我们将n个数字分为两部分来算,前面n/2个数字 我们枚举他们使用j次可以得到的数,然后后面的n-n/2个数再次使用这个方法,直接在dfs枚举的时候进行判断S-s 在前一个钟是否出现过,出现过就加起来. 分块处理

CodeForces 518C - Watto and Mechanism(模拟)

题意:有n(1 <= n <= 10^5)个应用,每屏有k(1 <= k <= 10^5)个应用,现在有m(1 <= m <= 10^5)个操作,每次操作会使用一个应用(使用时需滑到应用所在的屏),使用后此应用与前边的相邻应用交换位置,退出此应用后会回到初始屏.问这m次操作总的滚动屏幕次数. 模拟即可. #include<cstdio> #include<cstring> #include<cctype> #include<cs

Codeforces Round #293 (Div. 2)

 A Vitaly and Strings 题意:给定长度相等的字符串s,t,且s的字典序小于t,求一个字符串q字典序大于s小于t. 分析:将字符串看做26进制的数,对s”+1“即可. #include <bits/stdc++.h> #define pb push_back #define mp make_pair #define esp 1e-14 #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #defi

codeforces 525 E Anya and Cubes 中途相遇法

codeforces 525 E Anya and Cubes 中途相遇法 题意: 给出n个数a1,a2,...,an,要求从中选出一些数,可以把其中最多k个变成它自己的阶乘,然后选出的数求和,问最后和等于s的选法有多少种. 限制: 1 <= n <= 25; 0 <= k <= n; 1<= s <= 1e16; 1 <= ai <= 1e9 思路: 一般数据量20~30都会考虑中途相遇法,就是折半暴力. ps:用三进制暴力会比直接深搜多一个常数10,因为