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. 看完这些,你就算得上既了解围棋又了解alphago了

    首先,我们要祝贺小李下出第78手的“神之一手”,这一手堪称前无古人后无来者,尤其是结合了阿尔法狗自暴自弃的表现.小李说过他的失败并不是人类的失败,同样,小李的胜利也只是属于他一人的胜利. 然而人类在围 ...

  2. STL:string 大小(Size)和容量(Capacity)

    strings存在三种“大小”: 1.size()和length() 返回string中现在的字符个数.上述两个函数等效. 成员函数empty()用来检验字符数是否为0,亦即字符串是否为空.你应该优先 ...

  3. Gray code---hdu5375(格雷码与二进制码,普通dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题意就是:给你一串二进制码,里面可能含有'?'这个既可以表示0又可以表示1, 让我们把这个二进制 ...

  4. C# winform窗体假死

    C#  winform窗体假死 我们经常会遇到当执行一个比较大的函数时,窗体会出现假死的现象,给用户的体验不是很好,于是我们遇到了问题,那么就必须解决,我们该如何解决呢,首先在自己的脑里画个问号,接下 ...

  5. ActionScript 设置元件色彩属性

        var clr:Color = new Color(mc);     var ct:Object = {ra:100, rb:0, ga:100, gb:0, ba:100, bb:0, aa ...

  6. Fedora 15 KDE中如何打开software management及如何应用

    Fedora 15 KDE中如何打开software management级如何应用 software management中有转载和卸载软件(Get and remove software)的功能 ...

  7. JS方法

    1.方法可作为对象使用 function aa() { this.a = "aaaa"; this.b = 23; this.f = function () { alert(thi ...

  8. hihoCoder 1082然而沼跃鱼早就看穿了一切 (字符串处理)

    http://hihocoder.com/problemset/problem/1082 首先将字符串全部字母变成小写,不断用find查找字符串中的Marshtomp,并把每个字符变为’#‘ ,最后统 ...

  9. linux 查看程序是否运行

    命令格式:ps -ax|grep program_name 如查看包含python的程序是否运行: ps -ax|grep python ? Sl : python ToServer.py pts/ ...

  10. 使用jQuery插件PrintArea进行html页面打印

    在开发系统时,我们一般会有统计分析的功能需求,而现在大多数系统都是使用B/S的形式开发. 所以,html报表打印,并且打印指定内容,成了必要的功能. 开源地址:https://github.com/R ...