CF227B.Effective Approach

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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.

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.

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.

一次团队训练中,瓦夏、佩佳和萨沙遇到了一道关于在数组中实现线性查找的编程题。

据三位同学所述,线性查找的工作方式如下:按照预先选定的顺序,依次将数组中的元素与待查找的目标数值进行比较;一旦找到与目标值相等的数组元素,查找即告结束。该算法的效率由执行的比较次数来衡量——线性查找所作的比较次数越少,其效率就越高。

瓦夏认为,若线性查找按顺序遍历数组元素(从第 1 个元素开始,到第 nn 个元素结束,在本题中我们约定数组下标从 11 到 nn 编号),则效果更佳。而佩佳则反驳说瓦夏错了:若改为从第 nn 个元素开始、逆序遍历至第 1 个元素,则所需比较次数会更少。萨沙则主张这两种方法完全等效。

为了尽快开始正式解题,三人决定通过一个实例来平息争论。他们选取了一个长度为 nn 的数组,该数组是整数 11 到 nn 的一个排列,并生成了 mm 个查询,每个查询形如:在数组中查找值为 bib_i 的元素。他们希望分别计算出:采用上述两种遍历方式时,线性查找响应全部 mm 个查询所需的总比较次数。若第一种方式(正向遍历)总比较次数更少,则瓦夏获胜;若第二种方式(反向遍历)总比较次数更少,则佩佳获胜;若两者总比较次数相等,则萨沙胜出。

但问题在于,线性查找本身太慢了。因此,若无外力相助,三位同学在训练结束前根本无法得出谁对谁错的结论。现在,就轮到你登场了!请帮他们判断这场争论的最终赢家是谁。

输入格式

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.

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.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)—— 数组中元素的个数。
第二行包含 nn 个互不相同的、以空格分隔的整数 a1, a2, ..., ana_1,\,a_2,\,...,\,a_n(1≤ai≤n1 \leq a_i \leq n)—— 数组的元素。

第三行包含一个整数 mm(1≤m≤1051 \leq m \leq 10^5)—— 查询的个数。
最后一行包含 mm 个以空格分隔的整数 b1, b2, ..., bmb_1,\,b_2,\,...,\,b_m(1≤bi≤n1 \leq b_i \leq n)—— 搜索查询。注意:查询可能重复。

输出格式

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

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.

输出两个整数,分别表示瓦夏(Vasya)的方法和佩佳(Petya)的方法所需的比较次数。两个数字之间用空格分隔。

请注意,在 C++ 中不要使用 %lld 说明符来读取或写入 64 位整数。推荐使用 cin、cout 流,或 %I64d 说明符。

输入输出样例

  • 输入#1

    2
    1 2
    1
    1

    输出#1

    1 2
  • 输入#2

    2
    2 1
    1
    1

    输出#2

    2 1
  • 输入#3

    3
    3 1 2
    3
    1 2 3

    输出#3

    6 6

说明/提示

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

在第一个样例中,瓦西娅的方法只需一次比较(从第 1 个元素开始,立即找到目标数),而佩佳的方法需要两次比较(首先与数组的第 2 个元素比较,未找到目标,再与第 1 个元素比较)。

在第二个样例中,情况相反:瓦西娅的方法需要两次比较(先与第 1 个元素比较,再与第 2 个元素比较),而佩佳的方法仅需一次比较即可找到目标值(第一次即与第 2 个元素比较)。

输入解题思路,AI测评打分。不知道怎么写?

首页