【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 ...
随机推荐
- 【ACM-ICPC 2018 南京赛区网络预赛 I】Skr
[链接] 我是链接,点我呀:) [题意] 让你把一个字符串的所有回文子串加起来.(当做数字加起来) 求他们的和. [题解] 回文树. 从两个根节点分别遍历整棵回文树. 按照每个节点的定义. 得到每个节 ...
- SGU 185 Two shortest
Two shortest Time Limit: 500ms Memory Limit: 4096KB This problem will be judged on SGU. Original ID: ...
- spring的关于数据源的datasource接口的深入理解
1.DataSource的接口这是一个spring接口,可以获取数据库的Connection.是标准化的,取得连接的一种方式. 默认市面上有两个数据库连接池实现了spring的datasource接口 ...
- Ural 1152 False Mirrors(状压DP)
题目地址:space=1&num=1152">Ural 1152 初学状压DP,原来状压仅仅是用到了个位运算.. 非常水的状压DP.注意四则运算的优先级是高于位运算的..也就是 ...
- Spring整合TimerTask实现定时任务调度
一. 前言 近期在公司的项目中用到了定时任务, 本篇博文将会对TimerTask定时任务进行总结, 事实上TimerTask在实际项目中用的不多, 由于它不能在指定时间执行, 仅仅能让程序依照某一个频 ...
- python绘制caffe中网络模型
caffe-master/python/draw_net.py 实现绘制caffe中定义的网络模型功能,将.prototxt文件可视化. 需要先安装pydot和protobuf工具 通过Anacond ...
- 2017-3-3 leetcod 1 35 448
ACM退役了,接下来是考研的准备,刷刷leetcode保证不会生手,也算是调剂生活,初步计划是每天三题吧,希望可以坚持下去. 打算按照专题来做,先是Array....本来以为特别水,结果.... == ...
- QT-解除connect
前言:解除关联. 一.新建工程 二.新建部件 在ui设计界面拖入一个line edit,一个label以及两个button按钮 右键“关联”按钮转到槽,选择clicked(),添加如下代码: void ...
- [实例]ROS使用OpenCV读取图像并发布图像消息在rviz中显示
思路: (1)使用opencv读取本地图像 (2)调用cv_bridge::CvImage().toImageMsg()将本地图像发送给rviz显示 一.使用opencv读取本地图像并发布图像消息 ( ...
- php开启CURL支持
window下安装php_curl支持 1. 找到php.ini 修改extension=php_curl.dll 把前面的分号去掉2. 把 php_curl.dll libeay32.dll ssl ...