首页 > 学院 > 开发设计 > 正文

CF 779A Pupils Redistribution 模拟,水题

2019-11-06 06:36:15
字体:
来源:转载
供稿:网友

题目链接:见这里 题意:给了两个数组a, b。现在你可以交换a,b数组的任意两个数,问最少交换多少次,可以让a,b数组里面每个数的出现次数相同,不能达到上述状态输出”-1”。其中a,b数组里面每个数是1-5. 解法:模拟,想想就可以知道如果对于1-5这每个数,如果出现次数之和为奇数那么肯定不能达到目标。否则答案就是就是所有数出现次数的差除以2之和,并且最后答案再除以2。不这样计算,直接贪心模拟也是可以的。

//CF 779A#include <bits/stdc++.h>using namespace std;const int maxn = 100;int n, x, cnt1[6], cnt2[6];int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++){ int x; scanf("%d", &x); cnt1[x]++; } for(int i = 1; i <= n; i++){ int x; scanf("%d", &x); cnt2[x]++; } for(int i = 1; i <= 5; i++){ if((cnt1[i]+cnt2[i])&1){ puts("-1"); return 0; } } int ans = 0; for(int i = 1; i <= 5; i++){ ans += abs(cnt1[i] - cnt2[i]) / 2; } ans /= 2; PRintf("%d/n", ans); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表