codeforces 731C 并查集+贪心

CodeForces - 731C Socks (并查集+贪心) C. Socks time limit per test  2 seconds memory limit per test  256 megabytes input  standard input output  standard output Arseniy is already grown-up and independent. H 阅读详情

点击打开链接

利用并查集把颜色要相同的下标放在同一集合中,贪心:找到每个集合中出现最多的颜色 把该集合中剩下的颜色变为该颜色即可 ans+=size-mx

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> 
#include <vector>
#include <stack>
#include <map>
using namespace std;
const int N=2e5+10;
long long ans=0;
int c[N];
int l,r;
int fa[N];
vector<int> v[N];
int num[N];

int find(int x)
{
	if(fa[x]!=x)
	{
		fa[x]=find(fa[x]);
	}
	return fa[x];
}
void Union(int x,int y)
{
	int fx=find(x),fy=find(y);
	fa[fx]=fy; 
}
int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>c[i];
		fa[i]=i;
	}
	for(int i=1;i<=m;i++)
	{
		cin>>l>>r; //下标 l,r 
	 	Union(l,r);//把颜色要相同的先放在同一个集合 
	}
	int tot=0;
	for(int i=1;i<=n;i++)
	{
		if(fa[i]==i)
		num[i]=++tot;//集合编号 
	}
	for(int i=1;i<=n;i++)
	{
		v[num[find(i)]].push_back(c[i]);
	}
	for(int i=1;i<=tot;i++)
	{
		int y=v[i].size();
		int mx=0;
		map<int,int> x;
		for(int j=0;j<y;j++)//找到集合中颜色最多的 把剩下的元素变成该颜色即可 
		{
			x[v[i][j]]++;
			mx=max(mx,x[v[i][j]]);
		}
		ans+=(y-mx);
	}
	cout<<ans<<endl;
	
	return 0;
}


Python量化投资——mplfinance实现全功能动态交互式K线图(蜡烛图)【源码+详解】 特别鸣谢:@szFoxtech 同学热心提供了通过键盘操作平移和缩放K线图的代码!!在我的另一篇文章《七行代码利用tushare和mplfinance生成K线图》中,我介绍了两个非常好用的python库,结合起来可以非常方便容易地生成股票的K线图,对于使用python进行量化投资研究的朋友可谓是必备良方。如下图所示,仅仅几行代码,就能生成下面这张K线图:不过,仅仅使用七行代码生成的K线图,虽然看上去也像那么回事了,但是对于真正需要使用K线图作为研究工具或者可视化工具的朋友们来说,是远远不够的。在我看来,至少 阅读详情

相关推荐

UnityWebGL异常捕获全攻略:从报错提示到完美解决的3种配置方案

本文详细解析了UnityWebGL项目中异常捕获的三种核心配置方案:禁用异常、仅捕获显式异常以及完全捕获异常(无堆栈跟踪)。通过对比不同方案在构建体积、运行性能及错误信息详细度上的差异,帮助开发者根据开发、测试与发布阶段的不同需求,选择并配置最优的异常处理策略,从而有效提升WebGL应用的调试效率和线上稳定性。

5f4d3s2a1q的博客 405

codeforces(D. Dogeforces) 并查集

原题链接 排序+并查集 代码: //#pragma GCC optimize("Ofast") //#pragma GCC target("avx,avx2,fma") //#pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #define pb push_back #define ll long long #define IOS std::ios::sync_with_stdio(false);cin.tie

qq_43781431的博客 422

基于STM32的家用新风系统智能控制器 | 保姆式教程 开源可复刻

本项目基于STM32F103C8T6核心控制器,完整实现了家用新风系统智能控制器的全功能开发,覆盖多传感器数据采集、双模式控制、PID调速、异常报警、掉电记忆等核心功能,全程采用保姆式无跳步教学,从硬件选型、接线、CubeMX配置、代码开发、调试排坑全流程拆解,跟着步骤一步步操作,就能顺利完成项目复刻

ZhongWeiXiang9的博客 477

codeforces 468B Two Sets 并查集变形

题目链接: 题目大意:给你N个数,再给

e的专栏 962

ddr3ip核心_DDR3 IP 核配置

DDR3 控制器 IP 核2.DDR3 IP核配置点击 Flow Navigator 面板下的“Project Manager -->IP Catalog”。 在“Memories & Storage Elements -->Memory Interface Generators”分类展开后,可以看到名为“Memory Interface Generator (MIG 7 ...

weixin_33049919的博客 2942

Open Interpreter利用Code Interpreter实现本地化

作为一个可以在本地环境中运行的工具,Open Interpreter可以访问互联网,没有文件大小和运行时间的限制,并且可以利用任何包或库。去年7月,OpenAI发布了一个强大的插件,名为Code Interpreter(代码解释器),它可以根据用户的自然语言需求生成并执行代码解决方案,帮助完成各种任务,如数据分析、图表创建和文件编辑等。Open Interpreter不仅是一个编码解释器,它还是一个强大的工具,专注于处理复杂任务,将GPT-4代码解释器的强大功能与本地开发环境的灵活性完美结合在一起。

DigitalChina_DCG的博客 1798

并查集

一,并查集 并查集是一种森林,而且是反向森林,即每个节点都指向自己的父亲节点。 根据森林中根节点的个数,所有的节点都分成了若干个不相交的集合。 并查集的主要操作:查找一个节点的祖先、合并两颗树 二,查找祖先 1,普通查找 int find(int x) //找祖先 { int r = x; while (fa[r] != r)r = fa[r]; return r; } 普通查找比较慢,但是有些问题只能用普通查找,不能做路径压缩。 2,带路径压缩的查找 int f....

nameofcsdn的博客 2091

ComfyUI 汉化教程(中文界面)

如果你对翻译的结果不满意,还可以进行修改。首先,进入目录,可以看到该目录中有很多 JSON 文件:打开 JSON 文件,找到要修改的内容,修改对应的Value值即可:最后,重启 ComfyUI 即可。

水滴的博客 1万+

codeforces 并查集_Codeforces 1263D. Secret Passwords(并查集,思维)

题意:给n个字符串,如果任意两个字符串有一个字符相同,则他们相同即属于同一集合,问总共有几个集合思路:并查集,对于每个字符串,把每个字符与第一个字符合并,因为他们肯定在同一个集合(意思是每个字符串都当成ch[0],看有几个不同的),合并之后,for(1-26)以它为根的个数,即集合的个数,貌似是思维题(并查集裸题)代码:#include #include #include #include #in...

weixin_33331063的博客 303

《C#零基础入门之百识百例》(七)算数运算符 -- 圆的面积

运算符是一种告诉编译器执行特定的数学或逻辑操作的符号。C# 有丰富的内置运算符,本文带你学习C# 中算数运算符。

陈言必行 -- Unity游戏开发领域优质博主 3619

【多目标跟踪】ByteTrack复现(有yolov5例子)

1.环境 ubuntu16.04 cuda10.1 cudnn7 python3.6 Cython matplotlib>=3.2.2 numpy>=1.18.5 opencv-python>=4.1.2 Pillow PyYAML>=5.3 scipy>=1.4.1 tensorboard>=2.2 torch>=1.7.0 torchvision>=0.8.1 tqdm>=4.41.0 seaborn>=0.11.0 easydic

聿默的博客 1万+

codeforces并查集_1263D

本题可以使用并查集来进行解答,我们可以将一个字符串中所有的字符与第一个字符合并,再在最后进行一遍检查。给你n个字符串,如果两个字符串中拥有相同的字符,则他们属于同一集合,问你有多少个集合。

triangle_617的博客 414

基于LSTM的股票价格预测系统,Django框架,Web的股票预测系统,训练+数据集+源码(无需安装环境版)

股票价格预测是投资领域的一个重点关注课题。由于股票价格受到诸多非线性因素的影响,得到精确的预测结果较为困难。使用 PyCharm 作为代码开发工具,通过下载好的股票金融数据,进行数据预处理,构建 LSTM 网络模型,训练模型并训练出最优模型,输出股票未来收盘价波动的走势,最后在训练好的模型上,设计股票模型应用系统,运行效率较高,对股票预测具有一定的积极意义。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 963

【并查集+贪心】 CodeForces - 731C - Socks

【并查集+贪心】 CodeForces - 731C - Socks 输入第一行一个zz有n只袜子,要在接下来的m天穿,袜子有k种颜色 第二行是n只袜子的颜色 接下来m行是每天穿的两只袜子的编号输出至少要改变几只袜子的颜色才能使这个zz不会因为穿了两只不同颜色的袜子而被嘲笑

xuziling_的博客 337

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

codeforces 并查集_CodeForces - 731C Socks (并查集+贪心)

NoteIn the first sample, Arseniy can repaint the first and the third socks to the second color.In the second sample, there is no need to change any colors.题目大意是说,妈妈为孩子准备的袜子(都有自己的序号,不同序号代表不同颜色)。然后给出袜子的...

weixin_32340873的博客 254

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

CodeForces 731C - Socks ( 并查集 + 贪心 )

题意 Arseniy的所有袜子按照1-n编号, 每个袜子的颜色标记为c[i], 现在给出n个袜子和m天的穿袜子方案, 至多有k种颜色( 感觉是无用条件 ), 现在要求她每天穿着方案的两只袜子同色, Arseniy可以将其涂色改变颜色, 求最少的涂色数量 思路 比较容易想到并查集 将每天穿着方案中的两只袜子合并到同一个根节点下, 同一...

a7152326的博客 182

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

CodeForces - 731C Sock(并查集+贪心+STL优化)

Socks Time limit 2000 ms Memory limit 262144 kB Arseniy is already grown-up...

From now on...的Blogs 366

CodeForces - 731C Socks(并查集,贪心)

题目: C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Arseniy is already grown-up and independe

riba2534的博客 743

CodeForces - 731C Socks(并查集)(贪心)

Socks Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Ars

Darkarts 1494

CodeForces 731 C.Socks(贪心+并查集)

Description m天n只袜子,袜子有k种颜色,每次操作可以将任一只袜子染成这k种颜色的任一种,要求第i天只能穿第li只和第ri只袜子,问最少操作多少次可以使得每一天穿的袜子颜色都相同 Input 第一行三个整数n,m,k分别表示袜子数,天数和颜色数,之后n个整数c[i]表示第i只袜子的颜色,最后m行每天两个整数li和ri表示第i天要穿的两只袜子(2 Output 输出最少操作数使得

ZSQ 738

CodeForces - 731C Socks(贪心+并查集+STL)

Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy’s clothes....

Than-的博客 685

codeforces 731C Socks

题目链接:http://codeforces.com/problemset/problem/731/C题意:给出n只袜子,m天,k种颜色(感觉没什么用)。接下来给出每只袜子的颜色。然后给出m天里第i天要穿的袜子,要求同一天穿的袜子都是一种颜色。问一次性给袜子染色最少需要染几只。(可以染成任何颜色。)分析:并查集加贪心。首先把所有需要一个颜色的袜子弄到一个集合里(并查集并起来),然后每个集合选择其中颜

Fistice的博客 396

【Codeforces】Round #376 (Div. 2)

http://codeforces.com/contest/731 不发题面了,自己点链接 总结一下 考场上 原以为这次要加很多raiting。。。 但FST狗记邓,只加了58rating 总结一下 ABC切得很快(保持) B题WA了2发不应该,没有想清楚 F题写了大暴力,但不优美,虽然过了P,但就没有想了,很严重问题,得意忘形,虽然知道FST,但有侥幸心里,真...

weixin_33714884的博客 136

Codeforces #Round 376 部分题解

A: 题目传送门:http://codeforces.com/problemset/problem/731/A 直接根据题意模拟即可 1 #include "bits/stdc++.h" 2 3 using namespace std ; 4 typedef long long QAQ ; 5 6 char s[ 1010 ] ; 7 8 inl...

weixin_30950607的博客 143

【codeforces 731C 】【并查集+贪心 或者dfs搞连通分支 】【有n只袜子,k种颜色,在m天中,左右脚分别穿下标为l,r的袜子,问最少修改几只袜子颜色,可以使每天穿的袜子左右两只都同色】

传送门:http://codeforces.com/contest/731/problem/C 描述: 题意: 有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色。 思路一: 并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜

魔笛手的博客 2474
上一篇: codeforces 727c 交互式输入(水)
下一篇: codeforces 731F 暴力(前缀和)
Masamiiiii
博客等级 码龄11年 32粉丝 625原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值