题意:

Eugeny有n张卡片,他希望和Nikolay交换一些卡片使得他拥有的奇数数字和偶数数字的卡片数目一样,且所有数字都不同。

Nikolay有m张卡片,分别写着1到m。问最少交换几次,能够满足要求。并输出交换后的结果。无解输出-1,多解任意输出一组。

解法:

贪心,首先用没有出现过的数字填好重复出现的数字,并顺便尽量让odd,even差值较小。

然后贪心用没出现的数字填即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set> #define N 200010 using namespace std; int n, m, a[N], j = , k = ;
set<int> mp, Tp; int get(int x)
{
if(x == )
{
while(j <= m && mp.count(j)) j += ;
if(j <= m) return j;
else return ;
}
else
{
while(k <= m && mp.count(k)) k += ;
if(k <= m) return k;
else return ;
}
} int main()
{
scanf("%d %d", &n, &m);
int cnt0 = , cnt1 = , ans = ;
for(int i = ;i <= n;i++)
{
scanf("%d", &a[i]);
if(a[i] % == ) cnt0++;
else cnt1++;
mp.insert(a[i]);
}
// cout << cnt0 << ' ' << cnt1 << endl;
for(int i = ;i <= n;i++)
{
if(Tp.count(a[i]))
{
if(a[i]&) cnt1--;
else cnt0--;
int tmp0 = get(), tmp1 = get();
if(cnt0 < cnt1 && tmp0) a[i] = tmp0;
else if(cnt1 < cnt0 && tmp1) a[i] = tmp1;
else if(tmp0) a[i] = tmp0;
else if(tmp1) a[i] = tmp1;
else
{
puts("-1");
return ;
}
ans++;
mp.insert(a[i]);
if(a[i]&) cnt1++;
else cnt0++;
}
Tp.insert(a[i]);
}
// cout << "ans = " << ans << endl;
// for(int i = 1;i <= n;i++) cout << a[i] << ' ';
// cout << endl << cnt0 << ' ' << cnt1 << endl;
for(int i = ;i <= n && cnt0 != cnt1;i++)
{
if(cnt0 < cnt1 && a[i] % == )
{
cnt0++;
cnt1--;
int tmp = get();
if(tmp) a[i] = tmp, ans++;
else
{
puts("-1");
return ;
}
}
if(cnt1 < cnt0 && a[i] % == )
{
cnt0--;
cnt1++;
int tmp = get();
if(tmp) a[i] = tmp, ans++;
else
{
puts("-1");
return ;
}
}
mp.insert(a[i]);
}
cout << ans << endl;
for(int i = ;i <= n;i++) cout << a[i] << ' ';
cout << endl;
return ;
}

Numbers Exchange的更多相关文章

  1. 【codeforces 746E】Numbers Exchange

    [题目链接]:http://codeforces.com/problemset/problem/746/E [题意] 你有n张卡片,上面写着不同的数字; 然后另外一个人有m张上面写着不同的数字的卡片: ...

  2. Exchange Version and UpdateRollups

    Exchange Server 2010 Product name Build number Date KB Microsoft Exchange Server 2010 RTM 14.0.639.2 ...

  3. Exchange Server and Update Rollup Build Numbers

    原文链接https://social.technet.microsoft.com/wiki/contents/articles/240.exchange-server-and-update-rollu ...

  4. POJ1860 Currency Exchange(bellman-ford)

    链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...

  5. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  6. POJ3903:Stock Exchange(LIS)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/E 题目: Description The world ...

  7. POJ 3903 Stock Exchange

    Stock Exchange Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2954   Accepted: 1082 De ...

  8. POJ1860Currency Exchange(Bellman + 正权回路)

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23938   Accepted: 867 ...

  9. Currency Exchange(Bellman-ford)

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21349   Accepted: 765 ...

随机推荐

  1. 我的Android进阶之旅------&gt;解决:Execution failed for task &#39;:app:transformResourcesWithMergeJavaResForDebug&#39;.

    错误描写叙述 今天在Android Studio项目中添加了jackson的开发包,编译执行时候.引发了例如以下的错误: Error:Execution failed for task ':app:t ...

  2. python(34)- 模块与包练习

    创建如下目录结构 keystone/ ├── __init__.py └── auth     ├── __init__.py     └── plugins         └── core.py ...

  3. AngularJs 常用

    Angularjs开发一些经验总结:http://www.cnblogs.com/whitewolf/archive/2013/03/24/2979344.html 七步从AngularJS菜鸟到专家 ...

  4. hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)

    题目:pid=5318">http://acm.hdu.edu.cn/showproblem.php?pid=5318 题意:给定n个数字串和整数m,规定若数字串s1的后缀和数字串s2 ...

  5. Active Directory组织单位(Organizational Unit)操作汇总

    前言 本章聊Active Directory的组织单位(OU)的新增.修改.移动等操作,使用.NET Framework 为我们提供的System.DirectoryServices程序集. 不积跬步 ...

  6. java 回文字符串

    package string.string1_5; public class Palindrome { /** * 从两头向中间移动 * @param str * @return */ public ...

  7. centos编译 Compiling FFmpeg on CentOS RHEL Fedora

    This guide is based on a minimal installation of the latest CentOS release, and will provide a local ...

  8. Android Material Design 中文版

    http://www.google.com/design/spec/animation/authentic-motion.html http://www.oschina.net/question/14 ...

  9. Drawing Images and Text

    using System;using UIKit;using Foundation;using CoreGraphics;namespace GraphicsAnimation{ public cla ...

  10. php 面向对象的三大要素(封装、继承、多态)以及重写(override)和重载(overload)的举例说明

    PHP是一种HTML内嵌式的,用来制作动态网页的服务器端的脚本语言.其特点是:开发周期短,稳定安全,简单易学,免费开源,良好的跨平台特性.PHP是一种面向对象的程序设计语言,目前已成为全球最受欢迎的五 ...