C. Mittens
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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 ≤ cim).

Output

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.

Sample test(s)
input
6 3
1 3 2 2 1 1
output
6
2 1
1 2
2 1
1 3
1 2
3 1
input
4 2
1 2 1 1
output
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的更多相关文章

  1. 【codeforces 370C】Mittens

    [题目链接]:http://codeforces.com/problemset/problem/370/C [题意] 给你n个人,每个人都有一双相同颜色的手套; 然允许在所有人之间交换手套; (每个人 ...

  2. CodeForces 370C

    这道题其实是挺简单的.首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套.反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不 ...

  3. Codeforce 370C Mittens 巧妙数学题

    这道题目我一开始想错了,觉得只要排好序,再从头到尾把可以相互交换的进行下交换就可以了...事实证明是错的.正确的解法比较巧妙,而且写法非常好,值得学习 首先,要注意的一个规律是,假如最大的颜色数字出现 ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. Sereja and Bottles

    http://codeforces.com/problemset/problem/315/A 题目意思是第ai的瓶子能开bi的瓶子.给你相应的数据,求无法用其他瓶子打开的数量(即需要外力). 一开始看 ...

  2. 2014Esri全球用户大会之标准和互操作

    1.   ArcGIS是一个开放协作的平台吗?Esri是怎样看待"开放"的? 是的,ArcGIS是一个开放协作的平台.Esri公司致力于支持我们的用户更加easy的在异构环境下工作 ...

  3. [转]tripwire-文件指纹

    原文链接:http://www.ipython.me/centos/tripwire-file-md5.html Tripwire是目前最为著名的unix下文件系统完整性检查的软件工具,这一软件采用的 ...

  4. python变量传递给系统命令的方法

    python程序内执行shell命令可以有几种方式,在http://www.cnblogs.com/xuxm2007/archive/2011/01/17/1937220.html 里都有详细介绍. ...

  5. JVM--常见的虚拟机回收算法

    Serial GC -XX:+UseSerialGC 这样就设置为了串行GC回收方式,这种方式在Client模式下是默认的. 一般是使用在单机程序,小内存,CPU个数少的机器上. 没有复杂的算法,只用 ...

  6. Java数据流格式转换

    1 字节流InputStream                  ->FileInputStreamOutputStream                 ->FileOutputSt ...

  7. json datetime转换问题

    我用Newtonsoft.Json.dll转换成json,这次是把一个集合转换成json,这个集合里有个DateTime类型的数据,转换完成后会变成/Date(1286375605000+0800)/ ...

  8. Android圆形图片自定义控件

    Android圆形图片控件效果图如下: 代码如下: RoundImageView.java package com.dxd.roundimageview; import android.content ...

  9. MONGODB的内部构造 FROM 《MONGODB THE DEFINITIVE GUIDE》

    今天下载了<MongoDB The Definitive Guide>电子版,浏览了里面的内容,还是挺丰富的.是官网文档实际应用方面的一个补充.和官方文档类似,介绍MongoDB的内部原理 ...

  10. Python 数据分析(一) 本实验将学习 pandas 基础,数据加载、存储与文件格式,数据规整化,绘图和可视化的知识

    第1节 pandas 回顾 第2节 读写文本格式的数据 第3节 使用 HTML 和 Web API 第4节 使用数据库 第5节 合并数据集 第6节 重塑和轴向旋转 第7节 数据转换 第8节 字符串操作 ...