题意:

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. Python--常用模块部分

    模块 pip install #模块名称 #安装模块 #导入模块 from collections import namedtuple collections模块 提供了几个额外的数据类型: Coun ...

  2. Flatify分页

    Flatify分页:<ul uib-pagination total-items="siteCount" items-per-page="1" max-s ...

  3. jQuery UI Autocomplete是jQuery UI的自动完成组件

    支持的数据源 jQuery UI Autocomplete主要支持字符串Array.JSON两种数据格式. 普通的Array格式没有什么特殊的,如下: ? 1 ["cnblogs" ...

  4. Android版App的控件元素定位

    前言 如何获取页面上各控件元素,无论是Web自动化还是App自动化,此步骤都是非常关键的! Web页面的控件元素可通过开发者选项(Chrome浏览器的F12)来协助定位,App端也是有相应的工具来协助 ...

  5. Scrapy爬虫入门系列1 安装

    安装python2.7 参见CentOS升级python 2.6到2.7 安装pip 参见CentOS安装python setuptools and pip‎ 依赖 https://docs.scra ...

  6. MapReduce 1工作原理图文详解

    MapReduce工作原理图文详解 一 MapReduce程序执行流程 程序执行流程图如下: 流程分析:1.在客户端启动一个作业.2.向JobTracker请求一个Job ID.3.将运行作业所需要的 ...

  7. C# WPF DataGrid控件实现三级联动

    利用DataGrid控件实现联动的功能,在数据库客户软件中是随处可见的,然而网上的资料却是少之又少,令人崩溃. 本篇博文将介绍利用DataGrid控件模板定义的三个ComboBox实现“省.市.区”的 ...

  8. EasyDarwin开源流媒体服务器进行RTSP转发过程中将sdp由文件存储改成内存索引

    -本篇由团队成员Fantasy供稿! 原始版本 在Darwin Streaming Server版本中,推送端DoAnnounce的时候后服务器会根据easydarwin.xml中配置的movies_ ...

  9. vmware安装操作系统

    安装的时候,直接选择“从光盘或者映像安装”就可以了,很方便的.不需要再去从u盘什么的安装了.

  10. SVD分解的理解

    对称阵A 相应的,其对应的映射也分解为三个映射.现在假设有x向量,用A将其变换到A的列空间中,那么首先由U'先对x做变换: 由于正交阵“ U的逆=U‘ ”,对于两个空间来讲,新空间下的“ 基E' 坐标 ...