您的位置:首页 > 数据库 > Redis

CF - 779A. Pupils Redistribution 贪心+思维

2017-02-26 19:56 375 查看
1.题目描述:

A. 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 academic performance — integer value between 1 and 5.

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

The school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1,
the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the
composition of groups, so that for each value of academic performance the numbers of students in both groups are equal.

To achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class A and
one student of class B. After that, they both change their groups.

Print the least number of exchanges, in order to achieve the desired equal numbers 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 15 5 5 5 5 5


output
3


input
15
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


2.题意概述:

给你两组数列A和B,要你通过最小的交换次数使得A和B完全相等

3.解题思路:

先预处理一下A和B的每一个元素,如果存在(a[i]+b[i])是奇数,则肯定不能够通过若干次交换使得a[i] == b[i],如果是偶数,那么就进行abs(a[i] - b[i]) / 2次交换,这样贪心地保证了第i位数是相同的,然后对于a的任意一次交换必然影响b,所以结果除以2即可

4.AC代码:

include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 111#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7
using namespace std;
typedef long long ll;
int visa[6], visb[6];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n;
while (~scanf("%d", &n))
{
memset(visa, 0, sizeof(visa));
memset(visb, 0, sizeof(visb));
for (int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
visa[x]++;
}
for (int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
visb[x]++;
}
int flag = 0, ans = 0;
for (int i = 1; i <= 5; i++)
{
if ((visa[i] + visb[i]) & 1)
{
flag = 1;
break;
}
ans += abs(visa[i] - visb[i]) / 2;
}
printf("%d\n", flag == 1 ? -1 : ans / 2);
}

#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 算法 codeforces