zoukankan      html  css  js  c++  java
  • T

    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 (1n10^51k256), the number of pixels in the image, and the maximum size of a group, respectively.

    The second line contains n integers p1,p2,,pn (0pi255), 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代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=1e5+5;
     4 int n,k,vis[256],a[maxn];
     5 int main(){
     6     memset(vis,-1,sizeof(vis));//先初始化为-1,表示该区间还未使用
     7     cin>>n>>k;
     8     for(int i=0;i<n;++i){
     9         cin>>a[i];
    10         if(vis[a[i]]==-1){//判断是否已处于区间中,这里表示未被使用
    11             int t=a[i]-k+1;//如果不在区间中,那么最小值可能为t,最大值不超过本身a[i]
    12             if(t<0)t=0;//如果t小于0,那么t最小为0
    13             while(vis[t]!=-1&&vis[t]!=t)t++;//如果这个数已经处于其他区间了,那么t++,找出该区间可以使用的最小值,最大等于t本身
    14             for(int j=t;j<=a[i];++j)vis[j]=t;//将其他点覆盖为t,同样最大到本身,划分区间完毕
    15         }
    16     }
    17     for(int i=0;i<n;++i)//输出每个数对应的区间
    18         cout<<vis[a[i]]<<(i==n-1?'
    ':' ');
    19     return 0;
    20 }
  • 相关阅读:
    php学习笔记之一维数组
    MVC开发人员必备的五大工具
    ASP.NET MVC 3和Razor中的@helper
    Oracle表空间不足ORA-01654
    oracle创建计划任务
    淘宝下单高并发解决方案
    网站集成QQ登录功能
    jquery的一个模板引擎-zt
    Asp.net gzip压缩的启用
    Windows7下面手把手教你安装Django
  • 原文地址:https://www.cnblogs.com/acgoto/p/9316459.html
Copyright © 2011-2022 走看看