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

T - Posterized(贪心思维)的更多相关文章

  1. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  2. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  3. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  4. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  5. CF980C Posterized 贪心 二十五

    Posterized time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. C. Coffee Break 贪心 思维 有点难 有意思

    C. Coffee Break 这个贪心之前好像写过,还是感觉挺难的,有点不会写. 这个题目大意是:给你一个数列n个元素,然后给你一天的时间,给你一个间隔时间d, 问你最少要用多少天可以把这个数列的所 ...

  7. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  8. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  9. C. Brutality Educational Codeforces Round 59 (Rated for Div. 2) 贪心+思维

    C. Brutality time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

随机推荐

  1. Linux下C编程入门(7)

    Linux下项目同步工具介绍git和github 一.远程仓库工具github 1. 一.本地操作工具git 1.

  2. Jquery为DIV添加点击事件,Jquery为a标签超链接添加点击事件

    <div>1</div> <div>2</div> <div>3</div> <div>4</div> ...

  3. Codeforces Round #374 (Div. 2) C DAG上dp

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  4. Balanced Binary Tree (二叉树DFS)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. springboot + mybatis 完成图片上传并保存到数据库

    添加依赖 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons- ...

  6. 【转】关于easyui tab 加载 js ajax 不走后台的问题, 怕找不到 以防万一

    一直以来群里里面很多人反应,在用tab加载界面的时候,界面里面的js不会执行.今天在此说明一下原因. 不管是window,dailog还是tab其实质最终都是继承了panel.panel有两种方式展示 ...

  7. Java中如何获取spring中配置的properties文件内容

    有2种方式: 一. 1.通过spring配置properties文件 [java]  <bean id="propertyConfigurer"      class=&qu ...

  8. 免费好用的Microsoft iSCSI Software Target 3.3

    我们在搭建Windows群集的时候往往会使用到IP-SAN.但是面对昂贵的硬件IP-SAN,我们在学习和实验的环境中更加多的用到的往往是软件模拟的IP-SAN.今天就给大家介绍一个微软出品的免费iSC ...

  9. Linux---有关dig命令的有用脚本

    这里直接给出脚本以及运行的效果图,主要推断了一下cdn然后能够直接过滤url.默认就是dig +域名 +short. 脚本qdig(随便能够取一个名字)例如以下: #!/usr/bin/env bas ...

  10. CLR-基元类型以及溢出检查 (CLR-Via-C#) 类型基础

    CLR-基元类型以及溢出检查   =========(CLR via C#阅读笔记)======== 基元类型(primitive type): 基元类型也不做过多的解释,举个例子即可清晰的辨别 在j ...