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
output
input
output
input
output
input
output
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;
}
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;
}
相关文章推荐
- CF - 782D. Innokenty and a Football League - 模拟+思维+贪心+dfs插入
- CF - 805C. Find Amir - 贪心思维+构造
- CF 782D. Innokenty and a Football League 贪心,思维,模拟
- CF - 754A.Lesha and array splitting - 贪心+思维
- CF - 797A. k-Factorization - 思维+贪心+数学
- (CF 792E Colored Balls) 思维题 贪心
- CF - 803A. Maximal Binary Matrix - 思维+贪心
- Codeforce - 872 - B. Maximum of Maximums of Minimums【思维+贪心】
- HDU 5500 Reorder the Books(贪心+思维)
- 51nod1621-贪心&枚举&好题&cf-花钱买车牌
- Uva 11134 传说中的车 贪心的思维
- CodeForces 500C New Year Book Reading(贪心+思维)
- CF 329B(Biridian Forest-贪心-非二分)
- CF Watchmen 【思维+数学】
- CF 335A(Banana-贪心-priority_queue是大根堆)
- CF 589F 网络流(or贪心?)
- CF - 779C. Dishonest Sellers 排序+贪心
- CF 496E(Distributing Parts-序列混合贪心+set)
- CF 700E Cool Slogans 线段树+后缀数组贪心求解
- 【CodeForces】---Party Lemonade(贪心&&思维&&递归)