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 ≤ ci ≤ m).

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付手套,手套的颜色m种,任意两个人之间可以交换手套,让你求出最多可以使多少人满足左手和右手的手套颜色不同。
分析:当时比赛的时候是用了两个for循环,但第二个for循环是从i+1开始的,所以导致有些情况是漏掉的,如果第二个for循环改成从1开始的话就能过的,学了下别人的方法,具体看代码实现!
代码实现:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int a[],l[],r[],kind[];
int n,m; int main()
{
int i,Max,res;
scanf("%d%d",&n,&m);
memset(kind,,sizeof(kind));
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
kind[a[i]]++;
}
sort(a,a+n);
Max=;
for(i=;i<=m;i++)
if(kind[i]>Max)
Max=kind[i];
if(Max*>n)
res=(n-Max)*;
else
res=n;
for(i=;i<n;i++)
{
l[i]=a[i];
r[i]=a[(i+Max)%n];
}
printf("%d\n",res);
for(i=;i<n;i++)
printf("%d %d\n",l[i],r[i]);
return ;
}

Codeforces Round #217 (Div. 2) c题的更多相关文章

  1. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  2. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  3. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  4. Codeforces Round #552 (Div. 3) A题

    题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...

  5. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  7. Codeforces Round #425 (Div. 2))——A题&&B题&&D题

    A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...

  8. Codeforces Round #579 (Div. 3) 套题 题解

    A. Circle of Students      题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...

  9. Codeforces Round #786 (Div. 3) 补题记录

    小结: A,B,F 切,C 没写 1ll 对照样例才发现,E,G 对照样例过,D 对照样例+看了其他人代码(主要急于看后面的题,能调出来的但偷懒了. CF1674A Number Transforma ...

随机推荐

  1. CDC ->> 在某个SQL Server中开启CDC功能

    USE AdventureWorks;GO--开启某个数据库的CDC功能EXEC sys.sp_cdc_enable_db;GO--is_cdc_enabled栏位为1代表开启CDC功能了SELECT ...

  2. Grunt :任务自动管理工具

    来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 在Javascript的开发过程中,经常会遇到一些重复性的任务,比如合并文件.压缩代码.检查语法错误.将Sass代码 ...

  3. 在Visual C++下搭建OpenGL的开发环境

    1.确保你的电脑已经安装了visual c++编译器 如果还没安装的话,这里有个安装包,可以复制链接进行下载:http://pan.baidu.com/s/1bn4XTqn   2.下载GLUT 下载 ...

  4. 用xshell操作linux系统的常用命令

    (1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 ls -l *.doc 给出当前目录下以. ...

  5. windows服务异常值framework版本

    sc query WCService | find /i "state"if %errorlevel% == 0 ( net start "WCService" ...

  6. 3D volume texture和cube map

    cube map texture可以理解为6个面的纸盒, sample的时候使用vector射线型的sample. volume texture可以理解是一摞2D texture,sample的时候用 ...

  7. 【温故知新】c#事件event

    从上一篇文章[温故知新]C#委托delegate可知,委托delegate和事件Event非常的相似,区别就是event关键字,给delegate穿上了个“马甲”. 让我们来看官方定义: 类或对象可以 ...

  8. JQuery_给元素添加或删除类等以及CSS()方法

    一.addClass() - 向被选元素添加一个或多个类 <script src="jquery-1.11.1.min.js"></script> < ...

  9. Machine Learning for hackers读书笔记(十二)模型比较

    library('ggplot2')df <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\12-Model_C ...

  10. easyui tree 模仿ztree 使用扁平化加载json

    1,载入扩展JS //作者孙宇 //自定义loadFilter的实现 $.fn.tree.defaults.loadFilter = function (data, parent) { var opt ...