Codeforces Round #217 (Div. 2) c题
1 second
256 megabytes
standard input
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.
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).
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.
6 3 1 3 2 2 1 1
6 2 1 1 2 2 1 1 3 1 2 3 1
4 2 1 2 1 1
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题的更多相关文章
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- 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 ...
- 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 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
- Codeforces Round #579 (Div. 3) 套题 题解
A. Circle of Students 题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
- Codeforces Round #786 (Div. 3) 补题记录
小结: A,B,F 切,C 没写 1ll 对照样例才发现,E,G 对照样例过,D 对照样例+看了其他人代码(主要急于看后面的题,能调出来的但偷懒了. CF1674A Number Transforma ...
随机推荐
- Tomcat日志问题
[Tomcat日志设定][tomcat控制台日志][log4j日志] 1 Tomcat 日志概述 Tomcat 日志信息分 为 两 类 : 一是运行中的日志,它主要 记录 运行的一些信息,尤其是一些异 ...
- ARM菜鸟:JLINK与JTAG的区别
调试ARM,要遵循ARM的调试接口协议,JTAG就是其中的一种.当仿真时,IAR.KEIL.ADS等都有一个公共的调试接口,RDI就是其中的一种,那么我们如何完成RDI-->ARM调试协议(JT ...
- linux下安装配置DHCP服务器
前提是已经安装了 core 及 base 两个组 1 2 3 4 5 # cat /etc/redhat-release Red Hat Enterprise Linux Server relea ...
- linux系统识别和挂载文件系统
1. 使用df 命令查看文件系统及相关挂载点信息 [root@server101 ~]# df Filesystem 1K-blocks Used Available Use% Mounted on ...
- Android 实现全屏、无标题栏
实现全屏无标题栏: 1.在xml文件中进行配置 AndroidManifest.xml中,找到需要全屏或设置成无标题栏的Activity,在该Activity进行如下配置即可. 实现全屏效果: and ...
- 感知机(python实现)
感知机(perceptron)是二分类的线性分类模型,输入为实例的特征向量,输出为实例的类别(取+1和-1).感知机对应于输入空间中将实例划分为两类的分离超平面.感知机旨在求出该超平面,为求得超平面导 ...
- usb协议分析-设备描述符配置包-描述符
/* usb协议分析仅供大家参考---设备描述符配置包,设备描述符, 地址设置, 配置描述符, 字符串描述符 */ /* -1- usb设备描述符配置包 */ typedef struct _USB_ ...
- R之批处理
在linux下如何编写脚本调用R语言写的程序呢? R语言进行批处理有2种方式: R CMD BATCH --options scriptfile outputfile Rscript --option ...
- android:descendantFocusability的作用:viewgroup与其上面view的焦点控制,如何让子view失去焦点等。
ViewGroup的下面这个属性可以控制. 原文: android:descendantFocusability Defines the relationship between the ViewGr ...
- Linux Kernel CMPXCHG函数分析
原文地址:http://blog.csdn.net/penngrove/article/details/44175387 最近看到Linux Kernel cmpxchg的代码,对实现很不理解.上网查 ...