codeforces-scrape

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

227B.md (3545B)


      1 Source: https://codeforces.com/problemset/problem/227/B
      2 
      3 B. Effective Approach
      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 Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.
     22 
     23 According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.
     24 
     25 Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to *n*) and ending with the *n*-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the *n*-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.
     26 
     27 To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to *n*, and generated *m* queries of the form: find element with value *b**i* in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.
     28 
     29 But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.
     30 
     31 Input
     32 
     33 The first line contains integer *n* (1 ≤ *n* ≤ 105) — the number of elements in the array. The second line contains *n* distinct space-separated integers *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ *n*) — the elements of array.
     34 
     35 The third line contains integer *m* (1 ≤ *m* ≤ 105) — the number of queries. The last line contains *m* space-separated integers *b*1, *b*2, ..., *b**m* (1 ≤ *b**i* ≤ *n*) — the search queries. Note that the queries can repeat.
     36 
     37 Output
     38 
     39 Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.
     40 
     41 Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 2  
     49 1 2  
     50 1  
     51 1
     52 ```
     53 
     54 Output
     55 
     56 ```
     57 1 2
     58 ```
     59 
     60 Input
     61 
     62 ```
     63 2  
     64 2 1  
     65 1  
     66 1
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 2 1
     73 ```
     74 
     75 Input
     76 
     77 ```
     78 3  
     79 3 1 2  
     80 3  
     81 1 2 3
     82 ```
     83 
     84 Output
     85 
     86 ```
     87 6 6
     88 ```
     89 
     90 Note
     91 
     92 In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).
     93 
     94 In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).