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

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));
    	}
    }
 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章