CF370C.Mittens

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 c__i.

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.

城市 S 举办了一场圣诞派对,共有 nn 名儿童参加。所有儿童都戴着手套。手套的颜色可以各不相同,但每名儿童的左手套与右手套颜色相同。设手套的颜色用 11 到 mm 的整数编号,儿童则用 11 到 nn 的整数编号。那么第 ii 名儿童的两只手套颜色均为 cic_i。

派对上有圣诞老人(俄语中称“严寒老人”)、他的孙女雪姑娘,孩子们围着装饰华丽的圣诞树跳舞。事实上,现场如此绚丽多彩,以至于孩子们希望戴上颜色互不相同的两只手套。于是他们决定互相交换手套,使得最终每名儿童都拥有一只左手套和一只右手套,且这两只手套颜色不同。所有手套尺寸相同,均可适配任意儿童。

孩子们开始随意交换手套,却始终无法达成每人所戴两只手套颜色均不同的局面。其中一名儿童的父亲——瓦西里·彼得罗夫注意到,在一般情况下,孩子们的这一想法可能根本无法实现。此外,作为一名数学家,他设计出了一种手套分配方案,使得拥有颜色互异手套的儿童人数达到最大。你的任务就是重现他的这一发现。注意:左手套与右手套是不同的——每名儿童最终必须恰好拥有一只左手套和一只右手套。

输入格式

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 _c_1, _c_2, ... c__n, where c__i is the color of the mittens of the i-th child (1 ≤ c__i ≤ m).

第一行包含两个整数 nn 和 mm —— 分别表示儿童人数和可能的手套颜色种数(1 ≤ n ≤ 50001 \leq n \leq 5000,1 ≤ m ≤ 1001 \leq m \leq 100)。
第二行包含 nn 个整数 c1, c2, …, cnc_1,\,c_2,\,\dots,\,c_n,其中 cic_i 表示第 ii 个儿童的手套颜色(1 ≤ ci ≤ m1 \leq c_i \leq 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.

第一行输出最终能获得颜色不同的两只手套的儿童的最大人数。接下来的 nn 行中,输出在此情况下手套的分配方案。在这些行中的第 ii 行,输出两个用空格分隔的整数:第 ii 个儿童所获得的左手手套颜色和右手手套颜色。若存在多种解法,输出任意一种即可。

输入输出样例

  • 输入#1

    6 3
    1 3 2 2 1 1

    输出#1

    6
    2 1
    1 2
    2 1
    1 3
    1 2
    3 1
  • 输入#2

    4 2
    1 2 1 1

    输出#2

    2
    1 2
    1 1
    2 1
    1 1

输入解题思路,AI测评打分。不知道怎么写?

首页