Posterized
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.
Input
The first line of input contains two integers n
and k (1≤n≤105, 1≤k≤256
), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains n
integers p1,p2,…,pn (0≤pi≤255), where pi is the color of the i
-th pixel.
Output
Print n
space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
Examples
Input
4 3 2 14 3 4
Output
0 12 3 3
Input
5 2 0 2 1 255 254
Output
0 1 1 254 254
Note
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.
模拟题,题目意思是("▔□▔)嗯?就是给你n个数,把它们分个组,每组元素不能超过k个,每组里面选最小的数其实就是最左边的数输出。
嗯。。我是这么做的
1.首先判断这个数前面k-1个数是不是都没有被记录过 是的话那么就直接从那前面k-1一个数开始
2.发现被记录过了,那么就判断前面离x最近的前面k-1个数中哪个数被标记
3.如果这个数被标记 那么就算算他们这一组还缺多少人 够不够到x如果够就for循环都标记下
4.不够的话从从那个被标记的数+1开始for循环表示都是那个数字+1
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Scanner sc = new Scanner (System.in);
public static void main(String[] args) {
int n = sc.nextInt();
int k = sc.nextInt();
int []arr = new int[300];
for(int i = 0; i <= 256 ; i ++)
arr[i] = -1;
Vector<Integer>vec = new Vector<Integer>();
for(int i = 1; i <= n ; i ++)
{
int x = sc.nextInt();
if(arr[x] == -1)
{
int t = Math.max(0, x - k + 1);
int u = -1;
for(int j = t ; j <= x; j ++)
{
if(arr[j] != -1) {
u = j;
break;
}
}
if(u == -1)//表示这一路走来都是正常的那么就标记
{
for(int j = t; j <= x; j++)
arr[j] = t;
}
else //只要中间有人 那么就从这个人开始这边要分情况
{
int o = -1;
for(int j = x; j >= t; j--)
{
if(arr[j] != -1) {
o = j;
break;
}
}//知道这个人的情况!!!!!!
int gg = o - arr[o] + 1;//表示有几个了
if(gg + x - o <= k)
{
for(int j = o + 1; j <= x; j++)
arr[j] = arr[o];
}else
{
for(int j = o + 1; j <= x; j ++)
arr[j] = o + 1;
}
}
}
vec.add(arr[x]);
// for(int y = 0; y <= 14; y ++)
// System.out.println(y + " " + arr[y]);
}
int size = vec.size();
for(int i = 0; i < size; i++)
{
if(i != size - 1)
System.out.println(vec.get(i) + " ");
else
System.out.println(vec.get(i));
}
}
}