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.

Sample test(s)
input
8 3 3
1 2 3 4 5 6 7 8
7 8 1
output
7
input
5 4 2
3 1 5 2 4
4 4 4 4
output
8
Note

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.

这个有点意思,运行一次之后往前挪一个位置(只要不是第一个),于是我开了两个数组,分别储存数据和数据的位置。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<cctype>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#define LL long long
#define PF(x) ((x)*(x))
#define LF(x) ((x)*PF(x)) using namespace std;
const int INF=<<-;
const int max9=1e9;
const int max6=1e6;
const int max3=1e3; int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
//s1真实的数据
//s2数据所在的位置
int s1[],s2[];
int main()
{
LL n,m,k;
while(cin >> n >> m >> k)
{
for(int i=;i<n;i++)
{
cin >> s1[i];
s2[s1[i]]=i;
}
LL Ans=;
int temp;
for(int i=;i<m;i++)
{
cin >> temp;
Ans+=s2[temp]/k+;
int c; if(s2[temp])
{
int w1=s2[temp];
int w2=s2[temp]-;
int sh1=s1[w1];
int sh2=s1[w2];
s1[w1]=sh2;
s1[w2]=sh1;
s2[sh1]=w2;
s2[sh2]=w1;
}
}
cout << Ans << endl;
}
return ;
}

codeforces 518C. Anya and Smartphone的更多相关文章

  1. CoderForces 518C Anya and Smartphone (模拟)

    题意:给定一个手机,然后一共有 n 个app,告诉你每个屏幕最多放 k 个,现在要你运行 m 个app,每次都从第一个屏幕开始滑动,每运行一个,它就和前一个交换位置,第一个就不换了,现在问你要滑动多少 ...

  2. Codeforces Round #293 (Div. 2) C. Anya and Smartphone 数学题

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

  3. C. Anya and Smartphone

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

  4. CodeForces 508C Anya and Ghosts

     Anya and Ghosts Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  5. Codeforces 525E Anya and Cubes

    http://codeforces.com/contest/525/problem/E 题意: 有n个方块,上面写着一些自然数,还有k个感叹号可用.k<=n 你可以选任意个方块,然后选一些贴上感 ...

  6. CodeForces 508C Anya and Ghosts 贪心

    做不出题目,只能怪自己不认真 题目: Click here 题意: 给你3个数m,t,r分别表示鬼的数量,每只蜡烛持续燃烧的时间,每个鬼来时要至少亮着的蜡烛数量,接下来m个数分别表示每个鬼来的时间点( ...

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

    说的是给了n个立方体,立方体从1标号到n,每个立方体上有一个数字, 你有 k 个机会 使得其中 k个数位他们自己的阶乘,(自然使用可以少于k次机会,每个立方体最多被使用1次) ,那么求出你从这n个立方 ...

  8. Codeforces 525E Anya and Cubes 中途相遇法

    题目链接:点击打开链接 题意: 给定n个数.k个感叹号,常数S 以下给出这n个数. 目标: 随意给当中一些数变成阶乘.至多变k个. 再随意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制 ...

  9. Codeforces Round #293 (Div. 2)

    A. Vitaly and Strings 题意:两个字符串s,t,是否存在满足:s < r < t 的r字符串 字符转处理:字典序排序 很巧妙的方法,因为s < t,只要找比t字典 ...

随机推荐

  1. python3.4+pyspider爬58同城(二)

    之前使用python3.4+selenium实现了爬58同城的详细信息,这次用pyspider实现,网上搜了下,目前比较流行的爬虫框架就是pyspider和scrapy,但是scrapy不支持pyth ...

  2. python笔记之编程风格大比拼

    python笔记之编程风格大比拼 虽然我的python age并不高,但我仍然愿意将我遇到的或者我写的有趣的python程序和大家一块分享,下面是我找到的一篇关于各类python程序员的编程风格的比较 ...

  3. JDBC、Hibernate、Java类型对照表

    Hibernate映射类型 java类型 标准SQL类型 integer java.lang.Integer integer long java.lang.Long bigint short java ...

  4. You and your research

    英文版http://www.cs.virginia.edu/~robins/YouAndYourResearch.html 视频版http://www.youtube.com/watch?v=a1zD ...

  5. Keil UV4 BUG(带字库液晶不能显示“数、正、过”问题的请看)

    Keil UV3一直存在汉字显示(0xFD)的bug,以前在用到带字库的12864液晶的时候,“数”字总是不能正常显示,后来有网友告诉我这是keil的bug,解决掉了.后来keil升级了,我也换了新版 ...

  6. SQL数据转移

    方法一:如果想把数据库A中的表Table1中的数据复制到数据库B中的表Table2中,也就是要预先建立Table2,可以使用一下语句: use B goinsert into Table2 selec ...

  7. FTA

    FTA - 维基百科,自由的百科全书 FTA 维基百科,自由的百科全书   跳转至: 导航. 搜索 FTA可以指: 自由贸易协定(Free Trade Agreement) 自由贸易区(Free Tr ...

  8. wxpython StatuBar 带进度条的状态栏

    # -*- coding: utf- -*- import wx class customStatusBar(wx.StatusBar): def __init__(self, parent): wx ...

  9. EasyMonkeyDevice vs MonkeyDevice&amp;HierarchyViewer API Mapping Matrix

    1. 前言 本来这次文章的title是写成和前几篇类似的<EasyMonkeyDevice API实践全记录>,内容也打算把每一个API的实践和建议给记录下来,但后来想了下认为这样子并非最 ...

  10. RequireJs运行原理

    在require中,根据AMD(Asynchronous Module Definition)的思想,即异步模块加载机制,其思想就是把代码分为一个一个的模块来分块加载,这样无疑可以提高代码的重用. 在 ...