Numbers Exchange
题意:
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的更多相关文章
- 【codeforces 746E】Numbers Exchange
[题目链接]:http://codeforces.com/problemset/problem/746/E [题意] 你有n张卡片,上面写着不同的数字; 然后另外一个人有m张上面写着不同的数字的卡片: ...
- Exchange Version and UpdateRollups
Exchange Server 2010 Product name Build number Date KB Microsoft Exchange Server 2010 RTM 14.0.639.2 ...
- Exchange Server and Update Rollup Build Numbers
原文链接https://social.technet.microsoft.com/wiki/contents/articles/240.exchange-server-and-update-rollu ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
- POJ3903:Stock Exchange(LIS)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/E 题目: Description The world ...
- POJ 3903 Stock Exchange
Stock Exchange Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2954 Accepted: 1082 De ...
- POJ1860Currency Exchange(Bellman + 正权回路)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23938 Accepted: 867 ...
- Currency Exchange(Bellman-ford)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21349 Accepted: 765 ...
随机推荐
- 笔记16 C# typeof() & GetType()
C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称:GetType( ...
- nginx源代码分析--配置信息的继承&合并
这里仅仅讲述http{}模块下的配置: 在ngx_http_block()函数内(这个函数别调用时在ngx_inti_cycle内的ngx_conf_parse函数,这个函数遇到http命令时 回调n ...
- 基于EasyIPCamera实现的RTSP跨平台拉模式转发流媒体服务器
本文转自博客:http://blog.csdn.net/xinlanbobo/article/details/53224445 上一篇博客<EasyIPCamera通过RTSP协议接入海康.大华 ...
- mysql的事务隔离级别及其使用场景
1 什么是事务隔离级别 事务隔离指的是事务之间同步关系. 2 食物隔离级别的分类 第一隔离级别,脏读级别 在脏读级别下,第一个事务修改了某个数据,但是还没有提交,第二个事务可以读取到这个未提及的数据. ...
- Netty聊天器(实战一):从0开始实战100w级流量应用
Java 聊天程序(百万级流量实战一):系统介绍 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之14 [博客园 总入口 ] 源码IDEA工程获取链接:Java 聊天室 实战 源码 写在前面 ...
- 初识代码封装工具SWIG(回调Python函数)
这不是我最早使用swig了,之前在写Kynetix的时候就使用了swig为python封装了C语言写的扩展模块.但是当时我对C++还不是很了解,对其中的一些概念也只是拿来直接用,没有理解到底是什么,为 ...
- 建立FTP服务器(FTP服务器名要与创建的用户名一致)
1新建用户 2. 3.建立FTP
- 编写灵活、稳定、高质量的 HTML 和 CSS 代码的规范。
引用地址http://codeguide.bootcss.com/#html-ie-compatibility-mode <!DOCTYPE html> <html lang=& ...
- VVDocument+Appledoc生成文档
在写代码的时候写上适当的注释是一种良好的习惯,方便自己或者别人阅读的方便. **VVDocument**:(Github地址:[VVDocument](https://github.com/onevc ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...