【打CF,学算法——一星级】CodeForces 227B Effective Approach (水题)

【CF简介】

提交链接:CF 227B


题面:

B. Effective Approach
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 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 the1-st one (in this problem we consider the elements of the array indexed from1 to n) and ending with then-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from then-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 from1 to n, and generatedm 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 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.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line containsn distinct space-separated integers a1, a2, ..., an(1 ≤ ai ≤ n) — the elements of array.

The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line containsm space-separated integers b1, b2, ..., bm(1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.

Output

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 usecin, cout streams or the%I64d specifier.

Examples
Input
2
1 2
1
1
Output
1 2
Input
2
2 1
1
1
Output
2 1
Input
3
3 1 2
3
1 2 3
Output
6 6
Note

In the first sample Vasya's approach will make one comparison (it starts with the1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the2-nd array element, doesn't find the search item and compares with the1-st element).

In the second sample, on the contrary, Vasya's approach will need two comparisons (first with1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the2-nd element).

题意:

     求正向比较和反向比较的次数,注意爆int。


代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#define LL long long
using namespace std;
int pos[100010];

int main()
{
	int n,tmp,m;
	LL a=0,b=0;
	memset(pos,0,sizeof(pos));
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
    {
		scanf("%d",&tmp);
		pos[tmp]=i;
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&tmp);

		if(pos[tmp])
		{
			a+=pos[tmp];
			b+=n-pos[tmp]+1;
		}
		else
		{
			a+=n;
			b+=n;
		}

	}
	printf("%lld %lld\n",a,b);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值