题意:

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. Linux中du和df

    Linux运维过程中,常常发现du和df返回值不一样,偶尔会发现区别非常大. 特定情况下,可能df看到磁盘已满,可是du推断磁盘剩余空间非常大. 文件系统分配当中的一些磁盘块用来记录它自身的一些数据. ...

  2. kubernetes滚动更新

    系列目录 简介 当kubernetes集群中的某个服务需要升级时,传统的做法是,先将要更新的服务下线,业务停止后再更新版本和配置,然后重新启动并提供服务.如果业务集群规模较大时,这个工作就变成了一个挑 ...

  3. 怎样退出App之前唤醒还有一个App?

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 SDK并没 ...

  4. python的id()函数的一个小方面(转载)

    >>> a = 2 >>> b = 2 >>> id(a) 21132060 >>> id(b) 21132060 >&g ...

  5. codeforces Looksery Cup 2015 H Degenerate Matrix

    The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...

  6. LeetCode(82)题解: Remove Duplicates from Sorted List II

    https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目: Given a sorted linked list, ...

  7. opencv中的子库

    1 FLANN 近似最近邻库,NN就是nearest neighbor的缩写. 2 IlmImf Ilm是Industrial light & magic公司的缩写. Imf是image fo ...

  8. 基于Netty自研网关中间件

    微服务网关解决方案调研和使用总结 专题 - 沧海一滴 - 博客园 https://www.cnblogs.com/softidea/p/7261095.html 宜人贷蜂巢API网关技术解密之Nett ...

  9. EL表达式 介绍

    EL表达式      1.EL简介 1)语法结构        ${expression} 2)[]与.运算符      EL 提供.和[]两种运算符来存取数据.      当要存取的属性名称中包含一 ...

  10. 在Qt Creator中创建C++工程并使用CMake构建项目

    创建完毕后,若电脑上没有安装CMake,则无法构建工程, 我用的是绿色版,官网下载地址:https://cmake.org/files/v3.10/cmake-3.10.1-win64-x64.zip ...