【codeforces 746E】Numbers Exchange
【题目链接】: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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 367C】Sereja and the Arrangement of Numbers
[题目链接]:http://codeforces.com/problemset/problem/367/C [题意] 我们称一个数列a[N]美丽; 当且仅当,数列中出现的每一对数字都有相邻的. 给你n ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【35.37%】【codeforces 556C】Case of Matryoshkas
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【55.70%】【codeforces 557A】Ilya and Diplomas
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【42.59%】【codeforces 602A】Two Bases
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【51.27%】【codeforces 604A】Uncowed Forces
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- 00074_Array类
1.Array类的概述 此类包含用来操作数组(比如排序和搜索)的各种方法.需要注意,如果指定数组引用为 null,则访问此类中的方法都会抛出空指针异常NullPointerException. 2.常 ...
- java并发之阻塞队列
在前面我们接触的队列都是非阻塞队列,比如PriorityQueue.LinkedList(LinkedList是双向链表,它实现了Dequeue接口). 阻塞队列与普通队列的区别在于:当队列是空的时, ...
- HDU 2045不easy系列之三LELE的RPG难题(趋向于DP的递推)
不easy系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- Spark SQL with Hive
前一篇文章是Spark SQL的入门篇Spark SQL初探,介绍了一些基础知识和API,可是离我们的日常使用还似乎差了一步之遥. 终结Shark的利用有2个: 1.和Spark程序的集成有诸多限制 ...
- poj2947Widget Factory
对于同余方程的高斯消元啊. 其实也差不多吧.先同一位通分,然后减一下就好了. 主要是判无解和多解的麻烦,需要注意即使有自由元也可能先无解 #include<cstdio> #include ...
- 英语发音规则---R字母
英语发音规则---R字母 一.总结 一句话总结: 1.在词首和词中时,字母r常读作摩擦辅音/r/? red /red/ n. 红色 ruler /'ruːlə/ n. 尺:统治者 rub /rʌb/ ...
- 1. 批量梯度下降法BGD 2. 随机梯度下降法SGD 3. 小批量梯度下降法MBGD
排版也是醉了见原文:http://www.cnblogs.com/maybe2030/p/5089753.html 在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度 ...
- [HDU 5542] The Battle of Chibi
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5542 [算法] 树状数组优化DP [代码] #include<bits/stdc++.h&g ...
- 微信小程序发送模板消息
微信小程序发送模板消息 标签(空格分隔): php 看小程序文档 [模板消息文档总览]:https://developers.weixin.qq.com/miniprogram/dev/framewo ...
- windows上上传代码到Github
Repository name: 仓库名称 Description(可选): 仓库描述介绍 Public, Private : 仓库权限(公开共享,私有或指定合作者) Initialize this ...