【题目链接】:http://codeforces.com/contest/779/problem/A

【题意】



让你把两个组的5个人的数目都变成一样的.

支持交换操作;

问你最少需要交换几次.

【题解】

/*
哪个少了,就往对面的多的换一下;
模拟一下写个贪心就好
A:num[1..5]
B:num[1..5]
rep1(i,1,5)
{
if (bnum[i]>anum[i])
{
rep1(j,i,5)
swap(anum[j],bnum[j]);
}
if (bnum[i]<anum[i])
{
if ((bnum[i]+anum[i])&1)
return puts("-1"),0;
while (bnum[i]<anum[i])
{
int j = -1;
rep1(k,i+1,5)
if (bnum[k]>anum[k] && (anum[k]+bnum[k])%2==0)
{
j = k;
break;
}
if (j==-1)
return puts("-1"),0;
bnum[j]--,anum[j]++;
bnum[i]++,anum[i]--;
}
}
}
*/

【完整代码】


#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110; int anum[10],bnum[10]; int n,ans = 0; int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n);
rep1(i, 1, n)
{
int x;
rei(x);
anum[x]++;
}
rep1(i, 1, n)
{
int x;
rei(x);
bnum[x]++;
}
rep1(i, 1, 5)
{
if (bnum[i]>anum[i])
{
rep1(j, i, 5)
swap(anum[j], bnum[j]);
}
if (bnum[i]<anum[i])
{
if ((bnum[i] + anum[i]) & 1)
return puts("-1"), 0;
while (bnum[i]<anum[i])
{
int j = -1;
rep1(k, i + 1, 5)
if (bnum[k]>anum[k] && (anum[k] + bnum[k]) % 2 == 0)
{
j = k;
break;
}
if (j == -1)
return puts("-1"), 0;
bnum[j]--, anum[j]++;
bnum[i]++, anum[i]--;
ans++;
}
}
}
printf("%d\n", ans);
return 0;
}

【codeforces 779A】Pupils Redistribution的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 758C】Unfair Poll

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. python2与python3的不同

    1.源码: python2的源码混乱.冗余.重复,非常不规范 python3的源码经过整合,优美.清晰.简单 2.编码: python2的默认编码是ASCII码,不能识别中文,需要在行首添加# -*- ...

  2. 【2017 Multi-University Training Contest - Team 7 && hdu 6121】Build a tree

    [链接]点击打开链接 [题意] 询问n个点的完全k叉树,所有子树节点个数的异或总和为多少. [题解] 考虑如下的一棵k=3叉树,假设这棵树恰好有n个节点. 因为满的k叉树,第i层的节点个数为k^(i- ...

  3. MyBatis学习总结(14)——Mybatis使用技巧总结

    1. 区分 #{} 和 ${}的不同应用场景 1)#{} 会生成预编译SQL,会正确的处理数据的类型,而${}仅仅是文本替换. 对于SQL: select * from student where x ...

  4. 洛谷—— P1069 细胞分裂

    https://www.luogu.org/problem/show?pid=1069#sub 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细 ...

  5. [Vue] Load components when needed with Vue async components

    In large applications, dividing the application into smaller chunks is often times necessary. In thi ...

  6. MVC中url路由规则

    Routing:首先获取视图页面传过来的请求,并接受url路径中的controller和action以及参数数据,根据规则将识别出来的数据传递给某controller中的某个action方法 MapR ...

  7. [RxJS] Multicasting shortcuts: publish() and variants

    Because using multicast with a new Subject is such a common pattern, there is a shortcut in RxJS for ...

  8. redis学习笔记之虚拟内存

    首先说明下redis的虚拟内存与os的虚拟内存不是一码事,但是思路和目的都是相同的.就是暂时把不经常访问的数据从内存交换到磁盘中,从而腾出宝贵的 内存空间用于其他需要访问的数据.尤其是对于redis这 ...

  9. php 静态方法和非静态方法的调用说明

    1. php类中,静态方法调用当前类的非静态方法必须用self关键字,不能用$this 2. php类中,公有方法调用私有方法使用$this关键字,只能实例化调用 3. php类中,公有方法调用私有方 ...

  10. [AHK]自定义默认浏览器

    https://blog.csdn.net/liuyukuan/article/details/78844383