这道题其实是挺简单的。首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套。反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不同颜色的手套(每个人都留一只自己原本的,再要别人的一只手套就可以了)。

 #include <iostream>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <cstring>
#include <list>
using namespace std;
struct Child{
int color, num, mat;
};
bool partition_cmp(const Child& c1, const Child& c2){
return c1.color < c2.color;
}
bool sort_cmp(const Child& c1, const Child& c2){
return c1.num < c2.num;
}
int main(){
int n, m;
Child a[];
int c[];
cin >> n >> m;
for (int i = ; i <= m; i++){
c[i] = ;
}
for (int i = ; i < n; i++){
cin >> a[i].color;
a[i].num = i;
c[a[i].color]++;
}
int mx = ;
for (int i = ; i <= m; i++){
if (c[mx] < c[i]){
mx = i;
}
}
if (c[mx] * > n){
cout << * (n - c[mx]) << endl;
int mcount = , miter = -, d = n - c[mx];
for (int i = ; i < n; i++){
if (a[i].color == mx){
cout << a[i].color << " ";
if (d > ){
for (miter++; miter < n && a[miter].color == mx; miter++);
cout << a[miter].color << endl;
d--;
}
else{
cout << a[i].color << endl;
}
}
else{
cout << a[i].color << " " << mx << endl;
}
}
}
else{
cout << n << endl;
sort(a, a + n, partition_cmp);
for (int i = ; i < n; i++){
a[i].mat = a[(i + c[mx]) % n].color;
}
sort(a, a + n, sort_cmp);
for (int i = ; i < n; i++){
cout << a[i].color << " " << a[i].mat << endl;
}
}
return ;
}

最新文章

  1. 三层架构的OPP实现的演示例子
  2. 脚本tips
  3. Solve error: Cannot open include file: &#39;X11/Xlocale.h&#39;: No such file or directory
  4. C语言之移位操作
  5. java web项目 。classpath 文件解析
  6. SGU 103.Traffic Lights(最短路)
  7. HTML5之选择上传图片文件
  8. C#操作AD的例子
  9. 打印man手册为pdf文件
  10. 常用的linux系统监控命令整理
  11. C#中的协变(Covariance)和逆变(Contravariance)
  12. Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency
  13. 可空类型 Nullable&lt;T&gt;
  14. pycharm 安装与基本设置
  15. post插件
  16. Go语言 并发编程
  17. 信息检索中的TF/IDF概念与算法的解释
  18. js中创建数组,并往数组里添加元素
  19. Visual studio中编译和使用libpng和zlib
  20. Python Algorithms – chapter2 基础知识

热门文章

  1. Springboot 相关注解大全
  2. 题解报告:hdu 1015 Safecracker
  3. Linq 内连接和外连接(转载)
  4. 对数组名取地址&amp;a和 数组首地址a
  5. 376 Wiggle Subsequence 摆动序列
  6. scala-基础-映射(1)
  7. WordPress个性页面制作教程
  8. 继续C#开发or转做产品
  9. 【译】x86程序员手册23-6.5组合页与段保护
  10. Oracle RAC 后台进程