A11864 | Posterized
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $i$ -th integer represents the color of the $i$ -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 $k$ , 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 $k$ 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.
Their algorithm will be tested on an array of integers, where the $i$ -th integer represents the color of the $i$ -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 $k$ , 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 $k$ 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.
输入格式
The first line of input contains two integers $n$ and $k$ ( $1 \leq n \leq 10^5$ , $1 \leq k \leq 256$ ), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ( $0 \leq p_i \leq 255$ ), where $p_i$ is the color of the $i$ -th pixel.
The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ( $0 \leq p_i \leq 255$ ), where $p_i$ is the color of the $i$ -th pixel.
输出格式
Print $n$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
输入输出样例
输入 #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 $2$ belongs to the group $[0,2]$ , with group key $0$ .
Color $14$ belongs to the group $[12,14]$ , with group key $12$ .
Colors $3$ and $4$ belong to group $[3, 5]$ , with group key $3$ .
Other groups won't affect the result so they are not listed here.
Color $2$ belongs to the group $[0,2]$ , with group key $0$ .
Color $14$ belongs to the group $[12,14]$ , with group key $12$ .
Colors $3$ and $4$ belong to group $[3, 5]$ , with group key $3$ .
Other groups won't affect the result so they are not listed here.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted