( 貪心/並查集)Codeforces Round #480 (Div. 2) 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

題解:

題意:在區間[0,256]中,連續的數字分組,每組不多於k個成員,取每組中最小的那個值輸出,求總字典序最小的輸出

解:對於數p,往前k個數中裏尋找分組的最小左邊界,如果1.不存在已分好的組,直接輸出p-k+1;2.存在已分好的組,且合併該組後的數組大小<=k,輸出被合併組的左邊界;3.存在已分好的組,且合併該組後的數組大小>k,輸出被合併組的右邊界+1;

貪心:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
	int n,k;
	cin>>n>>k;
	int flag;
	int p[100005],vis[260];
	for(int i=0;i<n;i++)
	{
		cin>>p[i];
	}
	memset(vis,-1,sizeof(vis));
	int tmp,j,minn;
	for(int i=0;i<n;i++)
	{
		flag=0;
		if(vis[p[i]]!=-1)
		{
			cout<<vis[p[i]]<<" ";
		}
		else
		{
			tmp = p[i]-1;
			minn=tmp-k+2;
			if(minn<0)
				minn=0;
			for(j=tmp;j>=minn;j--)
			{
				if(vis[j]==-1)
					continue;
				else
				{
					if((p[i]-vis[j]+1)<=k)
					{
						for(int l=j+1;l<=p[i];l++)
						{
							vis[l]=vis[j];
						}
						flag=1;
						break;
					}
					else
					{
						for(int l=j+1;l<=p[i];l++)
						{
							vis[l]=j+1;
						}
						flag=1;
						break;
					}
				}
			}
			if(flag==0)
			{
				for(int l=j+1;l<=p[i];l++)
					vis[l]=j+1;
			}
			cout<<vis[p[i]]<<" ";
		}
	}
	cout<<endl;
	return 0;
}
並查集:
#include <iostream>
using namespace std;

int sum[100005],fa[100005];
int n,k;

void init()
{
	for(int i=0;i<=256;i++)
	{
		sum[i]=1;
		fa[i]=i;
	}
}

int Find(int a)
{
	if(fa[a]==a)
		return a;
	else
		return fa[a]=Find(fa[a]);
}

int main()
{
	cin>>n>>k;
	int p;
	init();
	for(int i=0;i<n;i++)
	{
		cin>>p;
		while(p-1>=0)
		{
			if(Find(p)!=p)
				break;
			else if(sum[Find(p-1)]+sum[Find(p)]>k)
				break;
			else
			{
				sum[Find(p-1)]+=sum[Find(p)];
				fa[p] = Find(p-1);
			}
			p--;
		}
		cout << fa[p] <<" ";
	}
	cout << endl;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章