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 ...
随机推荐
- ListView中button监听器 设置 及 优化
在应用开发中常常会用到ListView,而且每个Item里面都会有button之类的须要进行事件监听的控件.在给button加入OnClickListener的时候,一開始非常下意识的会想在ListV ...
- mqtt client python example
This is a simple example showing how to use the [Paho MQTT Python client](https://eclipse.org/paho/c ...
- pyqt5 学习总结
关于基类 一般的文件都会基于QWidget,QtWidgets.QMainWindow 或QDialog,like this class Example(QWidget): QWidget类是所有用户 ...
- 动态控制body最小高度
//动态控制body最小高度 var windowHeight = $(document).height() - 164; $(".body-content").css({ &qu ...
- php返回HTTP状态码
HTTP协议状态码,调用函数时候只需要将$num赋予一个下表中的已知值就直接会返回状态了.<?PHP /** * HTTP Protocol defined status codes* HTTP ...
- spring 过滤器简介
spring 过滤器简介 过滤器放在容器结构的什么位置 过滤器放在web资源之前,可以在请求抵达它所应用的web资源(可以是一个Servlet.一个Jsp页面,甚至是一个HTML页面)之前截获进入的请 ...
- 在Win7创建WiFi热点
1.在开始菜单搜索cmd 2.运行cmd 3.输入以下命令,注意:Your-WiFi-Name和Your-WiFi-Password分别为WiFi名称和密码 netsh wlan set hosted ...
- Delphi 7以来的Delphi 2009测试版新语法特性
我晕,Delphi 7 以后增加了这么多有用的语法,我都不知道.真是越学越觉得自己浅薄,自己所作的Delphi项目所用的知识还不够Delphi知识储备体系的十分之一,更别说Delphi还在继续发展. ...
- NOIP考前感悟
闭关这么久,后来突然后悔自己前几天和暑假的状态很頽 不然进步也还能多一点吧 还好提前发现了,最后也还是努力了一把 也算不枉费自己的选择吧 从初中开始学习OI,到头来也没有什么成果 但还好自己高一 也还 ...
- jQuery局部动态刷新
AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新. 在做项目的过程当中要用到jQuery动态刷新获取数据,上网查了一番教程,讲此知识点的教程很多,但个人感觉比较乱 ...