UVa12049 - Just Prune The List

20 篇文章 0 订阅

You are given two list of integers. You can remove any number of elements from any of them. You have to ensure that after removing some elements both of the list will contain same elements, but not necessarily in same order. For example consider the following two lists -

List #11 2 3 2 1
List #21 2 5 2 3

After removing 1 from first list and 5 from second list, both lists will contain same elements. We will find the following lists after removing two elements.

List #11 2 3 2
List #21 2 2 3

What is the minimum number of elements to be removed to obtain two list having same elements?

Input

The first line of the input file contains an integer T (T ≤ 100) which denotes the total number of test cases. The description of each test case is given below:

First line will contain two integers N (1 ≤ N ≤ 10000), the number of element in the first list and M (1 ≤ M ≤ 10000), the number of element in the second list. The next line will contain N integers representing the first list followed by another line having M elements representing the second list. Each integers in the input is 32 bit signed integer.

Output

For each test case output a single line containing the number of elements to be removed. See sample output for exact format.

Sample Input

1
5 5
1 2 3 2 1
1 2 5 2 3

Sample Output

2
用STL中的multiset

#include <cstdio>
#include <set>
#include <cstdlib>

using namespace std;

int n, m;
multiset<int> s1, s2;

void input()
{
	s1.clear();
	s2.clear();
	
	int num;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &num);
		s1.insert(num);
	}
	
	for (int i = 0; i < m; i++) {
		scanf("%d", &num);
		s2.insert(num);
	}
}

void solve()
{
	int ans = 0;
	int prev;
	for (multiset<int>::iterator it = s2.begin(); it != s2.end(); it++) {
		if (it == s2.begin()) prev = *it;
		else if (*it == prev) continue;
		
		if (s1.count(*it)) {
			ans += abs((int)(s1.count(*it) - s2.count(*it)));
			s1.erase(*it);
		} else {
			ans += s2.count(*it);
		}
		
		prev = *it;
	}
	
	ans += s1.size();
	
	printf("%d\n", ans);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif
	
	int t;
	scanf("%d", &t);
	for (int i = 1; i <= t; i++) {
		input();
		solve();
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值