CF779A(round 402 div.2 A) Pupils Redistribution

题意:

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 nstudents. 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

45 4 4 45 5 4 5

output

1

input

61 1 1 1 1 15 5 5 5 5 5

output

3

input

153

output

-1

input

93 2 5 5 2 3 3 3 24 1 4 1 1 2 4 4 1

output

4

思路:

构造。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 using namespace std;
 6
 7 int n, tmp, ca[6], cb[6];
 8 int main()
 9 {
10     cin >> n;
11     for (int i = 0; i < n; i++)
12     {
13         scanf("%d", &tmp);
14         ca[tmp]++;
15     }
16     for (int i = 0; i < n; i++)
17     {
18         scanf("%d", &tmp);
19         cb[tmp]++;
20     }
21     int cnt = 0;
22     bool flag = true;
23     for (int i = 1; i <= 5; i++)
24     {
25         tmp = abs(cb[i] - ca[i]);
26         if (tmp & 1)
27         {
28             flag = false;
29             break;
30         }
31         cnt += tmp;
32     }
33     if (!flag)
34         cout << "-1" << endl;
35     else
36         cout << cnt / 4 + cnt % 4 / 2 << endl;
37     return 0;
38 }
时间: 2024-10-10 09:06:15

CF779A(round 402 div.2 A) Pupils Redistribution的相关文章

Codeforces Round #402 (Div. 2) C. Dishonest Sellers

题目链接:Codeforces Round #402 (Div. 2) C. Dishonest Sellers 题意: 有n个商品,每个商品这一周为ai的价格,下一周为bi的价格. 现在那个人要将这n个商品全部买掉,这一周最少要买k个商品, 为最小的花费是多少. 题解: xjb贪心一下. 按ai-bi排序,为负的要全部买掉,然后如果小于k件,就买前k件. 最后再买下一周的. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i&

Codeforces Round #402 (Div. 2) D. String Game

题目链接:Codeforces Round #402 (Div. 2) D. String Game 题意: 给你两个字符串a,b,然后给你n=strlen(a)个数字n1,n2,...,nn,表示依次删a[ni-1]个字符. 当a串删到有k(k任意)个子串组合起来(顺序不变)刚好等于b串时,就不能删了. 比如 abba->(ab a) 刚好包括 aba ,bba不包括aab. 问最多能删多少次. 题解: 最开始还以为是要用某种数据结构啥的,结果都想复杂了,直接二分答案就行. 然后线性判断删掉后

Codeforces Round #402 (Div. 2)

补的,不过都是自己做的. A.Pupils Redistribution [数学] 题意:交换A.B两数组中的元素,使得两组数组含1.2.3.4.5元素的个数相等. 做法:统计A组中1~5的个数,B组中减去.统计正数/2.负数/2绝对值,求两者最大值.数学问题,自己推一下. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> #defi

Codeforces Round #402 (Div. 2) A

Description 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 a

Codeforces Round #402 (Div. 2) B

Description Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k?=?3, in the

【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding

暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=100; bool vis[21]; void calc(int now) { int t=0; bool flag=0; for(int i=m;i>=1;--i) if(!vis[i]) { if((!flag) && a[i]==0) return; t=t*10+a[i]; fla

Codeforces Round #402 (Div. 2) C

Description Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi. Not all of sellers are

Codeforces Round #402 (Div. 2) D

Description Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the w

CF778B(round 402 div.2 E) Bitwise Formula

题意: Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consis