Pupils Redistribution

本文介绍了一种算法,用于解决两个学生小组间学术表现分布均衡的问题。通过计算各组内不同学术表现的学生数量,并进行必要的交换操作,使得最终两组学生的学术表现分布相同。文章提供了完整的代码实现。

Pupils Redistribution

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

In Berland each high school student is characterized by academicperformance — integer value between 1 and 5.

In high school 0xFF there are two groups of pupils: thegroup A and the group B. Each groupconsists of exactly n students. Anacademic performance of each student is known — integer value between 1 and 5.

The school director wants to redistribute students between groups so thateach of the two groups has the same number of students whose academicperformance is equal to 1, the same number of students whoseacademic performance is 2 and so on. In other words, thepurpose of the school director is to change the composition of groups, so thatfor each value of academic performance the numbers of students in both groupsare equal.

To achieve this, there is a plan to produce a series of exchanges ofstudents between groups. During the single exchange the director selects onestudent from the class A and onestudent of class B. After that, theyboth change their groups.

Print the least number of exchanges, in order to achieve the desired equalnumbers of students for each academic performance.

Input

The first line of the input contains integer number n (1 ≤ n ≤ 100) — number of students in both groups.

The second line contains sequence of integer numbers a1, a2, ..., an (1 ≤ ai ≤ 5), where ai is academic performance of the i-th student of the group A.

The third line contains sequence of integer numbers b1, b2, ..., bn (1 ≤ bi ≤ 5), where bi is academic performance of the i-th student of the group B.

Output

Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.

Examples

input

4
5 4 4 4
5 5 4 5

output

1

input

6
1 1 1 1 1 1
5 5 5 5 5 5

output

3

input

1
5
3

output

-1

input

9
3 2 5 5 2 3 3 3 2
4 1 4 1 1 2 4 4 1

output

4

 

 问题分析:意思就是有两组1~5组成的数,要求最少交换几个数,是两组数的每个分数的数量相等。这里用的事模拟的方法。

首先,如果一个数字的个数是奇数,那么就不能够均分到两组,那么就肯定输出-1,这是肯定的。然后,我们比较两组每个分数的数量差值,将差值除以2就是要交换该分数的数量,从1到5依次讨论将差值除以2相加,最后会有一个和,但是由于交换是双方的,和除以2才是交换的次数,就比如第一个例子,4的差值是2,5的差值是2,和就是2,这是总的差值,要再除以2才是交换次数1,即用一个4和一个5相交换,相当于是去重


#include<stdio.h>
#include<string.h>
#include<math.h> 
#include<iostream>
using namespace std;

int a[110],b[110];
int numa[6],numb[6];

int main()
{
	int n;
	
	cin >> n;
	
	for(int i=0; i<n; i++)
	{
		cin >> a[i];
		numa[a[i]]++;		
	}		
	
	for(int i=0; i<n; i++)
	{
		cin >> b[i];
		numb[b[i]]++;
	}
	
	int p = 0;
	for(int i=1; i<=5; i++)
	{
		if ((numa[i] + numb[i]) % 2 != 0)
		{
			cout << -1 << endl;
			return 0;
		}
		p += abs(numa[i] - numb[i]) / 2;
	}
	cout << p / 2 << endl;
	
	return 0;
	
} 


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值