CF980C.Posterized

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

易卜拉欣教授为他的算法课布置了期末作业。他要求学生实现“海报化图像滤镜”(Posterization Image Filter)。

该算法将在一个整数数组上进行测试,其中第 ii 个整数表示图像中第 ii 个像素的颜色。图像是黑白图像,因此每个像素的颜色值为 00 到 255255(含端点)之间的整数。

为实现该滤镜,学生需将黑白颜色范围 [0,255][0, 255] 划分为若干组连续的颜色,并在每组中选定一个颜色作为该组的“键值”。为保留图像细节,每组的大小不得超过 kk,且每个颜色值必须且仅能属于一个组。

最后,学生需将数组中每个像素的颜色替换为其所属组的键值。

为更直观地理解该滤镜的效果,下图展示了一只晒太阳的海龟图像,从左至右依次应用了 kk 值递增的海报化滤镜:

为便于最终答案的校验,易卜拉欣教授要求学生以某种方式划分组并分配键值,使得最终所得数组在字典序意义上尽可能小。

输入格式

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

The second line contains nn integers p1,p2,…,pnp_1, p_2, \dots, p_n (0≤pi≤2550 \leq p_i \leq 255), where pip_i is the color of the ii-th pixel.

输入的第一行包含两个整数 nn 和 kk(1≤n≤1051 \leq n \leq 10^5,1≤k≤2561 \leq k \leq 256),分别表示图像中的像素数量和组的最大大小。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n(0≤pi≤2550 \leq p_i \leq 255),其中 pip_i 表示第 ii 个像素的颜色。

输出格式

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

输出 nn 个空格分隔的整数;即应用海报化(Posterization)滤镜后,字典序最小的可能图像数组。

输入输出样例

  • 输入#1

    4 3
    2 14 3 4

    输出#1

    0 12 3 3
  • 输入#2

    5 2
    0 2 1 255 254

    输出#2

    0 1 1 254 254

说明/提示

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.

第一组样例的一种可能的颜色分组与按键分配方式如下:

颜色 22 属于区间 [0,2][0,2] 所对应的组,该组的键值为 00。

颜色 1414 属于区间 [12,14][12,14] 所对应的组,该组的键值为 1212。

颜色 33 和 44 属于区间 [3,5][3, 5] 所对应的组,该组的键值为 33。

其余分组不会影响结果,因此此处未列出。

输入解题思路,AI测评打分。不知道怎么写?

首页