CF 518C (Anya and Smartphone- ing)

Source: Internet
Author: User
Tags cmath

CF 518C (Anya and Smartphone- ing)

 

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 exactlyNApplications, each application has its own icon. The icons are located on different screens, one screen containsKIcons. The icons from the first toK-Th one are located on the first screen, from (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 screenT, Anya needs to make the following gestures: first she scrolls to the required screen numberT, By makingT? -? 1 gestures (if the icon is on the screenT), 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 1N. 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 shoshould Anya make to launch the applications in the planned order? Www.bkjia.com

Note that one application may be launched multiple times.

Input

The first line of the input contains three numbersN,?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 containsNIntegers, permutationA1 ,?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 goesI-Th in the menu. Each integer from 1NOccurs exactly onceAI.Www.bkjia.com

The third line containsMIntegersB1 ,?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 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
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.


Use h [I] to represent the position of the id = I element, and then the force

 

Note: I forget long, don't I? Hack

 

 

#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        
         using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i
         
          =0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (200000+10)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}typedef long long ll;int n,m,k;int a[MAXN],b[MAXN];int h[MAXN];int main(){//freopen("Smartphone.in","r",stdin);//freopen(".out","w",stdout);scanf("%d%d%d",&n,&m,&k);For(i,n) scanf("%d",&a[i]);For(i,m) scanf("%d",&b[i]);For(i,n) h[a[i]]=i; ll ans=0;For(i,m){int t=h[b[i]];ans+=(t-1)/k+1;if (t>1){swap(a[t],a[t-1]);h[a[t]]=t;h[a[t-1]]=t-1;}}cout<
          

 

 

Related Article

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: info-contact@alibabacloud.com 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.