【题目链接】:http://codeforces.com/problemset/problem/746/E

【题意】



你有n张卡片,上面写着不同的数字;

然后另外一个人有m张上面写着不同的数字的卡片:卡片上的数字从1..m;

你可以和另外一个人交换卡片;

问你最少的交换次数;

使得你的n张卡片里面,卡片上的数字为奇数的和卡片上的数字为偶数的张数相同.

且这n张卡片不能有相同的数字;

【题解】



首先考虑去重的工作;

在去重之前;先算出;

原来的n张卡片里面,卡片上的数字是奇数的数字个数odd;

然后两个变量nexto和nexte分别表示下一个没被交换的奇数和偶数(1..m里面);

对于重复出现的卡片;

看看odd和n/2的关系;

如果



odd>n/2

则不能再来奇数了,所以只能拿一张偶数的和它交换(不管重复的这张的奇偶性);

(只是如果是奇数的,则奇数张数递减);



odd==n/2

则拿一张和这个数字奇偶性相同的卡片来交换;



odd< n/2

则不能来偶数了,需要拿一张奇数的卡片和它交换(仍旧不管重复的这张的奇偶性如何,都是拿一张奇数的)

这张重复的是偶数的话,odd++;

去重结束之后;

再根据odd和n/2的关系大小贪心换每一个数字;

如果

①odd< n/2

且遇到了一个偶数;

则拿一个奇数来和它换,odd++

②odd>n/2

且遇到了一个奇数

则拿一个偶数和它换,odd–

注意在换的时候,要保证,换过之后,序列中不会有相同的数字(即换完那一瞬间不能有相同的数字,也即换的那个数字不能和序列中的其他元素相同)



【Number Of WA】



2



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100; int n,m,a[N],odd=0,nodd,neven,flag,ans=0;
map <int,int> dic; void swapeven(int i)
{
// cout <<i<<endl;
while (neven<=m && dic[neven])
neven+=2;
if (neven>m)
flag = 0;
else
dic[a[i]]--,dic[neven]=1,a[i] = neven,neven+=2;
} void swapodd(int i)
{
while (nodd<=m && dic[nodd])
nodd+=2;
if (nodd>m)
flag = 0;
else
dic[a[i]]--,dic[nodd]=1,a[i] = nodd,nodd+=2;
} int main()
{
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n >> m;
rep1(i,1,n)
{
cin >> a[i];
dic[a[i]]++;
if (a[i]&1) odd++;
}
nodd = 1,neven = 2;
flag = 1; rep1(i,1,n)
{
if (dic[a[i]]==1) continue;
ans++;
if (odd>n/2)
{
if (a[i]%2==1)
odd--;
swapeven(i);
}
else
if (odd==n/2){
if (a[i]%2==0)
swapeven(i);
else
//a[i]%2==1
swapodd(i);
}
else{
//odd<n/2
if (a[i]%2==0)
odd++;
swapodd(i);
}
}
rep1(i,1,n)
{
if (odd==n/2) break;
if (odd<n/2)
{
if (a[i]%2==0)
{
odd++;
ans++;
swapodd(i);
}
}
else
{
//odd>n/2
if (a[i]%2==1)
{
odd--;
ans++;
swapeven(i);
}
}
}
if (odd!=n/2 || !flag)
return cout<<-1<<endl,0;
cout << ans << endl;
rep1(i,1,n-1)
cout << a[i] << ' ';
cout << a[n]<<endl;
return 0;
}

【codeforces 746E】Numbers Exchange的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 367C】Sereja and the Arrangement of Numbers

    [题目链接]:http://codeforces.com/problemset/problem/367/C [题意] 我们称一个数列a[N]美丽; 当且仅当,数列中出现的每一对数字都有相邻的. 给你n ...

  4. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【55.70%】【codeforces 557A】Ilya and Diplomas

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【42.59%】【codeforces 602A】Two Bases

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. jQuery选择器练习中,带空格和不带空格的问题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. J - Borg Maze

    J - Borg Maze 思路:bfs+最小生成树. #include<queue> #include<cstdio> #include<cstring> #in ...

  3. muduo总结

    总结说的有的过大,算是对自己学习的一个总结.兴许会不断补充. 模型总结 muduo是基于非堵塞的IO和事件驱动的网络库. muduo的总体结构时one loop per thread+threadpo ...

  4. ios基础-分辨率适配

    (一)分辨率定义 分辨率,是指单位长度内包括的像素点的数量,它的单位通常为像素/英寸(ppi).描写叙述分辨率的单位有:(dpi点每英寸).lpi(线每英寸)和ppi(像素每英寸). (二)ios分辨 ...

  5. C语言:一个涉及指针函数返回值与printf乱码、内存堆栈的经典案例

    一个奇怪的C语言问题,涉及到指针.数组.堆栈.以及printf.以下实现: 整数向字符串的转换,返回字符串指针,并在main函数中调用printf显示. #include<stdio.h> ...

  6. Oracle数据的基本操作

    一.什么是Oracle 在学习DRP系统之前,非常多次提到过Oracle,也了解过,那么Oracle是什么?今天我最终揭开了它的神奇面纱. Oracle:是一个公司.当然我在这里说的是Oracle数据 ...

  7. [SCOI 2007] 排列

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1072 [算法] 状压DP [代码] #include<bits/stdc++. ...

  8. [Tomcat]Tomcat安全设置

    1.关闭服务器端口:server.xml默认有下面一行: <Server port="8005" shutdown="SHUTDOWN"> 这样允许 ...

  9. Linux命令学习-curl

    作用 curl是利用URL语法的一款强大的网络工具,你可以使用它完成上传下载文件等操作. curl http://www.cnblogs.com 上诉的命令即可将页面内容打印到屏幕上. 常用参数 -o ...

  10. 本地PC安裝Centos 6.5 操作手冊

    http://www.xlgps.com/article/130038.html 一.准备工作 1.下载Centos6.5 ISO文件 我在官网上下的6.5版本CentOS-6.5-x86_64-bi ...