Posterized CodeForces - 980C (思维)

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≤1051k2561≤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.

题意:给出长为n的数列,现在,让你来划分区间,划分区间后,区间中的每个值都将会变为他所在区间的最小的值。每个区间的长度不超过k,求字典序最小的划分方案

思路:首先,判断一个数是否在区间里面。如果不在,那么,要使这个值最小,就要把这个值当做最大值,向左划区间!不过,需要注意的是,我们在向左划区间时,有可能你划到的区间已经在别的区间了!所以,一但遇到已经处于其他区间的数,那么这个数就包含在上一个区间了,所以你的区间的最小值向右挪一位。

#include "iostream"
#include "cstring"
using namespace std;
const int Max=1e5+10;
int vis[Max],a[Max];
int main()
{
    ios::sync_with_stdio(false);
    memset(vis,-1,sizeof vis);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(vis[a[i]]==-1){//判断是否已经处于区间之中
            int t=a[i]-k+1;//如果不在区间中,那么最小可能值为t
            if(t<0) t=0;
            while(vis[t]!=-1&&vis[t]!=t) t++;//如果这个数已经处于其他区间了,那么,t++;
            for(int j=t;j<=a[i];j++)//将其他点覆盖为t 划分区间完毕
                vis[j]=t;
        }
    }
    for(int i=0;i<n;i++){
        cout<<vis[a[i]];
        if(n!=n-1)
            cout<<" ";
    }
    cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80578652