CodeForces 370C. Mittens
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/2肯定不能换成每个人都不同。。。。若所有颜色的手套都小于n/2则每个人留着一个手套在和与自己相聚n/2的人换一个手套即可。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,m,a[5500]; int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
int cnt=0;
for(int i=0;i<n;i++) cnt+=(a[i]!=a[(i+(n/2))%n]);
cout<<cnt<<endl;
for(int i=0;i<n;i++) cout<<a[i]<<" "<<a[(i+(n/2))%n]<<endl;
return 0;
}
CodeForces 370C. Mittens的更多相关文章
- 【codeforces 370C】Mittens
[题目链接]:http://codeforces.com/problemset/problem/370/C [题意] 给你n个人,每个人都有一双相同颜色的手套; 然允许在所有人之间交换手套; (每个人 ...
- CodeForces 370C
这道题其实是挺简单的.首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套.反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不 ...
- Codeforce 370C Mittens 巧妙数学题
这道题目我一开始想错了,觉得只要排好序,再从头到尾把可以相互交换的进行下交换就可以了...事实证明是错的.正确的解法比较巧妙,而且写法非常好,值得学习 首先,要注意的一个规律是,假如最大的颜色数字出现 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- BZOJ 1270: [BeijingWc2008]雷涛的小猫( dp )
简单的dp.. dp(i,j) = max(dp(x,y))+cnt[i][j], (x,y)->(i,j)是合法路径. 设f(i)= max(dp(x,y))(1≤x≤N, 1≤y≤i), g ...
- codeigniter ,看完这些,就可以用它做项目了
一.MVC 1,入口文件 唯一一个让浏览器直接请求的脚本文件 2,控制器 controller 负责协调模型和视图 3,模型 model 只负责提供数据,保存数据 4,视图 只负责显示,以及搜集用户的 ...
- FireMonkey下的异形窗体拖动(句柄转换)
DelphiXE2 Firemoney FMX 的窗体不只是为windows的, 所以很多功能都没有了. 最常见的就是拖拽了 先看 VCL时代 一个经典拖动代码 ReleaseCapture(); S ...
- [置顶] getline函数-linux
头文件: #include <stdio.h> 函数: ssize_t getline(char **lineptr, size_t *n, FILE *stream); eg: ssiz ...
- mysql 表级锁
表级锁:分为读锁和写锁: lock tables table_name read;//其他事务只能读,不能加写锁,要等待更新. SESSION 50 执行: mysql> update test ...
- 统一横轴墨卡托投影(UTM)
UTM 坐标系统使用基于网格的方法表示坐标.UTM 系统将地球分为 60 个区,每一个区基于横轴墨卡托投影.画图法中的地图投影方法能够在平面中表示一个两维的曲面,比如一个标准地图.图 1 展示了一个横 ...
- .NET支持上下左右移动操作
效果如下图: 代码如下: public partial class ShowSet : System.Web.UI.Page { Hashtable resources = EquStatusSear ...
- Activity生命周期方法的调用顺序project与測试日志
以下为測试activity的方法的运行顺序 project与測试资源地址 androidproject AndroidManifest.xml <? xml version="1. ...
- CSS:重量和级联规则,确定其优先级
资源:http://www.ido321.com/1063.html 首先,给大家看一篇关于CSS优先级的演示样例:http://www.ido321.com/76.html 一.主要的优先级规则 比 ...
- Mnesia基本用法
查看表结构 查看mnesia表的结构: mnesia:info(). 查看此表的基本信息: mnesia:table_info(<tableName>, all). Mnesia初使化 m ...