【题目链接】:http://codeforces.com/problemset/problem/370/C

【题意】



给你n个人,每个人都有一双相同颜色的手套;

然允许在所有人之间交换手套;

(每个人的手套有左右之分,左手套不能戴在右手上)

问你最后,每个人的左右手的手套颜色不同的人数最多能有多少个;

【题解】



贪心;

把手套的颜色,按照数量降序排;

然后顺序枚举;

一种颜色一种颜色的枚举;

假设i..j是一种颜色;

则在1..i-1中找手套和它换;

优先找左右手的手套一样的人的;

因为这样换了之后,不同的人数+2

如果实在找不到相同的;

就找左右手套不同的人换就好;

这样,不同的人数+1;

这样的贪心策略显然是对的吧;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int M = 110;
const int N = 5e3+100; struct abc{
int idx,num;
}; int n,m,c[N][3],d[N][2];
vector <int> a[M];
abc b[N]; int main(){
// Open();
Close();
cin >> n >> m;
rep1(i,1,n){
int x;
cin >> x;
a[x].pb(i);
}
n = 0;
rep1(i,1,m)
if (!a[i].empty()){
b[++n].idx = i;
b[n].num = (int) a[i].size();
}
sort(b+1,b+1+n,[&] ( abc a,abc b) { return a.num > b.num;});
int nn = 0;
rep1(i,1,n){
int id = b[i].idx;
while ( (int) a[id].size()){
nn++;
c[nn][0] = c[nn][1] = id;
c[nn][2] = a[id].back();
a[id].pop_back();
}
}
n = nn;
rep1(i,1,n){
int j = i;
while (j+1<=n && c[j+1][0]==c[j][0]) j++;
//cout << i <<' '<< j << endl;
int now = i;
rep1(k,1,i-1)
if (c[k][0]==c[k][1] && c[k][0] != c[now][0]){
if (now > j ) break;
swap(c[k][0],c[now][0]);
now++;
}
rep1(k,1,i-1)
if (c[k][0] != c[now][0]){
if (now > j ) break;
swap(c[k][0],c[now][0]);
now++;
}
i = j;
}
int ans = 0;
rep1(i,1,n)
if (c[i][0]!=c[i][1]) ans++;
rep1(i,1,n){
d[c[i][2]][0] = c[i][0];
d[c[i][2]][1] = c[i][1];
}
cout << ans << endl;
rep1(i,1,n){
cout << d[i][0] <<' ' << d[i][1] << endl;
}
return 0;
}

【codeforces 370C】Mittens的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. sed将上下两行并列

    #cat log5 mmmm 1234 nnnn 2344 #sed -n '{N;s/\n/\t/p}' log5 mmmm 1234 nnnn 2344

  2. 论文阅读《ActiveStereoNet:End-to-End Self-Supervised Learning for Active Stereo Systems》

    本文出自谷歌与普林斯顿大学研究人员之手并发表于计算机视觉顶会ECCV2018.本文首次提出了应用于主动双目立体视觉的深度学习解决方案,并引入了一种新的重构误差,采用自监督的方法来解决缺少ground ...

  3. 洛谷P5239 回忆京都

    和 NOIP2016TG 组合数问题 差不多是一样的-- 首先要知道杨辉三角和组合数之间的关系 看一下数据范围,很明显要避免重复计算,而且查询的复杂度要非常小 一看n, m <= 1000 这明 ...

  4. Android 混淆后的代码调试

    ProGuard的输出文件及用处 混淆之后,会给我们输出一些文件,在gradle方式下是在<project_dir>/build/proguard/目录下,ant是在<project ...

  5. spring boot pom

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. Webserver管理系列:4、WinPE

    WinPE能够识别NTFS分区,使用WinPE能够备份/还原系统.能够重置用户password. 首先给大家看下怎样用WinPE重置password: 放入WinPE光盘-〉启动后-〉点開始菜单-〉程 ...

  7. iOS UI01_UIView

    // //  AppDelegate.m //  UI01_UIView // //  Created by dllo on 15/7/29. //  Copyright (c) 2015年 zhoz ...

  8. LeetCode -- 求字符串数组中的最长公共前缀

    题目描写叙述: Write a function to find the longest common prefix string amongst an array of strings.就是给定1个 ...

  9. 《Head First 设计模式》学习笔记——适配器模式 + 外观模式

    在ADO.NET中.对于我们从数据库中取出的数据都要放到一个DataSet中,无论你是Access的数据库,还是SQL的数据库,或者是Oracle的数据库都要放到DataSet中..NET中并没有提供 ...

  10. legend---十一、thinkphp事务中if($ans1&&$ans2){}else{}方式和try{}catch{}方式事务操作的区别在哪里

    legend---十一.thinkphp事务中if($ans1&&$ans2){}else{}方式和try{}catch{}方式事务操作的区别在哪里 一.总结 一句话总结:执行的条件其 ...