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

本文介绍了一种海报化滤镜算法的实现方法,该算法将图像颜色范围分为连续组,并选择每组的一个颜色作为关键颜色,以保留图像细节。通过示例说明了如何根据不同的参数设置获得最佳效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 ii-th integer represents the color of the ii-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 kk, 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 kk 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 nn and kk (1n1051≤n≤105, 1k2561≤k≤256), the number of pixels in the image, and the maximum size of a group, respectively.

The second line contains nn integers p1,p2,,pnp1,p2,…,pn (0pi2550≤pi≤255), where pipi is the color of the ii-th pixel.

Output

Print nn space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.

Examples
Input
Copy
4 3
2 14 3 4
Output
Copy
0 12 3 3
Input
Copy
5 2
0 2 1 255 254
Output
Copy
0 1 1 254 254

Note

One possible way to group colors and assign keys for the first sample:

Color 22 belongs to the group [0,2][0,2], with group key 00.

Color 1414 belongs to the group [12,14][12,14], with group key 1212.

Colors 33 and 44 belong to group [3,5][3,5], with group key 33.

Other groups won't affect the result so they are not listed here.

分析:并查集(注意按字典序输出)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
int a[1000000];
int pre[1000000];
int vis[1000000];
int n,k;
void out(int i,int x)
{
    if(i==n)
        printf("%d\n",x);
    else
        printf("%d ",x);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=0;i<=255;i++)pre[i]=i,vis[i]=0;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            if(x==0)
                out(i,0);   ///0直接输出0
            else if(pre[x]!=x||vis[x])
                out(i,pre[x]);   ///当前点已经被前面的点包含直接输出
            else
            {
                int idex;
                for(idex=x-1;;idex--)
                    if(pre[idex]!=idex||idex<0)
                        break;
                if(x-idex+1<=k)
                {
                    int ans;
                    if(x-pre[idex]+1<=k)   ///如果当前点与当前点距离最近的区间根节点的距离小于等于K区间合并
                    {
                        out(i,pre[idex]);
                        ans=pre[idex];
                    }
                    else
                    {
                        out(i,idex+1);    ///反之更新当前点区间
                        ans=idex+1;
                    }
                    for(int j=idex+1;j<=x;j++)
                        pre[j]=ans;
                    vis[ans]=1;
                }
                else
                {
                    int j=x-k+1;   ///当前点的root与前面的区间没有交集
                    out(i,j);
                    for(int l=j;l<=x;l++)
                        pre[l]=j;
                    vis[j]=1;
                }
            }
        }
    }
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值