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. React 万能的函数表达式

    一.语法简介 表达式可以以下两种方法, (1)(function A(){})(this),(this)参数在函数外面 (2)(function B(){}(this)),(this)参数在函数里面 ...

  2. [转]ArcGIS计算图斑的四邻坐标(XMin,XMax,YMin,YMax)

    1.背景: 在国土,调查等行业业务里面经常有需要计算某个图斑的四邻坐标,即xmax,xmin,ymin,ymax;也就是常说的MBR(最小外包矩形),本教程演示如何计算一个shapefile文件上的图 ...

  3. javascript 网络是否连接的几种方案

    js   网络是否连接的几种方案 1.通过html5的新属性: window.onload = function () {            var isOnLine = navigator.on ...

  4. C#基础(二)

    变量的命名规则: 1.变量名由字母,数字 ,下划线组成.@ 中文 2.首字母,不能是数字 3.不能与关键字重名 变量的定义 语法: 数据类型 变量名[=值]; int a;      //只定义变量 ...

  5. postgreSQLG关闭活动的connection、删除活动的数据库

    First, find the activities that are taken place against the target database, you can query thepg_sta ...

  6. 开发板上修改时间方法date命令【转】

    转自:http://blog.chinaunix.net/uid-25906157-id-3138157.html 之前使用开发板时发现每次开机后,系统的时间都恢复到初始状态1970年. 一. 先说一 ...

  7. 《Linux/Unix系统编程手册》读书笔记 目录

    <Linux/Unix系统编程手册>读书笔记1  (创建于4月3日,最后更新4月7日) <Linux/Unix系统编程手册>读书笔记2  (创建于4月9日,最后更新4月10日) ...

  8. 感谢大家的支持,发布一个JWFD的补丁文件

    请用这个文件覆盖原来的JWFD开发包里面的同名文件,然后删除JWFD目录下面的那个FLOWTREE.OBJ 文件 然后重启JWFD.... 这个补丁修正了  流程图设计器-树型列表的几个BUG,因为有 ...

  9. pyhton与json,Xml

    对简单数据类型的encoding 和 decoding: 使用简单的json.dumps方法对简单数据类型进行编码,例如: 1 2 3 4 5 6 import json   obj = [[1,2, ...

  10. [HDOJ2639]Bone Collector II(第k优01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:求01背包的第k优解 dp(i, j)表示容量为j时的i优解 对于第二维的操作和01背包几 ...