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.
The first line of input contains two integers nn and kk (1≤n≤1051≤n≤105, 1≤k≤2561≤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 (0≤pi≤2550≤pi≤255), where pipi is the color of the ii-th pixel.
Print nn space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.
4 3 2 14 3 4
0 12 3 3
5 2 0 2 1 255 254
0 1 1 254 254
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个数(<=100000个),0<=每个数<=255
问你怎么划分区间,区间为m,要求划出来的区间左端点越小越好。
重点在于并不是把所有的数据都算出,100000很大了
只要得到像素0-255时划分区间的左端点
从头往后找,如果0-255都找到了,那么b[a[i]]!=-1,就不用寻找
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int a[maxn];
int b[maxn];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(a,0,sizeof(a));
memset(b,-1,sizeof(b));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(b[a[i]]==-1)
{
int t=a[i]-m+1;
for(int j=t;j<=a[i];j++)
{
if(j<0)
continue;
if(b[j]==-1||b[j]==j)
{
t=j;
break;
}
}
for(int j=t;j<=a[i];j++)
b[j]=t;
}
}
for(int i=1;i<=n;i++)
{
if(i==1) printf("%d",b[a[i]]);
else printf(" %d",b[a[i]]);
}
printf("\n");
}
return 0;
}