C. Posterized(Div.2)(并查集)

Ferry工单平台部署 ferry工单平台部署 阅读详情

链接:http://codeforces.com/contest/980/problem/C

C. Posterized
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.

Their algorithm will be tested on an array of integers, where the ii-th integer represents the color of the ii-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).

To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than kk, and each color should belong to exactly one group.

Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.

To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing kk to the right.

To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.

Input

The first line of input contains two integers nn and kk (1≤n≤1051≤n≤105, 1≤k≤2561≤k≤256), the number of pixels in the image, and the maximum size of a group, respectively.

The second line contains nn integers p1,p2,…,pnp1,p2,…,pn (0≤pi≤2550≤pi≤255), where pipi is the color of the ii-th pixel.

Output

Print nn space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.

Examples
input
Copy
4 3
2 14 3 4
output
Copy
0 12 3 3
input
Copy
5 2
0 2 1 255 254
output
Copy
0 1 1 254 254
Note

One possible way to group colors and assign keys for the first sample:

Color 22 belongs to the group [0,2][0,2], with group key 00.

Color 1414 belongs to the group [12,14][12,14], with group key 1212.

Colors 33 and 44 belong to group [3,5][3,5], with group key 33.

Other groups won't affect the result so they are not listed here.

大意:在区间[0,255]中,连续的数字分组,每组不多于k个成员,取每组中最小的那个值输出,求总字典序最小的输出

思路:利用并查集,[0,255]的每个值都计数为1

1、如果该值与父节点不相等,则直接输出父节点(因为已经分好组了)

2、如果该值的计数与该值前面元素的父节点计数相加大于m,则直接输出该值(因为这两个联立计数会大于m)

3、如果该值的计数与该值前面元素的父节点计数相加小于等于m,则该值可以与前面元素联立

以下是AC代码:


#include <cstdio>   
#include <iostream> 
#include <algorithm> 
#include <cmath> 
#include <cstdlib> 
#include <cstring> 
#include <vector> 
#include <list> 
#include <map> 
#include <stack> 
#include <queue> 
using namespace std; 
#define ll long long
int f[100005];
int sum[100005];
int n,m;
void set()
{
	for(int i = 0;i <= 260;i++)
	{
		f[i] = i;
		sum[i] = 1;
	}
}
int father(int x)
{
	if(f[x] == x)
		return x;
	else
		return f[x] = father(f[x]);
}
void Union(int x,int y)
{
	int f1 = father(x);
	int f2 = father(y);
	if(f1 != f2)
	{
		f[max(f1,f2)] = min(f1,f2);
		sum[min(f1,f2)]++;
	}
}
int main() 
{
	while(cin >> n >> m)
	{
		set();
		while(n--)
		{
			int x;
			cin >> x;
			while(x - 1 >= 0)
			{
				if(father(x) != x)
					break;
				else if(sum[father(x-1)]+sum[f[x]] > m)
					break;
				else
				{
					sum[father(x-1)]+=sum[f[x]];
					f[x] = father(x-1);
				}
				x--;
			}
			cout << f[x];
			if(n == 0)
				cout << endl;
			else
				cout << " ";
		}
	}
    //cout << "AC" <<endl; 
    return 0; 
} 

过了很久,又遇到了这题,找了很久bug,以下是AC代码二:

#include <cstdio>  
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
int f[270];
int n;
int cent[270];
void init()
{
	for(int i = 0;i <= 256;i++)
	{
		f[i] = i;
		cent[i] = 1;
	}
}
int find(int x)
{
	if(f[x] == x)
		return x;
	return f[x] = find(f[x]);
}
void unio(int x,int y)
{
	int f1 = find(x);
	int f2 = find(y);
	if(f1 != f2)
		f[max(f1,f2)] = min(f1,f2);
}
int main()
{
	int n,k;
	while(scanf("%d%d",&n,&k) != EOF)
	{
		init();
		int a[n];
		int ans[n];
		for(int i = 0;i < n;i++)
			scanf("%d",&a[i]);
		for(int i = 0;i < n;i++)
		{
			for(int j = a[i]-1;j >= 0&& j >= a[i]-k;j--)
				if(cent[find(j)]+cent[find(a[i])] <= k && find(j) != find(a[i]))//当时没有写find(j) != find(a[i]),这样可能就会出现两个相同组再联立一次的情况,导致cent无故加倍(找了很久很久才找到的bug)
				{
					int x1 = find(a[i]);
					int x2 = find(j);
					unio(x1,x2);
					cent[find(a[i])] = cent[x1]+cent[x2];
				}
				else
					break;
			ans[i] = find(a[i]);
		}
		for(int i = 0;i < n-1;i++)
			cout << ans[i] << ' ';
		cout << ans[n-1] <<endl;
	}
	//cout << "AC" <<endl;
	return 0;
}



知识见闻 - Salesforce公司发展史 不过,通过将自己开发的产品与公司收购的其它云产品进行结合,Salesforce可以为用户提供更多的产品种类,Salesforce可以继续强化自己的平台和产品套件属性,这一平台和套件的发展规模已经远远超出了一个单一的CRM的规模。他们围绕一个相对简单的产品向用户兜售云的概念,然后,他们开始开发其它产品,并寻找培育一个不断增长的生态系统的方法,最理想的情况是能驱动Salesforce的营收增长。现在,Salesforce通过打造更多的用例并改进自己产品的方式来直接应对竞争对手的竞争,从而捍卫自己现在的地位。 阅读详情

相关推荐

5分钟搞懂语义通信:如何用深度学习让AI理解你的真实意图?

本文深入解析了语义通信如何利用深度学习技术,实现从传统‘比特保真’到‘语义保真’的范式转移。通过介绍基于神经网络的端到端系统架构、编解码技术及实战案例,阐明AI如何理解并高效传递信息核心意图,为6G时代的人机交互奠定基础。

weixin_29191669的博客 263

NEUQ-acm第二期训练Week1——代码源div2

NEUQ-acm第二期训练Week1——代码源div2

夕颜的博客 318

Android双目摄像头(RGB+IR)实时预览与人脸活体检测实战指南

本文详细介绍了Android双目摄像头(RGB+IR)实时预览与人脸活体检测的实战指南。通过双摄同步预览、人脸检测与活体判断等技术实现,提升安全认证的准确性和鲁棒性。文章还提供了开发环境搭建、常见问题解决方案及性能优化建议,帮助开发者快速掌握双目摄像头在Android平台的应用。

vulkan6gpu的博客 691

NEUQ-acm第二期训练Week3——代码源div2

NEUQ-acm第二期训练Week3——代码源div2

夕颜的博客 278

百度地图街景图像批量获取

由于个人研究需要在最近试了一下使用API获取百度地图街景图片,在此介绍一下基于我个人需求的百度街景批量获取方法,本文以Python编码作为主要方法,如果对零编码工具有需要的可以关注一下UA城市分析小组刚刚发布的街景图片获取工具。 街景图像用作区域细粒度空间指标测度需要解决几个问题。 1. 首先就是调用额度的限制,现在百度对于个人认证开发者每天可用的街景图像调用额度为100张,在不考虑购买额度上限(1万张600元,其实有经费的前提下可以直接考虑钞能力)的情况下动辄几千上万张的爬取数量会大大增加需要爬取的时间周

boycetoon29的博客 1万+

S32K144驱动TPS929120避坑实录:从原理图设计到PWM调光漏电流的完整解决流程

本文详细解析了S32K144驱动TPS929120在车规LED驱动中的常见问题与解决方案,重点解决了PWM调光漏电流的行业难题。从原理图设计、FlexWire通信到软件调试,提供了一套完整的工程化避坑指南,特别针对车规MCU应用中的温度漂移、地址冲突等痛点给出了实用建议。

weixin_30872157的博客 561

Codeforces Round #480 (Div. 2) C. Posterized

C. Posterizedtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputProfessor Ibrahim has prepared the final homework for his algorithm’s class. He asked ...

Become hunger become strong 519

MATLAB AppDesigner 中TextArea保留原有信息并换行显示提示信息

App Designer 中的文本区域TextArea组件,一般可以用来作为matlab app设计中的信息显示。 app.TextArea.Value = 'Hello Matlab'; Value可以是字符向量、字符向量元胞数组、字符串数组或一维分类数组。这样直接给TextArea赋值,每次赋值都会消除先前的信息,然后显示新的信息。如果想要在app实现保留原有信息、多行信息的输出,在MathWorks官方帮助中心给出了一种方法: cellArrayText{1} = sprintf('%s\n

我是麻瓜喔的博客 1万+

Codeforces Round #480 (Div. 2) C. Posterized

C. PosterizedProfessor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.Their algorithm will be tested on an array o...

s540239976的博客 474

如何让Arduino IDE支持你手里的STM32 nucleo开发板

Arduino IDE开发环境在默认情况下不支持STM32 NUCLEO开发板,只有通过添加附加开发板管理包才能使用。目前为止,只有一部分板子支持使用Arduino IDE开发环境进行开发,分别是 Nucleo F207ZG, Nucleo F429ZI, Nucleo F030R8, Nucleo F091RC, Nucleo F103RB, Nucleo F303RE, Nucl...

mzw72188552的专栏 8972

【流复制环境PostgreSQL-14.1到PostgreSQL-16.1大版本升级】

PostgreSQL大版本会定期添加新特性,这些新特性通常会改变系统表的布局,但内部数据存储格式很少改变。pg_upgrade通过创建新的系统表和重用旧的用户数据文件来执行快速升级。pg_upgrade升级主要有三种用法:1、使用pg_upgrade拷贝升级。2、使用pg_upgrade链接升级(带有- -link选项),- -link较快,但是启动新版本后修改了数据文件,再启动旧版本可能数据损坏,回滚较麻烦,所以尽量避免使用link参数进行升级。

小怪兽的博客 2926

Codeforces Round #480 (Div. 2): C. Posterized(贪心)

C. Posterizedtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputProfessor Ibrahim has prepared the final homework for his algorithm’s class. He asked ...

加载中... 804

关于React Native Android找不到node的错误

最近,按照官方文档初始化了一个React Native项目,但是在Android端运行的时候,报了如下的一个错误。 org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:recordFilesBeforeBundleCommandDebug'. Caused by: org.gradle.process.internal.ExecException: A problem occurred starting p

xiangzhihong8的专栏 4246

Codeforces Round #480 (Div. 2) C. Posterized (贪心)

Codeforces Round #480 (Div. 2) C. Posterized (贪心) 思路:每次贪心地选从左到右选a[j]=−1a[j]=-1a[j]=−1或a[j]=ja[j]=ja[j]=j的颜色。 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7; #define mst(a,b) memse

Harris-H的博客 548

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Codeforces Round #480 (Div. 2) C - Posterized

Codeforces Round #480 (Div. 2) C - Posterized 题目地址:http://codeforces.com/contest/980/problem/C 官方题解:   题解:一共256个像素网格,可以把这个256个分组,每个分组大小&lt;=k。给出n个像素格子,要求每个像素用分组里的一个数表示,并且表示出来...

qq_42052818的博客 167

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

( 贪心/并查集)Codeforces Round #480 (Div. 2) C. Posterized

C. Posterizedtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputProfessor Ibrahim has prepared the final homework for his algorithm’s class. He asked ...

sunny crazy 347

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Codeforces #480 (Div. 2) 解题报告 C. Posterized 贪心

题意:一共有256个像素格,你可以将这些像素格分组,每个分组小于等于K。        给出n个像素格,像素格以其所在分组内的一个像素表示,就是每个分组对应一个组名(像素格的数值),求给出的数的对应的组名的字典序最小的情况。题解:1、贪心,想办法在满足条件的情况下让相应的组名取得更小的值2、分情况贪心,首先让所有的像素都未分组,然后按照给出的数一个一个地进行分组3、最简单的情况就是该组已经被分到了...

三生锁链 306

Codeforces Round #480 (Div. 2) C - Posterized 贪心

题意:给定一个序列A,(ai:【0,255】),可以组合其中连续的一些数,这些数个数不能超过m;这些数可以以最小的数作为key问这个序列A对应的字典序最小的 key 序列是啥;思路:贪心的想法,从左到右,对于当前数a[i],如果已经合入某个组,那直接输出其key;否则:为了字典序最小:我们找到它前面(包括它)的第m个数,从这个数往后找是否有一个数还没加入某个组,或者加入的某个组能够包含a[i],那...

床前明月光 269

Codeforces Round #480 (Div. 2)980C Posterized+分组类贪心

传送门:http://codeforces.com/contest/980/problem/C 参考 题意:给定n个数字,每个数在0~256间,现在给至多连续k的数分为一组,给出字典序最小的答案。 思路:贪心,对于每一个a[i],查找max(0,a[i] - k + 1)到 a[ i ]中符合vis[ t ] ==t || vis[ t ] = -1的最小数字 T ,把这个T到a[...

weixin_30737433的博客 151

【贪心】Codeforces Round #480 (Div. 2) C. Posterized

题意:让你对[0,255]这个序列任意划分成一些不重叠的子段,每个子段的大小不超过K。给你n个不超过255的数,让你将每个数替换成它所在子段的任意一个元素,使得最终这个n个数的序列的字典序最小。 p[x]代表x作为代表元素的话,其所控制的区间的最后一个元素是谁。 读入一个数a的时候,在[0,255]数轴上找它前面的离它最近的一个已被标记的数b,如果与其的距离不超过K,则a的代表元素就是b,然后...

weixin_34242658的博客 119

Codeforces Round #480 (Div. 2)

A. Links and Pearls 题解:~~~没啥说的,没有pearls也算YES!!! 1 #pragma warning(disable:4996) 2 #include<map> 3 #include<string> 4 #include<cstdio> 5 #include<cstring> 6 #incl...

weixin_30339969的博客 79
上一篇: Cyclic Components(并查集)
下一篇: P1087 FBI树
MMMMMMMW
博客等级 码龄9年 34粉丝 64原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值