题目链接:http://codeforces.com/contest/731/problem/C

题解:

1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条袜子可以穿多次或者不穿。那么自然就想到用并查集(DSU), 把有关联的袜子放在一个集合(经过处理后,这个集合中所有的袜子的颜色一样)。

2.集合问题搞定了,那么就轮到选颜色的为题了。怎么选颜色,使得每个集合的袜子颜色一样,且需要改变颜色的袜子尽可能少呢?方法是:对于每一个集合,选择袜子条数最多的那种颜色,作为该集合的最终颜色, 即除了被选定的颜色的袜子之外,集合中的其他袜子都需要改变颜色。

3.用map或者vector维护集合就可以了。

学习之处:

1.vector可以用sort()进行排序: sort(v.begin(), v.end() );

2.先对map清空,然后可以直接 map[key]++, 即使一开始map中不存在key。 执行操作后key就存在了, 且map[key] = 1。

3.统计有几个连续符合条件的:

  1. if(i== || a[i]==a[i-] ) cnt++;
  2. else cnt = ;
  3. maxx = max(maxx, cnt); //不要放在else语句里面。因为最后一个可能符合条件,但却没有进入else,更新maxx。

代码1(map):

  1. #include<bits/stdc++.h>
  2. #define ms(a, b) memset((a), (b), sizeof(a))
  3. using namespace std;
  4. typedef long long LL;
  5. const int INF = 2e9;
  6. const LL LNF = 9e18;
  7. const int mod = 1e9+;
  8. const int maxn = 2e5+;
  9.  
  10. int n, m, k;
  11. int col[maxn], fa[maxn];
  12. map<int, int>Set[maxn];
  13.  
  14. int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
  15.  
  16. void init()
  17. {
  18. scanf("%d%d%d",&n,&m,&k);
  19. for(int i = ; i<=n; i++)
  20. scanf("%d",&col[i]);
  21.  
  22. for(int i = ; i<=n; i++)
  23. fa[i] = i, Set[i].clear();
  24. }
  25.  
  26. void solve()
  27. {
  28. for(int i = ; i<=m; i++)
  29. {
  30. int u, v ;
  31. scanf("%d%d",&u,&v);
  32. u = find(u);
  33. v = find(v);
  34. if(u!=v)
  35. fa[u] = v;
  36. }
  37.  
  38. for(int i = ; i<=n; i++) //统计:在集合x中, 颜色为y的有z个。
  39. Set[find(i)][col[i]]++;
  40.  
  41. int ans = n;
  42. map<int,int>::iterator it;
  43. for(int i = ; i<=n; i++)
  44. {
  45. if(fa[i]==i) //当fa[i]==i时, 为一个集合
  46. {
  47. int maxx = -;
  48. for(it = Set[i].begin(); it != Set[i].end(); it++)
  49. maxx = max(maxx, it->second);
  50. //选择个数最多的那种颜色
  51. ans -= maxx; //除了被选中的颜色的袜子外,这个集合的其他颜色的袜子都染上这种颜色
  52. }
  53. }
  54. cout<<ans<<endl;
  55. }
  56.  
  57. int main()
  58. {
  59. init();
  60. solve();
  61. }

代码2(vector):

  1. #include<bits/stdc++.h>
  2. #define ms(a, b) memset((a), (b), sizeof(a))
  3. using namespace std;
  4. typedef long long LL;
  5. const int INF = 2e9;
  6. const LL LNF = 9e18;
  7. const int mod = 1e9+;
  8. const int maxn = 2e5+;
  9.  
  10. int n, m, k;
  11. int col[maxn], fa[maxn];
  12. vector<int>Set[maxn];
  13.  
  14. int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
  15.  
  16. void init()
  17. {
  18. scanf("%d%d%d",&n,&m,&k);
  19. for(int i = ; i<=n; i++)
  20. scanf("%d",&col[i]);
  21.  
  22. for(int i = ; i<=n; i++)
  23. fa[i] = i, Set[i].clear();
  24. }
  25.  
  26. void solve()
  27. {
  28. for(int i = ; i<=m; i++)
  29. {
  30. int u, v ;
  31. scanf("%d%d",&u,&v);
  32. u = find(u);
  33. v = find(v);
  34. if(u!=v)
  35. fa[u] = v;
  36. }
  37.  
  38. for(int i = ; i<=n; i++)
  39. Set[find(i)].push_back(col[i]);
  40.  
  41. int ans = n;
  42. for(int i = ; i<=n; i++)
  43. {
  44. if(Set[i].size()==) continue;
  45.  
  46. sort(Set[i].begin(), Set[i].end() );
  47. int cnt = , maxx = ;
  48. for(int j = ; j<Set[i].size(); j++)
  49. {
  50. if(j== || Set[i][j-]==Set[i][j] ) cnt++;
  51. else cnt = ;
  52. maxx = max(maxx, cnt);
  53. }
  54. ans -= maxx;
  55. }
  56. cout<<ans<<endl;
  57. }
  58.  
  59. int main()
  60. {
  61. init();
  62. solve();
  63. }

Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心的更多相关文章

  1. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  2. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  3. Codeforces Round #376 (Div. 2) C. Socks bfs

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  4. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  5. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  6. Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)

    #include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...

  7. Codeforces Round #346 (Div. 2) E题 并查集找环

    E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...

  8. Codeforces Round #623 (Div. 2) D.Recommendations 并查集

    ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...

  9. Codeforces Round #812 (Div. 2) E(并查集)

    种类并查集:定义种类之间的关系来判断操作是否进行 题目大意:对于题目给出的一个矩阵,我们可以进行一种操作:swap(a[i][j],a[j][i]) 使得矩阵可以变换为字典序最小的矩阵 思路: 通过扫 ...

随机推荐

  1. TensorFlow——tensorflow指定CPU与GPU运算

    1.指定GPU运算 如果安装的是GPU版本,在运行的过程中TensorFlow能够自动检测.如果检测到GPU,TensorFlow会尽可能的利用找到的第一个GPU来执行操作. 如果机器上有超过一个可用 ...

  2. Careercup | Chapter 2

    链表的题里面,快慢指针.双指针用得很多. 2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow w ...

  3. AtCoder - 3913 XOR Tree

    Problem Statement You are given a tree with N vertices. The vertices are numbered 0 through N−1, and ...

  4. IDEA查看源码时提示:Library source does not match the bytecode for class的问题分析

    通过Maven查看依赖的源码时,通常是Maven自动下载JAR包附属的source包,但是会出现一个问题,由于使用lombok插件会造成编写的Java文件和编译后的class上有差别,所以IDEA打开 ...

  5. 奇酷手机显示Log

    1.在桌面点击拨号,在拨号盘输入“*20121220#”,进入工程模式;2.看到日志输出等级,点进去 Log print enable 选 enable Java log level 选 LOGV C ...

  6. android listview 添加数据

    <span style="white-space:pre"> </span>listView = (ListView) findViewById(R.id. ...

  7. 精简版的MySQL制作步骤

    1.删除所有的目录,只保留 datasharebin 2.删除BIN下面除以下三个文件之外的所有文件: libmysql.dll(MYSQL5中的文件,在MYSQL5.5中不存在)mysqladmin ...

  8. Access自定义函数(人民币大写)

    人民币大写函数:整数不超过13位. Public Function 人民币大写(A) As String Dim aa As String Dim bb As String Dim cc As Str ...

  9. 没有IP地址的主机怎样保持IP层联通

    在<两台不同网段的PC直连能否够相互ping通>一文中,我有点像在玩旁门左道,本文中.我继续走火入魔.两台机器,M1和M2,各自有一个网卡eth0,配置例如以下:M1的配置:eth0上不配 ...

  10. openERP server action,最强大的功能,没有之一

    Jeffery9@gmail.com 出品 @jeffery-陈帆 原理 ations OE定义了ir.actions.actions,并从中派生了众多的子类 ir.actions.client ir ...