Codeforces Round #480 (Div. 2): 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
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


题意:

给你n个数字代表像素点的颜色,范围[0, 255],你可以将值相邻的若干个数字分成一组,之后将这些数字全部变成最小的那个,例如可以将[0,1,2,3]分成一组,[4]单独一组,[5,6,7,8,9]分成一组,这样所有的k∈[5,9]都会变成5,k∈[0,3]都会变成3,k=4不变,要求每个组的长度不能超过k,求出如何分组使得给你的n个数字按照上述规则变换后得出的新序列字典序最小

思路:

直接贪心,如果这个数没有被分在某个组中,则尽可能把这个数变小,也就是尽可能让这个数字成为某个尽可能长的组中最大的那个就ok,如果这个数已经被分组了就直接输出组中最小值


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100005], flag[333];
int main(void)
{
	int n, k, i, j, p;
	scanf("%d%d", &n, &k);
	for(i=1;i<=n;i++)
		scanf("%d", &a[i]);
	memset(flag, -1, sizeof(flag));
	for(i=1;i<=n;i++)
	{
		if(flag[a[i]]!=-1)
			printf("%d ", flag[a[i]]);
		else
		{
			for(j=a[i];j>=0;j--)
			{
				if(flag[j]!=-1)
				{
					if(a[i]-flag[j]<=k-1)
					{
						for(p=a[i];p>=j;p--)
							flag[p] = flag[j];
					}
					else
					{
						for(p=a[i];p>=max(a[i]-k+1, j+1);p--)
							flag[p] = max(a[i]-k+1, j+1);
					}
					break;
				}
			}
			if(j==-1)
			{
				for(p=a[i];p>=max(a[i]-k+1, 0);p--)
					flag[p] = max(a[i]-k+1, 0);
			}
			printf("%d ", flag[a[i]]);
		}
	}
	puts("");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值