(贪心)Codeforces Round #402 A. Pupils Redistribution

来源:互联网 发布:淘宝客怎么拉人进群 编辑:程序博客网 时间:2024/06/05 22:57

题目网址: Codeforces Round #402 A. Pupils Redistribution

题意分析:

题意: 有两个小组的成绩(成绩分数范围1-5), 现要让两个组相同分数人数相等, 所以 要进行两个小组之间的人交换, 求最少的交换次数

  • 思路: 分别用数组 a, 数组b记录 两个小组各个分数的人数, 然后判断某个分数的人数是否是奇数, 若是 奇数, 无法使得两个组相同分数人数相同.
  • 交换次数要最少, 则交换的人数总和为(每个分数的总人数除以2 - 当前某个组该分数的人数)的绝对值.最后 交换的总人数/2(因为人是交换的)

代码:

#include <iostream>#include <cstring>#include <algorithm>using namespace std;int a[6];int b[6];int main(int argc, char const *argv[]){    int n;    int tmp;    int ans;    while (~scanf("%d", &n))    {        memset(a, 0, sizeof(a));        memset(b, 0, sizeof(b));        for (int i = 0; i < n; ++i)        {            scanf("%d", &tmp);            ++a[tmp];        }        for (int i = 0; i < n; ++i)        {            scanf("%d", &tmp);            ++b[tmp];        }        ans = 0;        for (int i = 1; i < 6; ++i)        {            tmp = a[i] + b[i];            if (tmp & 1)            {                ans = 1;                break;            }            else            {                ans += abs(tmp / 2 - a[i]);            }        }        if (ans & 1) printf("-1\n");        else printf("%d\n", ans / 2);    }    return 0;}
原创粉丝点击