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== || a[i]==a[i-] ) cnt++;
else cnt = ;
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+;
const int maxn = 2e5+; 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 = ; i<=n; i++)
scanf("%d",&col[i]); for(int i = ; i<=n; i++)
fa[i] = i, Set[i].clear();
} void solve()
{
for(int i = ; 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 = ; i<=n; i++) //统计:在集合x中, 颜色为y的有z个。
Set[find(i)][col[i]]++; int ans = n;
map<int,int>::iterator it;
for(int i = ; i<=n; i++)
{
if(fa[i]==i) //当fa[i]==i时, 为一个集合
{
int maxx = -;
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+;
const int maxn = 2e5+; 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 = ; i<=n; i++)
scanf("%d",&col[i]); for(int i = ; i<=n; i++)
fa[i] = i, Set[i].clear();
} void solve()
{
for(int i = ; 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 = ; i<=n; i++)
Set[find(i)].push_back(col[i]); int ans = n;
for(int i = ; i<=n; i++)
{
if(Set[i].size()==) continue; sort(Set[i].begin(), Set[i].end() );
int cnt = , maxx = ;
for(int j = ; j<Set[i].size(); j++)
{
if(j== || Set[i][j-]==Set[i][j] ) cnt++;
else cnt = ;
maxx = max(maxx, cnt);
}
ans -= maxx;
}
cout<<ans<<endl;
} int main()
{
init();
solve();
}
Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心的更多相关文章
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- 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 ...
- 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 ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)
#include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...
- 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 ...
- Codeforces Round #623 (Div. 2) D.Recommendations 并查集
ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...
- Codeforces Round #812 (Div. 2) E(并查集)
种类并查集:定义种类之间的关系来判断操作是否进行 题目大意:对于题目给出的一个矩阵,我们可以进行一种操作:swap(a[i][j],a[j][i]) 使得矩阵可以变换为字典序最小的矩阵 思路: 通过扫 ...
随机推荐
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- 投影纹理映射(Projective Texture Mapping) 【转】
摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 投影纹理映射( Projective ...
- libsvm交叉验证与网格搜索(参数选择)
首先说交叉验证.交叉验证(Cross validation)是一种评估统计分析.机器学习算法对独立于训练数据的数据集的泛化能力(generalize), 能够避免过拟合问题.交叉验证一般要尽量满足:1 ...
- STM32串行通信USART解说笔记
STM32串行通信USART程序例举链接:http://blog.csdn.net/dragon12345666/article/details/24883111 1.STM32串行通信USART的相 ...
- Android加壳native实现
本例仅在Android2.3模拟器跑通过,假设要适配其它机型.请自行研究,这里不过抛砖引玉. 0x00 在Android中的Apk的加固(加壳)原理解析和实现,一文中脱壳代码都写在了java层非常ea ...
- 2014年8月25日,收藏家和杀手——面向对象的C++和C(一)
近期事情特别多,睡眠也都非常晚,有点精神和身体混乱的感觉,所以想写写技术分析文章.让两者的我都调整一下.这篇技术分析文章是一直想写的,当前仅仅是开篇,有感觉的时候就写写,属于拼凑而成,兴许的篇章没有时 ...
- Amazon SNS移动推送更新——新增百度云推送和Windows平台支持
Amazon SNS(Simple Notification Service)是一种基于云平台的消息通知和推送服务. SNS提供简单的 Web 服务接口和基于浏览器的管理控制台让用户可以简易设置.执行 ...
- iOS设计模式 - (2)UML类间关系精解
在正式讲设计模式之前, 介绍一下UML类图之间的关系还是非常有必要的, 由于一些教程, 书籍, 包含我之后的文章, 都会大量使用类图, 去描写叙述各个类之间的关系.这是一种非常直观, 简约的方式. 当 ...
- 内核顶层Makefile相关3
http://www.groad.net/bbs/simple/?f104.html 伪目标 .PHONY是一个特殊工作目标(special target),它用来指定一个假想的工作目标,也就是说它后 ...
- Hadoop 服务器配置的副本数量 管不了客户端
副本数由客户端的参数dfs.replication决定(优先级: conf.set > 自定义配置文件 > jar包中的hdfs-default.xml)如果前两个都没有,就用最后一个ja ...