C. Posterized(Div.2)(並查集)

鏈接:http://codeforces.com/contest/980/problem/C

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.

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

思路:利用並查集,[0,255]的每個值都計數爲1

1、如果該值與父節點不相等,則直接輸出父節點(因爲已經分好組了)

2、如果該值的計數與該值前面元素的父節點計數相加大於m,則直接輸出該值(因爲這兩個聯立計數會大於m)

3、如果該值的計數與該值前面元素的父節點計數相加小於等於m,則該值可以與前面元素聯立

以下是AC代碼:


#include <cstdio>   
#include <iostream> 
#include <algorithm> 
#include <cmath> 
#include <cstdlib> 
#include <cstring> 
#include <vector> 
#include <list> 
#include <map> 
#include <stack> 
#include <queue> 
using namespace std; 
#define ll long long
int f[100005];
int sum[100005];
int n,m;
void set()
{
	for(int i = 0;i <= 260;i++)
	{
		f[i] = i;
		sum[i] = 1;
	}
}
int father(int x)
{
	if(f[x] == x)
		return x;
	else
		return f[x] = father(f[x]);
}
void Union(int x,int y)
{
	int f1 = father(x);
	int f2 = father(y);
	if(f1 != f2)
	{
		f[max(f1,f2)] = min(f1,f2);
		sum[min(f1,f2)]++;
	}
}
int main() 
{
	while(cin >> n >> m)
	{
		set();
		while(n--)
		{
			int x;
			cin >> x;
			while(x - 1 >= 0)
			{
				if(father(x) != x)
					break;
				else if(sum[father(x-1)]+sum[f[x]] > m)
					break;
				else
				{
					sum[father(x-1)]+=sum[f[x]];
					f[x] = father(x-1);
				}
				x--;
			}
			cout << f[x];
			if(n == 0)
				cout << endl;
			else
				cout << " ";
		}
	}
    //cout << "AC" <<endl; 
    return 0; 
} 

過了很久,又遇到了這題,找了很久bug,以下是AC代碼二:

#include <cstdio>  
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
int f[270];
int n;
int cent[270];
void init()
{
	for(int i = 0;i <= 256;i++)
	{
		f[i] = i;
		cent[i] = 1;
	}
}
int find(int x)
{
	if(f[x] == x)
		return x;
	return f[x] = find(f[x]);
}
void unio(int x,int y)
{
	int f1 = find(x);
	int f2 = find(y);
	if(f1 != f2)
		f[max(f1,f2)] = min(f1,f2);
}
int main()
{
	int n,k;
	while(scanf("%d%d",&n,&k) != EOF)
	{
		init();
		int a[n];
		int ans[n];
		for(int i = 0;i < n;i++)
			scanf("%d",&a[i]);
		for(int i = 0;i < n;i++)
		{
			for(int j = a[i]-1;j >= 0&& j >= a[i]-k;j--)
				if(cent[find(j)]+cent[find(a[i])] <= k && find(j) != find(a[i]))//當時沒有寫find(j) != find(a[i]),這樣可能就會出現兩個相同組再聯立一次的情況,導致cent無故加倍(找了很久很久才找到的bug)
				{
					int x1 = find(a[i]);
					int x2 = find(j);
					unio(x1,x2);
					cent[find(a[i])] = cent[x1]+cent[x2];
				}
				else
					break;
			ans[i] = find(a[i]);
		}
		for(int i = 0;i < n-1;i++)
			cout << ans[i] << ' ';
		cout << ans[n-1] <<endl;
	}
	//cout << "AC" <<endl;
	return 0;
}



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