cf C. Mittens

http://codeforces.com/contest/370/problem/C

题意:有n个人,m中颜色的手套,开始每个人都是两只相同颜色的手套,经过交换最多可以换出多少个人戴不同颜色的手套。

先按照出现颜色个数排序,然后向后递推交换右手套就可以。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 60000
 5 using namespace std;
 6 
 7 int n,m;
 8 int c[maxn];
 9 int num[maxn];
10 struct node
11 {
12     int val;
13     int lco,rco;
14     int cnt;
15     bool operator <(const node &a)const
16     {
17         return (cnt>a.cnt)||(cnt==a.cnt&&val<a.val);
18     }
19 } p[maxn],q[maxn];
20 
21 int main()
22 {
23     while(scanf("%d%d",&n,&m)!=EOF)
24     {
25         memset(num,0,sizeof(num));
26         for(int i=0; i<n; i++)
27         {
28             scanf("%d",&c[i]);
29             num[c[i]]++;
30         }
31         for(int i=0; i<n; i++)
32         {
33             p[i].val=c[i];
34             p[i].cnt=num[c[i]];
35         }
36         sort(p,p+n);
37         for(int i=0; i<n; i++)
38         {
39             p[i].lco=p[i].val;
40             p[i].rco=p[i].val;
41         }
42         int j=p[0].cnt;
43         for(int i=0; i<n; i++)
44         {
45             if(j>=n) break;
46             swap(p[i].rco,p[j].rco);
47             j++;
48         }
49         int t1=0;
50         for(int i=0; i<n; i++)
51         {
52             if(p[i].lco!=p[i].rco) t1++;
53         }
54         printf("%d\n",t1);
55         for(int i=0; i<n; i++)
56         {
57             printf("%d %d\n",p[i].lco,p[i].rco);
58         }
59     }
60     return 0;
61 }
View Code

 

posted @ 2014-09-15 19:32  null1019  阅读(120)  评论(0编辑  收藏  举报