这道题其实是挺简单的。首先很容易发现最多人用的颜色的人数如果大于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 ;
}

CodeForces 370C的更多相关文章

  1. 【codeforces 370C】Mittens

    [题目链接]:http://codeforces.com/problemset/problem/370/C [题意] 给你n个人,每个人都有一双相同颜色的手套; 然允许在所有人之间交换手套; (每个人 ...

  2. CodeForces 370C. Mittens

    C. Mittens time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Android之条形码、二维码扫描框架(非原创)

    文章大纲 一.条形码.二维码扫描框架介绍二.条形码.二维码的区别和组成结构介绍三.条形码.二维码扫描框架应用场景四.BGAQRCode-Android框架实战五.项目源码下载六.参考文章 一.条形码. ...

  2. [IOI1998]Polygon

    很早就看到这题了...但因为有个IOI标志,拖到现在才做 由于是以前在书上看到的,就没有想过其他算法,直接区间DP了... 方程式也挺好想的 跟我们平时做数学题求几个数乘积最大差不多 最大的*最大的 ...

  3. JavaScript编程艺术-第7章代码汇总(2)

    [7.4节] 重回“JavaScript美术馆”代码 ***亲测可用*** HTML: JS:

  4. thinkphp 5 常用的助手函数

    load_trait:快速导入Traits,PHP5.5以上无需调用 /**     * 快速导入Traits PHP5.5以上无需调用     * @param string    $class t ...

  5. BFS(倒水问题) HDU 1495 非常可乐

    题目传送门 /* BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都 ...

  6. js重写alert()弹窗

    //重写alertwindow.alert = function(str){ var alertFram = document.getElementById('alertFram'); var shi ...

  7. vue 父子组件双向绑定

    vue组件有2大特性: 1.全局组件和局部组件 2.父子组件的数据传递 接下来直接用demo直接看如何传值(静态传值) father.vue <template> <div> ...

  8. python网络爬虫。第一次测试-有道翻译

    2018-03-0720:53:56 成功的效果如下 代码备份 # -*- coding: UTF-8 -*- from urllib import request from urllib impor ...

  9. 三维重建:GitHub百度Apollo 2.0

    GitHub:https://github.com/ApolloAuto/apollo 1. 关于Apollo的数据:Apollo的数据会如何开放? 自动驾驶数据将包括具有高分辨率图像和像素级别标注的 ...

  10. POJ_1163_The triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40079   Accepted: 24144 De ...