Description

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≤10^5, 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.

解题思路:题目的意思就是以每一个像素的大小x为区间右端点,从[x-k+1,x]这个区间中尽可能选择一个比较小的值,作为这个区间的key值;用vis数组来判断这个区间(初始值都为-1)是否已经占用,如果已被占用即vis[x]!=-1,则不用处理;如果没有占用,遍历这个区间找最小可能代替的值,最大为x,然后依次用这个最小值t来覆盖区间[x-k+1,x]中还没有被覆盖的元素,最后输出每个数对应区间的最小key值即可。贪心思维填充区间key值~

AC代码:

 #include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int n,k,vis[],a[maxn];
int main(){
memset(vis,-,sizeof(vis));//先初始化为-1,表示该区间还未使用
cin>>n>>k;
for(int i=;i<n;++i){
cin>>a[i];
if(vis[a[i]]==-){//判断是否已处于区间中,这里表示未被使用
int t=a[i]-k+;//如果不在区间中,那么最小值可能为t,最大值不超过本身a[i]
if(t<)t=;//如果t小于0,那么t最小为0
while(vis[t]!=-&&vis[t]!=t)t++;//如果这个数已经处于其他区间了,那么t++,找出该区间可以使用的最小值,最大等于t本身
for(int j=t;j<=a[i];++j)vis[j]=t;//将其他点覆盖为t,同样最大到本身,划分区间完毕
}
}
for(int i=;i<n;++i)//输出每个数对应的区间
cout<<vis[a[i]]<<(i==n-?'\n':' ');
return ;
}

最新文章

  1. iOS开发中遇到的错误整理 - 集成第三方框架时,编译后XXX头文件找不到
  2. lua 快速排序
  3. Linux设置环境变量(解决许多命令找不到)
  4. Nginx下配置ThinkPHP的URL Rewrite模式和pathinfo模式支持
  5. MFC中release版本和debug版本区别
  6. jquery cookie用法(获取cookie值,删除cookie)
  7. 关于ajax提交的公共接口的一大用处
  8. C# 迪杰斯特拉(Dijkstra)算法
  9. 东正王增涛浅析OA信息化整合平台系统在企业中的应用价值
  10. 一个php技术栈后端猿的知识储备大纲
  11. 如何设置静态IP
  12. 浅谈 RxAndroid + Retrofit + Databinding
  13. java.lang.UnsatisfiedLinkError解决方法汇集(转载)
  14. root用户无法访问Mysql数据库问题的解决
  15. Spring MVC 集成Disconf
  16. Code::Blocks 导入Makefile工程
  17. c# WinFo判断当前程序是否已经启动或存在的几种方式
  18. VS2003在解决方案范围内搜索卡死问题的解决
  19. golang 垃圾回收机制
  20. Anroid 4大组件之android.app.Service

热门文章

  1. cogs——21. [HAOI2005] 希望小学
  2. hadoop(2)hadoop配置
  3. wget: unable to resolve host address “mirrors.163.com” 的解决办法
  4. Java发送邮件示例
  5. Eclipse 远程tomcat调试程序
  6. IE插件
  7. 机器学习笔记——SVM
  8. 浏览器判断是否安装了ios/android客户端程序
  9. 阿里云CentOS7.3搭建多用户私有git服务器(从安装git开始)
  10. [Python]基于权重的随机数2种实现方式