【题目链接】:http://codeforces.com/problemset/problem/746/E

【题意】



你有n张卡片,上面写着不同的数字;

然后另外一个人有m张上面写着不同的数字的卡片:卡片上的数字从1..m;

你可以和另外一个人交换卡片;

问你最少的交换次数;

使得你的n张卡片里面,卡片上的数字为奇数的和卡片上的数字为偶数的张数相同.

且这n张卡片不能有相同的数字;

【题解】



首先考虑去重的工作;

在去重之前;先算出;

原来的n张卡片里面,卡片上的数字是奇数的数字个数odd;

然后两个变量nexto和nexte分别表示下一个没被交换的奇数和偶数(1..m里面);

对于重复出现的卡片;

看看odd和n/2的关系;

如果



odd>n/2

则不能再来奇数了,所以只能拿一张偶数的和它交换(不管重复的这张的奇偶性);

(只是如果是奇数的,则奇数张数递减);



odd==n/2

则拿一张和这个数字奇偶性相同的卡片来交换;



odd< n/2

则不能来偶数了,需要拿一张奇数的卡片和它交换(仍旧不管重复的这张的奇偶性如何,都是拿一张奇数的)

这张重复的是偶数的话,odd++;

去重结束之后;

再根据odd和n/2的关系大小贪心换每一个数字;

如果

①odd< n/2

且遇到了一个偶数;

则拿一个奇数来和它换,odd++

②odd>n/2

且遇到了一个奇数

则拿一个偶数和它换,odd–

注意在换的时候,要保证,换过之后,序列中不会有相同的数字(即换完那一瞬间不能有相同的数字,也即换的那个数字不能和序列中的其他元素相同)



【Number Of WA】



2



【完整代码】

  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 ms(x,y) memset(x,y,sizeof x)
  13. #define Open() freopen("F:\\rush.txt","r",stdin)
  14. #define Close() ios::sync_with_stdio(0),cin.tie(0)
  15. typedef pair<int,int> pii;
  16. typedef pair<LL,LL> pll;
  17. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  18. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  19. const double pi = acos(-1.0);
  20. const int N = 2e5+100;
  21. int n,m,a[N],odd=0,nodd,neven,flag,ans=0;
  22. map <int,int> dic;
  23. void swapeven(int i)
  24. {
  25. // cout <<i<<endl;
  26. while (neven<=m && dic[neven])
  27. neven+=2;
  28. if (neven>m)
  29. flag = 0;
  30. else
  31. dic[a[i]]--,dic[neven]=1,a[i] = neven,neven+=2;
  32. }
  33. void swapodd(int i)
  34. {
  35. while (nodd<=m && dic[nodd])
  36. nodd+=2;
  37. if (nodd>m)
  38. flag = 0;
  39. else
  40. dic[a[i]]--,dic[nodd]=1,a[i] = nodd,nodd+=2;
  41. }
  42. int main()
  43. {
  44. //Open();
  45. Close();//scanf,puts,printf not use
  46. //init??????
  47. cin >> n >> m;
  48. rep1(i,1,n)
  49. {
  50. cin >> a[i];
  51. dic[a[i]]++;
  52. if (a[i]&1) odd++;
  53. }
  54. nodd = 1,neven = 2;
  55. flag = 1;
  56. rep1(i,1,n)
  57. {
  58. if (dic[a[i]]==1) continue;
  59. ans++;
  60. if (odd>n/2)
  61. {
  62. if (a[i]%2==1)
  63. odd--;
  64. swapeven(i);
  65. }
  66. else
  67. if (odd==n/2){
  68. if (a[i]%2==0)
  69. swapeven(i);
  70. else
  71. //a[i]%2==1
  72. swapodd(i);
  73. }
  74. else{
  75. //odd<n/2
  76. if (a[i]%2==0)
  77. odd++;
  78. swapodd(i);
  79. }
  80. }
  81. rep1(i,1,n)
  82. {
  83. if (odd==n/2) break;
  84. if (odd<n/2)
  85. {
  86. if (a[i]%2==0)
  87. {
  88. odd++;
  89. ans++;
  90. swapodd(i);
  91. }
  92. }
  93. else
  94. {
  95. //odd>n/2
  96. if (a[i]%2==1)
  97. {
  98. odd--;
  99. ans++;
  100. swapeven(i);
  101. }
  102. }
  103. }
  104. if (odd!=n/2 || !flag)
  105. return cout<<-1<<endl,0;
  106. cout << ans << endl;
  107. rep1(i,1,n-1)
  108. cout << a[i] << ' ';
  109. cout << a[n]<<endl;
  110. return 0;
  111. }

【codeforces 746E】Numbers Exchange的更多相关文章

  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. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 367C】Sereja and the Arrangement of Numbers

    [题目链接]:http://codeforces.com/problemset/problem/367/C [题意] 我们称一个数列a[N]美丽; 当且仅当,数列中出现的每一对数字都有相邻的. 给你n ...

  4. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【34.57%】【codeforces 557D】Vitaly and Cycle

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

  7. 【55.70%】【codeforces 557A】Ilya and Diplomas

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

  8. 【42.59%】【codeforces 602A】Two Bases

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

  9. 【51.27%】【codeforces 604A】Uncowed Forces

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

随机推荐

  1. 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B】Weakened Common Divisor

    [链接] 我是链接,点我呀:) [题意] 给你n个数对(ai,bi). 让你求一个大于1的数字x 使得对于任意的i x|a[i] 或者 x|b[i] [题解] 求出第一个数对的两个数他们有哪些质因子. ...

  2. 关于ajax异步请求不到数据的问题 302跨域请求

    项目大致问题是这样的 在线咨询模块的数据是通过ajax异步加载来请求到数据,然后动态解析并且显示 前台页面的请求代码 后台action: 另外就是这个项目还有一个登陆权限的认证,如果不登录后台或者登录 ...

  3. A - Jungle Roads

    A - Jungle Roads 思路:并查集的板子,重点是字符的转换,不能忘了加上1. #include<cmath> #include<cstdio> #include&l ...

  4. java的数组index[]方括号内是可以进行算数运算的

    java的数组index[]方括号内饰可以进行算数运算的 如: String[] stringArray = testString.split("\\."); System.out ...

  5. &lt;监听器模式&gt;在C++ 与 Java 之间实现的差异

    前言: 关于各种语言孰优孰劣的讨论在软件界就是个没完没了的话题,今天我决定也来掺和下. 只是我想探讨的不是哪种语言的性能怎样,钱途怎样.而是站在语言本身特性的基础上中肯地比較探讨.由于如今工作用的是C ...

  6. Android顶部粘至视图具体解释

    不知从某某时间開始,这样的效果開始在UI设计中流行起来了.让我们先来看看效果: 大家在支付宝.美团等非常多App中都有使用.要实现这个效果,我们能够来分析下思路: 我们肯定要用2个一样的布局来显示我们 ...

  7. android 虚拟按键是通过哪种机制上报的?

    1.在normal mode下,tp button也是和其他触摸事件一样,以坐标形式的input_event进行上报.在初始化时会通过tpd_button_setting()函数依据定义在tpd_cu ...

  8. 派生类地址比基类地址少4(CDerived对象的起始地址存放的是虚表指针vptr,也就是子类的第一项内容。接下来的是基类的成员变量,接下来再是自身的成员变量)

    大家对虚表并不陌生,都知道每个含有虚函数的类对象都有1个虚指针,但是在现实使用中,却总是因为这而调试半天,才发现原来是虚指针惹的祸.我这几天在调试代码时候也中招了,我的问题是这样的,如下图,CTree ...

  9. js 智能识别收获地址

    项目地址https://github.com/wzc570738205/smart_parse 支持以下数据格式 马云,1351111111,北京市朝阳区姚家园3楼 马云1351111111北京市朝阳 ...

  10. springMVC+uploadify3.1 文件上传 demo

    uploadify3.1 api 可参考:(点击打开链接) 需要springmvc的jar包 1.upload.jsp(主要代码) <script type="text/javascr ...