【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 ...
随机推荐
- bzoj 1266 1266: [AHOI2006]上学路线route
1266: [AHOI2006]上学路线route Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2356 Solved: 841[Submit][S ...
- Spring Boot 第一个demo
Sring boot 一直没有使用过,跳槽来到新公司,暂时没有事情就学习一下. Spring boot 这里采用的是maven 来创建的 maven项目的pom.xml 文件 <?xml v ...
- idea安装Jerebel 与使用
在File->setting->plugins->下选择Browse repositories下搜索JRebel Plugin 下载,下载完成之后重启idea. 重启完成后,可见在工 ...
- what we benefit from big data
大数据玩的是什么,趋势,故障,寿命? 物联网拉动的是终端厂商的销量,作为终端设备生产商(OEM).无论是汽车.手机.家电行业.最有理由推动物联网的普及,可是作为传统行业,玩"网"并 ...
- 使用UE4公布安卓平台游戏
使用了几天的UE4 ,总算是将游戏在安卓平台执行起来了.当中遇到非常多问题,而且终于依旧有一些问题没能解决. 整体感觉是UE4这款引擎眼下还不够成熟.问题较多. 没有unity使用起来方便. 可是既然 ...
- Hit 2255 Not Fibonacci
今天下午刚起来眼睛就比較涨,,并且还有点恶心,唉.结果一直不在状态.并且这个题太坑了.. .. 点击此处即可传送 Hit 2255 Maybe ACMers of HIT are always fon ...
- Linux 网卡驱动学习(六)(应用层、tcp 层、ip 层、设备层和驱动层作用解析)
本文将介绍网络连接建立的过程.收发包流程,以及当中应用层.tcp层.ip层.设备层和驱动层各层发挥的作用. 1.应用层 对于使用socket进行网络连接的server端程序.我们会先调用socket函 ...
- DirectX11 学习笔记1 - 第一个程序
为了加快学习速度,和使程序更加easy理解. 我把sampler tutorial里面的一个样例 的固定代码和常常修改的代码经过简单的类的封装了一下. 以后学习的时候就能够仅仅在还有一个文件写ren ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第一步---開始界面&关卡选择
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦.他说:随便写,第一别全然照搬代码:第二能够说 ...
- iOS-UIApplication详解
UIApplication简介 UIApplication对象是应用程序的象征. 每一个应用程序都有自己的UIApplication对象,而且是单例. 一个iOS程序启动后创建的第一个对象就是UIAp ...