Anya and Smartphone

Source: Internet
Author: User

Anya have bought a new smartphone that uses berdroid operating system. The Smartphone menu has exactly n applications and each application have its own icon. The icons is located on different screens and one screen contains k icons. The icons from the first to the K-th One is located on the first screens, from the (K + 1)-th to The 2k-th ones is on the second screens and so on (the last screen is 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:f Irst She scrolls to the required screens number T, by making T -1 gestures (if the icon was on th E screen T), and then make another gesture-press the icon of the required application exactly once to Launc H it.

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

All applications is numbered From 1 to  n . We know a certain order in which the icons of the applications is located in the menu at the beginning, but it changes as Long as your use of the operating System. berdroid is intelligent system, so it cha Nges 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 applicat Ion (that is, the icon of a application on the position, which is smaller by one in the Order of menu). The preceding icon may possibly is located on the adjacent screen. The only exception was when the icon of the the launched application already occupies the first place, in this case the icon AR Rangement doesn ' t change.

Anya has planned the order in which she'll launch applications. How many gestures should Anya do to launch the applications in the planned order?

Note that one application is launched multiple times.


The first line of the input contains three numbers n, m, k (1≤ n, m, k ≤105)-the number of applications that Anya have on she smartphone, the number of applications that would Be launched and the number of icons is located on the same.

The next line contains  n  integers, permutation  a 1, a 2, ..., a n  - the Initial order of icons 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 b1, b2, ..., bm(1≤< C10>bin)-the IDs of the launched applications in the planned order. One application may launched multiple times.


Print a single number-the number of gestures this 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
5 4 2
3 1 5) 2 4
4 4 4 4

In the first test the initial configuration looks like (123) (456) (at), that's, the first screen contains icons of apps Lications 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 was launched, the arrangement of icons in the menu doesn ' t. To launch it Anya makes 1 gesture.

In total, Anya makes 7 gestures.

The main idea: the screen, each slide in the position of the screen -1+1, note more than int--| | WA 5 times on that set of data, open two arrays, one record value, another record subscript, when the next mark changes, the value should also change, that is, A[b[num]] and A[b[temp]] exchange, TEMP is the value, B[temp] is subscript, a[b[temp]] is the subscript under the number , temp minus one from subscript, temp=a[b[num]-1];

Then open long long to consider 1 when the line, found cout,cin good use ~ ~ ~ ~ ~

#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespacestd;Const intMAX =233333 ;Long Longn,m,k,num,temp1,pos,ans,temp;Long LongA[max],b[max];intMain () {CIN>>n >> M >>K;  for(inti =1; I <= N; i++) {cin>>A[i]; B[a[i]]=i; } ans=0;  for(intI=1; I <= m; i++) {cin>>num; if(B[num] = =1) {ans++; }        Else{ans+ = b[num]/k;//B[num] is subscript         if(b[num]%k) ans++; POS= B[num]-1;//POS is the subscript of the number to be exchangedtemp = A[pos];//temp is the value of the number to be swapped.b[temp]++; B[num]--; Temp1=A[b[temp]]; A[b[temp]]=A[b[num]]; A[b[num]]=Temp1; }} cout<<ans; return 0;}
View Code

Anya and Smartphone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.