Codeforces Round #217 (Div. 2) c题 C. Mittens n手套,手套的颜色m种,任意两个人之间可以交换手套,最多多少人左手和右手的手套颜色不同
2013-12-21 20:41
465 查看
Codeforces Round #217 (Div. 2) c题
C. Mittens
time limit per test1 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 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 否则一定所有的人都可以 满足左右手套颜色不一样 代码实现:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int a[5002]; int num[1002]; int main() { int i,n,m,k; while(~scanf("%d%d",&n,&m)) { memset(num,0,sizeof(num)); int maxx=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); num[a[i]]++; if(num[a[i]]>maxx) maxx=num[a[i]]; } int ans; sort(a,a+n); if(maxx*2>n) ans=(n-maxx)*2; else ans=n; printf("%d\n",ans); for(i=0;i<n;i++) printf("%d %d\n",a[i],a[(i+maxx)%n]);//相同颜色的都在一堆 而最大的一堆的maxx i+maxx就肯定不会是在i所属的那一堆 } return 0; }
相关文章推荐
- 正六面体染色 正六面体用4种颜色染色。共有多少种不同的染色样式?要考虑六面体可以任意旋转
- 正六面体染色 正六面体用4种颜色染色。共有多少种不同的染色样式?要考虑六面体可以任意旋转、翻转。
- 有N级的台阶,你一开始在底部,每次可以向上迈最多K级台阶(最少1级),问到达第N级台阶有多少种不同方式。
- 二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。 对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为: ( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根 小易想知道最多可以放多少块蛋糕在网格盒子里。 输入描述: 每组数组包含网格长宽W,
- 在SELECT语句的FROM子句中最多可以指定多少个表或视图,相互之间要用什么分隔,当所查询的表不在当前数据库时,可用什么格式来指出表或视图对象?
- 二、给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。
- 独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。一条独木舟最多只能乘坐两个人,且乘客的总
- 同一个TextView内不同文字颜色、字体、事件可以不同
- js切换不同的div的颜色
- 在arcgis server上发布可以显示不同颜色的交通路况信息。
- 你能描述一下渐进增强和优雅降级之间的不同吗?如果提到了特性检测,可以加分。
- 自定义的Label,可以设置同一Label字体的大小不同,颜色不一
- 一个进程(Process)最多可以生成多少个线程(Thread)
- Excel 最多可以有多少行
- Windows一个文件夹下面最多可以放多少文件
- 让自定义的类型可以和任意的类型之间转换
- web服务器与file服务器不同域(Domain)之间的数据交换
- 对于一个数组{1,2,3}它的子数组有{1,2},{1,3}{2,3},{1,2,3},元素之间可以不是连续的,对于数组{5,9,1,7,2,6,3,8,10,4},升序子序列有多少个?
- Codeforces Round #353 (Div. 2) B. Restoring Painting(方块内放数,问有多少种不同的放法)
- 共有5中颜色的球若干个,取3个球,每个颜色都不同,问有多少种取法;