Codeforce 980 C. Posterized 思维

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.


题意: 就是让我们把数串中每一个元素的值尽可能压缩 让数串中的值 尽可能小 变小的规则就是最多可以把连续的k个元素变成一个值

让我们求出信息压缩后字典序最小的数组是什么

分析:非常神奇的一个题 我们发现如果要字典序最小 其实就是尽可能地让每个值往前找k个元素 包括他自己 看看如果可以包括他本身 就用最前面的元素 标记 因为这个元素属于在k步之内的某个标号 那么他一定能找到这个相同标号的区间的起始元素 这结果数组为a 也就是找到a[i] = i的节点 如果不能找到 说明在k步之外 也就是不可能标号为那个元素了 如果没有起点元素 那么就用里他最近的没有被标记的元素去标记他 满足题目约束条件

开始没想到这一点 就导致写了一个很长和麻烦的代码 orz.....

复杂度O(1e5*256)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	static final int maxn = (int)(1e5+10);
	static int[] ans = new int[maxn];
	public static void main(String[] args) {
		Scanner sc = new Scanner(new BufferedInputStream(System.in));
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));	
		int n,k;
		n = sc.nextInt();
		k = sc.nextInt();
		Arrays.fill(ans, -1);
		for(int i=1;i<=n;i++) {
			int t = sc.nextInt();
			if(ans[t]==-1)//这里也要注意就是如果这个元素已经被赋值过了 就不必再计算啦 
			for(int s = Math.max(0, t-k+1);s<=t;s++) {
				if(ans[s]==-1||ans[s]==s) {
					for(int j = s;j<=t;j++)ans[j] = s;
					break;
				}
			}
			out.print(ans[t]+" ");
		}
		
		out.flush();
	}

}

猜你喜欢

转载自blog.csdn.net/qq_33859479/article/details/80372590