CodeForces 370C. Mittens
1 second
256 megabytes
standard input
standard output
A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th child has both mittens of color ci.
The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children.
The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.
The first line contains two integers n and m — the number of the children and the number of possible mitten colors (1 ≤ n ≤ 5000, 1 ≤ m ≤ 100). The second line contains n integers c1, c2, ... cn, where ci is the color of the mittens of the i-th child (1 ≤ ci ≤ m).
In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th child will get. If there are multiple solutions, you can print any of them.
6 3
1 3 2 2 1 1
6
2 1
1 2
2 1
1 3
1 2
3 1
4 2
1 2 1 1
2
1 2
1 1
2 1
1 1
若哪个颜色的手套大于n/2肯定不能换成每个人都不同。。。。若所有颜色的手套都小于n/2则每个人留着一个手套在和与自己相聚n/2的人换一个手套即可。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,m,a[5500]; int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
int cnt=0;
for(int i=0;i<n;i++) cnt+=(a[i]!=a[(i+(n/2))%n]);
cout<<cnt<<endl;
for(int i=0;i<n;i++) cout<<a[i]<<" "<<a[(i+(n/2))%n]<<endl;
return 0;
}
CodeForces 370C. Mittens的更多相关文章
- 【codeforces 370C】Mittens
[题目链接]:http://codeforces.com/problemset/problem/370/C [题意] 给你n个人,每个人都有一双相同颜色的手套; 然允许在所有人之间交换手套; (每个人 ...
- CodeForces 370C
这道题其实是挺简单的.首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套.反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不 ...
- Codeforce 370C Mittens 巧妙数学题
这道题目我一开始想错了,觉得只要排好序,再从头到尾把可以相互交换的进行下交换就可以了...事实证明是错的.正确的解法比较巧妙,而且写法非常好,值得学习 首先,要注意的一个规律是,假如最大的颜色数字出现 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Python 模块续和面向对象的介绍(六)
一.基本模块 shutil 文件.目录.压缩包的处理模块 shutil.copyfile(src, dst) 拷贝文件 >>> shutil.copyfile('a.log','b. ...
- JVM -- 类加载
学习自周志明老师的<深入理解Java虚拟机>第二版 类的加载时机 如上图所示: 类从被加载到虚拟机内存中开始,直到卸载出内存为止,它的整个生命周期包括了: 加载.验证.准备.解析.初始化. ...
- 如何用 new 来动态开辟一个二维数组
一般的做法是: int **p = new int*[m]; //m行n列型 for (i = 0; i < m; ++i) { p[i] = new int[n]; for (j = 0; j ...
- 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...
- Altera FPGA中的pin进一步说明
最近END china上的大神阿昏豆发表了博文 <FPGA研发之道(25)-管脚>,刚好今天拿到了新书<深入理解Altera FPGA应用设计>第一章开篇就讲pin.这里就两者 ...
- 使用 getopt() 进行命令行处理
引言 在早期的 UNIX® 中,其命令行环境(当时的唯一用户界面)包含着数十种小的文本处理工具.这些工具非常小,通常可很好地完成一项工作.这些工具通过较长的命令管道链接在一起,前面的程序将其输出传递给 ...
- IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例
IOS开发中UIBarButtonItem上按钮切换或隐藏案例实现案例是本文要介绍的内容,这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add bu ...
- Column store index 列数据如何匹配成行数据?
SQL Server 2012引入了列存储索引,对每列的数据进行分组和存储,然后联接所有列以完成整个索引.这不同于传统索引,传统索引对每行的数据进行分组和存储,然后联接所有行以完成整个索引. 在访问基 ...
- ARM标准汇编与GNU汇编
ARM标准汇编与GNU汇编 http://www.cnblogs.com/hnrainll/archive/2011/05/17/2048315.html
- python 函数形参四种格式
1:f(a,b) 2:f(a,b=value)有默认值的参数必须在后面 3:f(*a)多个参数直接传进一个元组 4:f(**a)以keys=values 形式给参数,传入转换为字典 def test( ...