Timus 1880. Psych Up's Eigenvalues题解

At one of the contests at the Petrozavodsk Training Camp, players of the Psych Up team quickly found the simple problem and Fedya sat at the computer. The solution was ready in five minutes. Without spending time for testing, Fedya submitted it and in a few seconds got Time Limit Exceeded.
Fedya crumpled the problem statement and left the room slamming the door behind him. Things were going the wrong way and he needed to wind down. On his way to the restroom Fedya heard a conversation of the contest authors. Pasha was discussing with his friend the solution of the problem for which Fedya had got TLE. Fedya could only discern the word “eigenvalues.”
Fedya thought about it and decided that he, for sure, had his eigenvalues. For example, the date of birth, the number of his apartment, the mark he had got at the latest exam, or the number of travels to contests. But they had a team contest, so what was their team's eigenvalue? Of course, a number was a team's eigenvalue if it was an eigenvalue for each of its players. With these joyful thoughts Fedya returned to the contest room.

Input

The input data consist of three blocks two lines each. The first line of each block contains the number nof a player's eigenvalues<nobr>(1 ≤<em>n</em>≤ 4000)</nobr>. In the second line you are given ndistinct integers in ascending order, which are the eigenvalues. All the eigenvalues are positive integers not exceeding10 9.

Output

Output the number of eigenvalues of the Psych Up team.

Sample

input output
5
13 20 22 43 146
4
13 22 43 146
5
13 43 67 89 146
3


即比较三个数组有多少相同的数字:

可以时间效率是O(n),n是最小数组的长度

#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;

void PsychUpEigenvalues()
{
	int t1 = 0, t2 = 0, t3 = 0;
	cin>>t1;
	vector<int> v1(t1);
	for (int i = 0; i < t1; i++)
	{
		cin>>v1[i];
	}
	cin>>t2;
	vector<int> v2(t2);
	for (int i = 0; i < t2; i++)
	{
		cin>>v2[i];
	}
	cin>>t3;
	vector<int> v3(t3);
	for (int i = 0; i < t3; i++)
	{
		cin>>v3[i];
	}
	int c = 0;
	for (int i = 0, j = 0, k = 0; i < t1 && j < t2 && k < t3; )
	{
		int t = min(v1[i], min(v2[j], v3[k]));
		if (t == v1[i] && t == v2[j] && t == v3[k])
		{
			c++, i++, j++, k++;
		}
		else if (t == v1[i]) i++;
		else if (t == v2[j]) j++;
		else k++;
	}
	cout<<c<<endl;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值