B. Proactive approach

Source: Internet
Author: User
Tags cmath
Time limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.

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 specified med comparisons. The fewer comparisons the linear search has made, the more effective it is.

Vasya believes that a linear search wocould work better if it sequentially iterates through the elements, starting with 1 -St one (in this problem
We consider the elements of the array indexed from1 To N )
And ending with N -Th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements
Starting from N -Th and ending with 1 -St
One. Sasha argues that the two approaches are equivalent.

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 valueBI 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
Winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.

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.

Input

The first line contains integer N (1 digit ≤ DigitNLimit ≤ limit 105) -
The number of elements in the array. The second line containsN Distinct space-separated Integers A1, Bytes,A2, Please..., please ,...,AN (1 digit ≤ DigitAILimit ≤ limitN) -
The elements of array.

The third line contains integer M (1 digit ≤ DigitMLimit ≤ limit 105) -
The number of queries. The last line containsM Space-separated Integers B1, Bytes,B2, Please..., please ,...,BM (1 digit ≤ DigitBILimit ≤ limitN) -
The search queries. Note that the queries can repeat.

Output

Print two integers, showing how to compute comparisons Vasya's approach needs and how to compute comparisons Petya's approach needs. Separate the numbers by spaces.

Please, do not use% LLDSpecifier to read or write 64-bit integers in bytes ++. It is preferred to useCIN,CoutStreams
Or% I64dSpecifier.

Sample test (s) Input
21 211
Output
 
1 2
Input
22 111
Output
 
2 1
Input
33 1 231 2 3
Output
 
6 6
Note

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 ).

In the second sample, on the contrary, Vasya's approach will need two comparisons (first1-St element, and then with2-Nd ),
And Petya's approach will find the required value in one comparison (the first comparison with2-Nd element ).

Description: The question refers to finding a number from the beginning and from the end of a series and determining the number of comparisons. The simplest idea is to follow this idea, but obviously it is easy to time out when there are too many searches. To this end, we should record the position of each number in the output series. The position is the number of comparisons in the first search. Adding 1 to the total number of searches at the end minus this position is the number of comparisons.

 # include 
  
    # include 
   
     # include 
    
      # include 
     
       # include 
      
        # include 
       
         # include 
        
          using namespace STD; int main () {int arr [100005], n, W, M, I; long vas = 0, PET = 0; scanf ("% d", & N ); for (I = 1; I <= N; I ++) {scanf ("% d", & W); arr [w] = I ;} scanf ("% d", & M); for (I = 1; I <= m; I ++) {scanf ("% d", & W ); vas + = arr [w]; PET + = N-Arr [w] + 1;} printf ("% i64d % i64d \ n", VAS, PET ); return 0 ;}
        
       
      
     
    
   
  

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.