CF518C.Anya and Smartphone

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 2_k_-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.

安雅购买了一部使用 Berdroid 操作系统的新智能手机。该手机的菜单中恰好有 nn 个应用程序,每个应用程序都有其专属图标。这些图标分布在不同的屏幕上,每屏显示 kk 个图标:第 11 至第 kk 个图标位于第一屏,第 (k+1)(k+1) 至第 2k2k 个图标位于第二屏,依此类推(最后一屏可能未填满)。

初始状态下,手机菜单显示的是第 11 屏。要启动位于第 tt 屏上的某个应用程序图标,安雅需执行如下手势操作:首先通过滑动屏幕切换至目标屏幕 tt,共需 t−1t-1 次滑动手势(若该图标恰在第 tt 屏上),然后额外执行一次手势——精确点击该应用程序图标一次以启动它。

应用启动后,菜单会自动返回至第 11 屏。也就是说,启动下一个应用时,必须再次从第 11 屏开始重新滑动浏览菜单。

所有应用程序编号为 11 至 nn。我们已知初始状态下各应用图标在菜单中的排列顺序;但该顺序会在操作系统使用过程中动态变化。Berdroid 是一套智能系统,它会将使用频率更高的应用图标逐步移至列表前端。具体而言:每当一个应用被启动后,Berdroid 立即将该应用图标与其前一个位置(即菜单顺序中索引小 11 的位置)上的图标进行交换。该前一个图标可能位于相邻的屏幕上。唯一的例外是:若被启动的应用图标已处于列表首位,则图标顺序不发生任何改变。

安雅已规划好她将要启动应用的顺序。问:安雅按此计划顺序启动全部应用,总共需要多少次手势操作?

注意:同一个应用可能被多次启动。

输入格式

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 _a_1, _a_2, ..., a__n — the initial order of icons from left to right in the menu (from the first to the last one), a__i — is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among a__i.

The third line contains m integers _b_1, _b_2, ..., b__m(1 ≤ b__i ≤ n) — the ids of the launched applications in the planned order. One application may be launched multiple times.

输入的第一行包含三个数字 nn、mm、kk(1≤n,m,k≤1051 \leq n, m, k \leq 10^5)——分别表示 Anya 的智能手机上安装的应用数量、将要启动的应用数量,以及同一屏幕中显示的图标数量。

第二行包含 nn 个整数,构成一个排列 a1,a2,…,ana_1, a_2, \dots, a_n —— 表示菜单中图标的初始从左到右顺序(从第一个到最后一个),其中 aia_i 表示位于菜单第 ii 位的图标的对应应用编号。aia_i 中恰好包含从 11 到 nn 的每个整数各一次。

第三行包含 mm 个整数 b1,b2,…,bmb_1, b_2, \dots, b_m(1≤bi≤n1 \leq b_i \leq n)—— 表示按计划启动的应用编号序列。同一个应用可能被多次启动。

输出格式

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

输出一个整数——Anya 为按指定顺序启动所有应用程序所需做出的手势数量。

输入输出样例

  • 输入#1

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

    输出#1

    7
  • 输入#2

    5 4 2
    3 1 5 2 4
    4 4 4 4

    输出#2

    8

说明/提示

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.

在第一个测试用例中,初始配置为 (123)(456)(78),即:第一屏包含应用程序 1、2、3 的图标,第二屏包含应用程序 4、5、6 的图标,第三屏包含应用程序 7、8 的图标。

启动应用程序 7 后,图标的新排列变为 (123)(457)(68)。为启动它,Anya 做了 3 次手势。

启动应用程序 8 后,图标配置变为 (123)(457)(86)。为启动它,Anya 做了 3 次手势。

启动应用程序 1 后,菜单中的图标排列不发生变化。为启动它,Anya 做了 1 次手势。

总计,Anya 做了 7 次手势。

输入解题思路,AI测评打分。不知道怎么写?

首页