这道题其实是挺简单的。首先很容易发现最多人用的颜色的人数如果大于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. 有符号char转无符号short

    ; cout<<(int)ch<<endl; //-1 unsigned short d = ch; short dd = ch; cout<<d<<e ...

  2. 认识 jQuery (第一笔 点出重点)

    1.我们先来讨论一下 jQuery 是什么? 要认识jQuery(简写jQ)首先要知道JavaScript(简写 js),因为jq就是一堆js的函数,是普通的js,只不过jq应用广泛,形成了行业标准[ ...

  3. js解析地址栏参数

    /** * 获取地址栏中url后面拼接的参数 * eg: * 浏览器地址栏中的地址:http://1.1.1.1/test.html?owner=2db08226-e2fa-426c-91a1-66e ...

  4. 重新学习Java——Java基本的程序设计结构(二)

    上一节简单回顾了Java基本的一些程序设计的知识,这一节将继续根据<Java核心技术>这本书,进行这方面知识的复习与探索. 1. 字符串 Java字符串实际上就是Unicode字符序列.例 ...

  5. Activity的退出和進入效果

    看了android的源代码和资源文件,终于明白如何去修改设置Dialog和Activity的进入和退出效果了.设置Dialog首先通过 getWindow()方法获取它的窗口,然后通过getAttri ...

  6. leetcode486 Predict the Winner

    思路: 博弈. 实现: class Solution { public: bool PredictTheWinner(vector<int>& nums) { ][]; int n ...

  7. viewpager滑动时页面不能刷新

    有一种解决方法就是覆盖PagerAdapter中的getItemPosition()方法,这种方案虽然简单,但是因为这种方法是让每次呼叫PagerAdapter时,都会遍历childView,通过ge ...

  8. Java———较大二进制文件的读、写

    由于项目需要,需要对二进制文件进行读写.转换. 文件说明:由其他程序得到的二进制文件,文件内容为:包含23543个三角形.13270个顶点的三角网所对应的721组流速矢量(u.v)文件,通俗些说,一条 ...

  9. GEO/SRA数据库

    GEO数据库 GEO数据库隶属于NCBI,是最大最全面的基因表达数据库,主要是芯片和转录组测序数据.除储存数据外,也提供一些数据挖掘工具,因此利用好这个数据库,没有实验,没有自己的数据也能发好文章! ...

  10. java项目其他基础配置

    创建完maven项目之后. 1.pom.xml文件配置项目相关的架包. 2.src.main.resources下边 创建文件夹:spring以及mapper. 3.src.main.resource ...