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

安装m_map以及用matlab绘制高精度海岸线地图 #如何用matlab绘制高精度海岸线地图 阅读详情

链接: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;
}



python 抖音采集_python爬取抖音视频的实例详解 import requestsimport jsonimport reimport osfrom pprint import pprint as ppimport queueclass DouYin:header = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*... 阅读详情

相关推荐

uniapp中renderjs实战:手把手教你实现SSE即时通讯(附完整代码)

本文通过一个完整的实战案例,详细讲解了如何在uni-app中使用RenderJS实现SSE即时通讯功能。文章剖析了RenderJS作为Vue视图层强力补充的原理,并提供了包含自动重连、状态管理等健壮性设计的完整代码,帮助开发者突破框架限制,高效集成浏览器原生API。

weixin_29005499的博客 189

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

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

夕颜的博客 318

企业级 Agent 在 K8s 上的运行模型

Kubernetes├── Agent Orchestrator(控制面)├── Agent Runtime Pods(执行面)├── Graph DSL(流程层)├── Tool Services(能力层)K8s(调度)+ Graph(流程)+ Agent(决策)+ Tool(执行)+ Guard(治理)

一起修行,不孤单。这里有编程语言、开发工具、学习方法论和踩坑笔记。用简单的话说复杂的事,陪你从小白到进阶。周更干货,欢迎一起成长! 1172

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

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

夕颜的博客 278

游戏开发实战:如何用旋转矩阵实现2D角色平滑转向(附Unity代码)

本文深入探讨了在Unity游戏开发中,如何运用旋转矩阵原理实现2D角色的平滑转向。文章从核心理论出发,提供了计算目标旋转、实现平滑插值(Lerp与SmoothDamp)的完整C#代码,并解决了与动画系统、物理引擎协同的常见难题,帮助开发者提升游戏操作手感与视觉流畅度。

weixin_29057237的博客 287

FPS游戏自动枪械识别+压枪(以PUBG为例)

使用Python3.9作为开发环境,实现一个通用的FPS游戏自动检测枪械配件+压枪宏的软件开发思路

weixin_37827742的博客 3万+

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

深入解析JESD204B Subclass 1中的SYSREF与LMFC同步机制

本文深入解析了JESD204B Subclass 1协议中的核心同步机制,重点阐述了SYSREF信号如何作为系统级参考,对齐所有设备内部的本地多帧时钟(LMFC),从而实现高速多通道数据采集系统中的确定性延迟。文章结合实战经验,详细说明了硬件设计、时序要点及软件配置流程,是解决多ADC芯片同步对齐问题的关键指南。

weixin_29271263的博客 398

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

CloudCompare 二次开发(35)——提取多条直线

使用CloudCompare与PCL编程实现的多条空间直线的提取。

点云侠的博客 439

手把手教你学PCIE(10.5)--PCIE故障排除方法的分步排查法介绍

通过以上分步排查法,你可以系统地诊断和解决 PCIe 设备的故障。每个步骤都有助于逐步缩小问题范围,最终找到并解决问题。

MHD0815的博客 1873

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

全新DEIM有效涨点改进目录 | 本专栏持续更新500+篇内容 | 包含各种顶会顶刊卷积、注意力、特征融合模块、有效特征聚合提取模块,上采样模块、下采样模块,二次创新模块、独家创新等几百种创新点改进

🚀【CVPR2025DEIM改进专栏速览】🔥 专栏提供500+篇目标检测创新改进方案,涵盖卷积/注意力/特征融合等创新改进点,均源自顶会论文及实战经验。支持多领域应用(工业/医疗/遥感等),配套完整教程和答疑服务。亮点包括: 1️⃣ 持续更新SOTA模型改进方案 2️⃣ 小白友好型实验操作指南 3️⃣ 跨学科科研支持(已助非计算机专业发表论文) 4️⃣ 提供虚拟环境配置方案。 订阅可获完整项目代码及交流群服务,适合研究生科研论文改进需求。价格呈上涨趋势,早订更优惠!

Ai缝合怪、助力小伙伴高效发论文 2050

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

Camera2Demo

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

计算机控制技术及应用

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

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

OV9734 DATASHEET,完整规格书

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

( 贪心/并查集)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

Statistics and Machine Learning Toolbox

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

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、付费专栏及课程。

余额充值