187A.md (1896B)
1 Source: https://codeforces.com/problemset/problem/187/A 2 3 A. Permutations 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot. 22 23 One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through *n* and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation. 24 25 Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second. 26 27 Input 28 29 The first line contains a single integer *n* (1 ≤ *n* ≤ 2·105) — the quantity of the numbers in the both given permutations. 30 31 Next line contains *n* space-separated integers — the first permutation. Each number between 1 to *n* will appear in the permutation exactly once. 32 33 Next line describe the second permutation in the same format. 34 35 Output 36 37 Print a single integer denoting the minimum number of moves required to convert the first permutation to the second. 38 39 Examples 40 41 Input 42 43 ``` 44 3 45 3 2 1 46 1 2 3 47 ``` 48 49 Output 50 51 ``` 52 2 53 ``` 54 55 Input 56 57 ``` 58 5 59 1 2 3 4 5 60 1 5 2 3 4 61 ``` 62 63 Output 64 65 ``` 66 1 67 ``` 68 69 Input 70 71 ``` 72 5 73 1 5 2 3 4 74 1 2 3 4 5 75 ``` 76 77 Output 78 79 ``` 80 3 81 ``` 82 83 Note 84 85 In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3. 86 87 In the second sample, he removes number 5 and inserts it after 1. 88 89 In the third sample, the sequence of changes are like this: 90 91 * 1 5 2 3 4 92 * 1 4 5 2 3 93 * 1 3 4 5 2 94 * 1 2 3 4 5 95 96 So he needs three moves.