【Codeforces-402A】-Pupils Redistribution(思维)
2017-02-27 21:14
357 查看
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
output
input
output
input
output
input
output
题解:要求两组拥有的1~5数目相同,记数,判断,计算
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
int n;
int a[6],b[6];
scanf("%d",&n);
CLR(a,0);
CLR(b,0);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
a[x]++;
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
b[x]++;
}
int num=0;
int flag=0;
for(int i=1;i<=5;i++)
{
if(a[i]==b[i])
continue;
else
{
int ant=max(a[i],b[i])-min(a[i],b[i]);
if(ant&1)
{
flag=1;
break;
}
else
num+=ant;
}
}
if(flag)
printf("-1\n");
else
printf("%d\n",num/4);
return 0;
}
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
题解:要求两组拥有的1~5数目相同,记数,判断,计算
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
int n;
int a[6],b[6];
scanf("%d",&n);
CLR(a,0);
CLR(b,0);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
a[x]++;
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
b[x]++;
}
int num=0;
int flag=0;
for(int i=1;i<=5;i++)
{
if(a[i]==b[i])
continue;
else
{
int ant=max(a[i],b[i])-min(a[i],b[i]);
if(ant&1)
{
flag=1;
break;
}
else
num+=ant;
}
}
if(flag)
printf("-1\n");
else
printf("%d\n",num/4);
return 0;
}
相关文章推荐
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- Codeforces 230D Planets【思维+SPFA】
- CodeForces - 839B Game of the Rows 贪心+思维
- codeforces 181.div2 300A --Array 思维问题
- CodeForces - 734D Anton and Chess (思维题)
- CodeForces 496 D. Tennis Game【思维】
- Codeforces 233B:Non-square Equation(数学+思维)
- Codeforces 349B Color the Fence【贪心+思维】
- CodeForces - 750C(思维)
- Codeforces 582B Once Again...【思维YY+Dp(LIS)】好题~
- Codeforces 844C Sorting by Subsequences【思维】
- CodeForces 740C - Alyona and mex(思维)
- codeforces 577A Multiplication Table【思维】
- codeforces 359A- Table(简单思维)
- codeforces-749【思维】
- Codeforces 106D Treasure Island【思维+二维前缀和】
- codeforces-757-【B、C思维】
- Codeforces 534B Covered Path【有点奇怪的思维+贪心】
- codeforces 269A Magical Boxes 思维问题
- codeforces 954C. Matrix Walk(思维模拟)