【题目链接】: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. 【例题 7-9 UVA-1601】The Morning after Halloween

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对于没有出现的,当成0节点就好. 所以总是认为有3个人需要走到各自的终点. 将平面图转成点边图.这样比较好枚举. (二维变成一维,模 ...

  2. [Angular] Update FormArray with patchValue

    Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...

  3. Java Callable Future Example(java 关于Callable,Future的例子)

    Home » Java » Java Callable Future Example Java Callable Future Example April 3, 2018 by Pankaj 25 C ...

  4. 怎样用Adobe Acrobat 7 Pro把PDF文档拆分成多个啊?

    这个pdf文档里有多篇文章,我想把他们分开并分别保存在独立的pdf文档.怎么操作?我的电脑基础不太好,麻烦说得详细一些. Adobe Acrobat 7 Pro拆分PDF文档的方法: 1.点左边的“书 ...

  5. Android 蓝牙扫描代码

    /** * Created by rbq on 2016/11/1. */ import android.bluetooth.BluetoothAdapter; import android.blue ...

  6. OC学习篇之—写类别(类的扩展)

    首先我们来看一下场景,如果我们现在想对一个类进行功能的扩充,我们该怎么做? 对于面向对象编程的话,首先会想到继承,但是继承有两个问题: 第一个问题:继承的前提是这个类可以被继承,因为在Java中有些类 ...

  7. client产生CLOSE_WAIT状态的解决方式

    现象 生产环境和測试环境都发现有个外围应用通过搜索服务调用搜索引擎时.偶尔会出现大量的訪问超时的问题,通过例如以下方式进行分析排查: l 首先是拿到搜索服务的JavaCore.发现其堵在HttpCli ...

  8. ios越狱开发

    theos/Logos常用命令 %hook 用的最多,意思是钩住一个类. %hook SpringBoard %end %new (v@:) 新建方法 v是返回值@代表参数名 %new(v@:@i) ...

  9. jQuery插件AjaxFileUpload实现ajax文件上传时老是运行error方法 问题原因

    今天在用jQuery插件AjaxFileUpload实现ajax文件上传时,遇到一个问题,如图: 老是运行error.无法运行succes方法,追踪ajaxfileupload.js源代码发现: wa ...

  10. [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates

    You add array of todos to the store simply by adding them to the state defined in your store/index.j ...