zoukankan      html  css  js  c++  java
  • Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接: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.统计有几个连续符合条件的:

    if(i==0 || a[i]==a[i-1] ) cnt++;
    else cnt = 1;
    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+7;
     8 const int maxn = 2e5+10;
     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 = 1; i<=n; i++)
    20         scanf("%d",&col[i]);
    21 
    22     for(int i = 1; i<=n; i++)
    23         fa[i] = i, Set[i].clear();
    24 }
    25 
    26 void solve()
    27 {
    28     for(int i = 1; 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 = 1; 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 = 1; i<=n; i++)
    44     {
    45         if(fa[i]==i)    //当fa[i]==i时, 为一个集合
    46         {
    47             int maxx = -1;
    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 }
    View Code

    代码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+7;
     8 const int maxn = 2e5+10;
     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 = 1; i<=n; i++)
    20         scanf("%d",&col[i]);
    21 
    22     for(int i = 1; i<=n; i++)
    23         fa[i] = i, Set[i].clear();
    24 }
    25 
    26 void solve()
    27 {
    28     for(int i = 1; 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 = 1; i<=n; i++)
    39         Set[find(i)].push_back(col[i]);
    40 
    41     int ans = n;
    42     for(int i = 1; i<=n; i++)
    43     {
    44         if(Set[i].size()==0) continue;
    45 
    46         sort(Set[i].begin(), Set[i].end() );
    47         int cnt = 0, maxx = 0;
    48         for(int j = 0; j<Set[i].size(); j++)
    49         {
    50             if(j==0 || Set[i][j-1]==Set[i][j] ) cnt++;
    51             else cnt = 1;
    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 }
    View Code


  • 相关阅读:
    asp image
    Linux安装破解sublime text 2
    Ubuntu Java环境配置
    Ubuntu上完美视频播放软件XBMC
    制作Ubuntu Live启动盘
    STM32·flash烧写
    Ubuntu上用Windows那些软件
    Ubuntu屏幕亮度控制在acer4750g笔记本上失效的解决方法
    Ubuntu的grub启动界面,默认高亮选项的修改方法
    Ubuntu解决无法删除msttcorefonts的问题
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538686.html
Copyright © 2011-2022 走看看