zoukankan      html  css  js  c++  java
  • Codeforces Round #217 (Div. 2) c题 C. Mittens n手套,手套的颜色m种,任意两个人之间可以交换手套,最多多少人左手和右手的手套颜色不同

    Codeforces Round #217 (Div. 2) c题

    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 ≤ 50001 ≤ m ≤ 100). The second line contains nintegers 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种,任意两个人之间可以交换手套,最多多少人左手和右手的手套颜色不同。

    我还以为是二分匹配来着。。 实际上这里要是 颜色最多的次数*2>n 那么就是 不是这个颜色的人和是这个颜色的人 交换一只手套 答案就是 (n-ans)*2
    否则一定所有的人都可以 满足左右手套颜色不一样

    代码实现:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 int a[5002];
     8 int num[1002];
     9 
    10 int main()
    11 {
    12     int i,n,m,k;
    13     while(~scanf("%d%d",&n,&m))
    14     {
    15         memset(num,0,sizeof(num));
    16         int maxx=0;
    17         for(i=0;i<n;i++)
    18         {
    19             scanf("%d",&a[i]);
    20             num[a[i]]++;
    21             if(num[a[i]]>maxx)
    22                maxx=num[a[i]];
    23         }
    24         int ans;
    25         sort(a,a+n);
    26         if(maxx*2>n)
    27             ans=(n-maxx)*2;
    28         else
    29             ans=n;
    30         printf("%d
    ",ans);
    31         for(i=0;i<n;i++)
    32             printf("%d %d
    ",a[i],a[(i+maxx)%n]);//相同颜色的都在一堆 而最大的一堆的maxx i+maxx就肯定不会是在i所属的那一堆  
    33 
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    十分钟上手-搭建vue开发环境(新手教程)
    如何去掉bootstrap table中表格样式中横线竖线
    进阶攻略|前端最全的框架总结
    进阶攻略|前端完整的学习路线
    2018上半年GitHub上最热门的开源项目
    三分钟教你学会如何将密文解码成明文
    【前端图表】echarts实现散点图x轴时间轴
    IOS应用开发版本控制工具之Versions使用
    从零开始学ios开发(十七):Storyboards(上)
    ios按钮点击后翻转效果
  • 原文地址:https://www.cnblogs.com/assult/p/3485455.html
Copyright © 2011-2022 走看看