CodeForces 980C

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.

实不相瞒,第一题我没读懂题, 还怀疑样例是不是错了(真的菜),半信半疑的把自己错误的思路码了一段代码,果然不对,前思后想怎么也想不出来哪里错了, 后来看了题解发现我原来是理解的错题意了(怪不得)。

代码: 有点类似于并查集,提交运行了300+, 暂时还想不到什么方式来优化。

#include <iostream>
#include <cstring>

using namespace std;

const int N = 100010;

int a[N], vis[N];

int main()
{
    int n, k;
    cin >> n >> k;
    
    for(int i = 0; i < n; i ++) scanf("%d", a + i);
    
    memset(vis, -1, sizeof vis);
    for(int i = 0; i < n; i ++) 
    {
        int res = vis[a[i]];
        if(vis[a[i]] == -1)
        {
            for(int j = a[i]; j > (a[i] - k) && j >= 0; j --)
            {
                if(vis[j] != -1) 
                {
                    if(vis[j] > a[i] - k) res = vis[j];
                    break;
                }
                res = j;
            }
            
            for(int j = res; j <= a[i]; j ++) vis[j] = res;
        }
        
        cout << res << endl;
    }
    return 0;
}

傻傻的我写的错误code~

#include <iostream>

using namespace std;

const int N = 100010;

int a[N];

int main()
{
    int n, k;
    cin >> n >> k;
    
    for(int i = 0; i < n; i ++) scanf("%d", a + i);
    int ed = (a[0] % k + 1) % k; 
    
    cout << ed << endl;
    
    for(int i = 0; i < n ; i ++)
    {
        if(a[i] - ed < 0) cout << 0 << endl;
        
        else cout << (a[i] - ed) / k * k + ed << endl;
        
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值