codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

518C.md (3923B)


      1 Source: https://codeforces.com/problemset/problem/518/C
      2 
      3 C. Anya and Smartphone
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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).
     22 
     23 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.
     24 
     25 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.
     26 
     27 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.
     28 
     29 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?
     30 
     31 Note that one application may be launched multiple times.
     32 
     33 Input
     34 
     35 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.
     36 
     37 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*.
     38 
     39 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.
     40 
     41 Output
     42 
     43 Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.
     44 
     45 Examples
     46 
     47 Input
     48 
     49 ```
     50 8 3 3  
     51 1 2 3 4 5 6 7 8  
     52 7 8 1
     53 ```
     54 
     55 Output
     56 
     57 ```
     58 7
     59 ```
     60 
     61 Input
     62 
     63 ```
     64 5 4 2  
     65 3 1 5 2 4  
     66 4 4 4 4
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 8
     73 ```
     74 
     75 Note
     76 
     77 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.
     78 
     79 After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures.
     80 
     81 After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures.
     82 
     83 After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.
     84 
     85 In total, Anya makes 7 gestures.