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

被折叠的 条评论
为什么被折叠?



