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):

#include<bits/stdc++.h>
#define ms(a, b)  memset((a), (b), sizeof(a))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 2e5+10;

int n, m, k;
int col[maxn], fa[maxn];
map<int, int>Set[maxn];

int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }

void init()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 1; i<=n; i++)
        scanf("%d",&col[i]);

    for(int i = 1; i<=n; i++)
        fa[i] = i, Set[i].clear();
}

void solve()
{
    for(int i = 1; i<=m; i++)
    {
        int u, v ;
        scanf("%d%d",&u,&v);
        u = find(u);
        v = find(v);
        if(u!=v)
            fa[u] = v;
    }

    for(int i = 1; i<=n; i++)   //统计:在集合x中, 颜色为y的有z个。
        Set[find(i)][col[i]]++;

    int ans = n;
    map<int,int>::iterator it;
    for(int i = 1; i<=n; i++)
    {
        if(fa[i]==i)    //当fa[i]==i时, 为一个集合
        {
            int maxx = -1;
            for(it = Set[i].begin(); it != Set[i].end(); it++)
                maxx = max(maxx, it->second);
            //选择个数最多的那种颜色
            ans -= maxx;    //除了被选中的颜色的袜子外,这个集合的其他颜色的袜子都染上这种颜色
        }
    }
    cout<<ans<<endl;
}

int main()
{
    init();
    solve();
}


代码2(vector):

#include<bits/stdc++.h>
#define ms(a, b)  memset((a), (b), sizeof(a))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 2e5+10;

int n, m, k;
int col[maxn], fa[maxn];
vector<int>Set[maxn];

int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }

void init()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 1; i<=n; i++)
        scanf("%d",&col[i]);

    for(int i = 1; i<=n; i++)
        fa[i] = i, Set[i].clear();
}

void solve()
{
    for(int i = 1; i<=m; i++)
    {
        int u, v ;
        scanf("%d%d",&u,&v);
        u = find(u);
        v = find(v);
        if(u!=v)
            fa[u] = v;
    }

    for(int i = 1; i<=n; i++)
        Set[find(i)].push_back(col[i]);

    int ans = n;
    for(int i = 1; i<=n; i++)
    {
        if(Set[i].size()==0) continue;

        sort(Set[i].begin(), Set[i].end() );
        int cnt = 0, maxx = 0;
        for(int j = 0; j<Set[i].size(); j++)
        {
            if(j==0 || Set[i][j-1]==Set[i][j] ) cnt++;
            else cnt = 1;
            maxx = max(maxx, cnt);  
        }
        ans -= maxx;
    }
    cout<<ans<<endl;
}

int main()
{
    init();
    solve();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值