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

【题意】



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

支持交换操作;

问你最少需要交换几次.

【题解】

  1. /*
  2. 哪个少了,就往对面的多的换一下;
  3. 模拟一下写个贪心就好
  4. A:num[1..5]
  5. B:num[1..5]
  6. rep1(i,1,5)
  7. {
  8. if (bnum[i]>anum[i])
  9. {
  10. rep1(j,i,5)
  11. swap(anum[j],bnum[j]);
  12. }
  13. if (bnum[i]<anum[i])
  14. {
  15. if ((bnum[i]+anum[i])&1)
  16. return puts("-1"),0;
  17. while (bnum[i]<anum[i])
  18. {
  19. int j = -1;
  20. rep1(k,i+1,5)
  21. if (bnum[k]>anum[k] && (anum[k]+bnum[k])%2==0)
  22. {
  23. j = k;
  24. break;
  25. }
  26. if (j==-1)
  27. return puts("-1"),0;
  28. bnum[j]--,anum[j]++;
  29. bnum[i]++,anum[i]--;
  30. }
  31. }
  32. }
  33. */

【完整代码】


  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) scanf("%d",&x)
  13. #define rel(x) scanf("%lld",&x)
  14. typedef pair<int, int> pii;
  15. typedef pair<LL, LL> pll;
  16. const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
  17. const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
  18. const double pi = acos(-1.0);
  19. const int N = 110;
  20. int anum[10],bnum[10];
  21. int n,ans = 0;
  22. int main()
  23. {
  24. //freopen("F:\\rush.txt", "r", stdin);
  25. rei(n);
  26. rep1(i, 1, n)
  27. {
  28. int x;
  29. rei(x);
  30. anum[x]++;
  31. }
  32. rep1(i, 1, n)
  33. {
  34. int x;
  35. rei(x);
  36. bnum[x]++;
  37. }
  38. rep1(i, 1, 5)
  39. {
  40. if (bnum[i]>anum[i])
  41. {
  42. rep1(j, i, 5)
  43. swap(anum[j], bnum[j]);
  44. }
  45. if (bnum[i]<anum[i])
  46. {
  47. if ((bnum[i] + anum[i]) & 1)
  48. return puts("-1"), 0;
  49. while (bnum[i]<anum[i])
  50. {
  51. int j = -1;
  52. rep1(k, i + 1, 5)
  53. if (bnum[k]>anum[k] && (anum[k] + bnum[k]) % 2 == 0)
  54. {
  55. j = k;
  56. break;
  57. }
  58. if (j == -1)
  59. return puts("-1"), 0;
  60. bnum[j]--, anum[j]++;
  61. bnum[i]++, anum[i]--;
  62. ans++;
  63. }
  64. }
  65. }
  66. printf("%d\n", ans);
  67. return 0;
  68. }

【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. JS错误记录 - 微博发布

    <style> *{ margin: 0; padding: 0;} #ul1{ width: 400px; height: 400px; border: 1px solid #000; ...

  2. postman--基本使用1

    本文转自:http://blog.csdn.net/five3/article/details/53021084 HTTP的接口测试工具有很多,可以进行http请求的方式也有很多,但是可以直接拿来就用 ...

  3. C#数据池

    //ThreadPool(线程池)是一个静态类,它没有定义任何的构造方法(),我们只能够使用它的静态方法,这是因为,这是因为ThreadPool是托管线程池(托管线程池http://msdn.micr ...

  4. C语言结构体的字节对齐原则

    为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这就需要各种类型数据 ...

  5. Mac怎么设置wifi热点

    苹果 Mac 系统中要把无线当作 Wifi 热点来用的话,需要电脑有其它网络接入才可以,也就是说它需要一个可以用于上网的网络,比如有线网络.尤其是对于使用 MacBook Pro 或 MacBook ...

  6. TOP全异步模式

    Top全异步方式调用技术方案 背景:目前top通过servlet3.0技术结合异步管道化框架做到半异步调用,半异步调用采用异步线程同步调用后端的方式来做api call @飞不起的奥特曼 的部分文档) ...

  7. HDU 1406 完数 因子的和

    http://acm.hdu.edu.cn/showproblem.php?pid=1406 完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6= ...

  8. eclipse开发环境下集成activiti插件

    一.环境 eclipse 4.3.0 Activiti Designer 5.14.1 二.Activiti Designer 5.14.1插件安装 在eclipse中菜单help->Insta ...

  9. 【a101】高精度实数加法

    Time Limit: 1 second Memory Limit: 2 MB 问题描述 给出两个高精度正实数(可以含有小数点或没有),最长200位,字符串读入 求它们的和,小数部分末尾的0要舍去. ...

  10. FZU 1650 1752 a^b mod c

    http://acm.fzu.edu.cn/problem.php?pid=1752 http://acm.fzu.edu.cn/problem.php?pid=1650 给跪了. 我的快速幂会越界. ...