POJ 1009题解

题意就是给定一个图的RLE表示。然后求这个图的每个像素点的绝对值。

每个像素点的绝对值由其周围的8个点的最大绝对值来决定。

从给的两个图应该可以看出来了。不多说了。


限制:

数量级很大,时间要求很短。

要求需要处理好内存的关系与时间的关系。

首先你是不可以把整个图按其真实情况存放起来的。

那么需要按照如下方式处理:

由于相同的点是连续的,那么在处理时,这些连续的点中间的值的绝对值肯定都是由两端变化的点所决定的。

所以只需要处理给定的连续的两端则可。

除了图最后一点以外,处理连续线段的后面一个端点是没有意义的。所以只需要处理连续点的头一个点,外加只需要处理图片的最后一行的开头与最后一行的结尾。

(注意处理最后一行的开头与最后一行的结尾)。

然后把处理的结果排一下序就可以了。

注意边界条件。我写了好久

#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<hash_map>
using namespace std;
using namespace stdext;
class node
{
public:
	int pos;
	int val;
public:
	node(int a, int b):val(a), pos(b){}
	node(){}
	//	bool operator < (const node &a){return pos < a.pos;}
	friend bool operator < (const node &a, const node &b) { return a.pos < b.pos;}
};

int max_rows, max_cols, max_pixs;
hash_map<int, int> rec;

const int dk[][2] = {{-1,-1}, {-1, 0}, {-1, 1},
{ 0,-1},           {0, 1},
{1, -1}, {1, 0},   {1, 1}};

int _get_value_by_bs(int pos, vector<node> &vs)
{
	int b = 0, e = vs.size(), m;
	while (b < e)
	{
		m = b + ((e - b) >> 1);
		if (vs[m].pos == pos) return vs[m].val;
		if (vs[m].pos > pos) e = m;
		else
		{
			if (m + 1 < vs.size() && vs[m+1].pos > pos) return vs[m].val;
			else b = m + 1;
		}
	}
	return vs[m].val;
}

int _get_value_by_seq(int pos, vector<node> &vs)
{
	int i = 0;
	while (i < vs.size() && vs[i].pos <= pos) ++i;
	if (0 == i) return vs[0].val;
	return vs[i-1].val;
}

inline int _get_value_by_hash(int pos)
{
	hash_map<int, int>::iterator iter = rec.find(pos);
	if (rec.end() == iter) return -1;
	return iter->second;
}

int _get_value(int pos, vector<node> &vs)
{
	int ret = _get_value_by_hash(pos);
	if (-1 == ret)
	{
		ret = _get_value_by_bs(pos, vs);
		rec[pos] = ret;
	}
	return ret;
}

int _get_code(int pos, vector<node> &vs)
{
	const int center_row = pos / max_cols;
	const int center_col = pos % max_cols;
	const int center_value = _get_value(pos, vs);

	int max_abs = 0;
	for (int i = 0; i < 8; ++i)
	{
		const int new_row = center_row + dk[i][0];
		const int new_col = center_col + dk[i][1];
		const int new_pos = new_row * max_cols + new_col;
		if (0 <= new_row && new_row < max_rows &&
			0 <= new_col && new_col < max_cols && 
			new_pos < max_pixs)
		{
			const int new_value = _get_value(new_pos, vs);
			const int ret = abs(new_value - center_value);
			max_abs = max_abs > ret ? max_abs : ret;
		}
	}
	return max_abs;
}

void _one_effect(int pos, vector<node> &vs, vector<node> &out)
{
	const int center_pos = pos;
	const int center_row = pos / max_cols;
	const int center_col = pos % max_cols;
	const int center_value = _get_code(center_pos, vs);
	out.push_back(node(center_value, center_pos));

	for (int i = 0; i < 8; ++i)
	{
		const int new_row = center_row + dk[i][0];
		const int new_col = center_col + dk[i][1];
		const int now_pos = new_row * max_cols + new_col;
		if (0 <= new_row && new_row < max_rows &&
			0 <= new_col && new_col < max_cols &&
			now_pos < max_pixs)
		{
			const int now_value = _get_code(now_pos, vs);
			out.push_back(node(now_value, now_pos));
		}
	}
}

int main(void)
{
	int COLS;
	while (scanf("%d", &COLS) != EOF && COLS)
	{
		int value, number, pos = 0; rec.clear();
		vector<node> vs; vector<node> out;
		while (scanf("%d%d", &value, &number) != EOF && (value || number))
		{
			if (!number)continue;
			vs.push_back(node(value, pos));
			pos += number;
		}
		max_rows = pos / COLS; max_cols = COLS; max_pixs = pos;

		for (vector<node>::iterator iter = vs.begin(); iter != vs.end(); ++iter)
		{
			_one_effect(iter->pos, vs, out);
		}
		if (max_pixs - max_cols) _one_effect(max_pixs - max_cols, vs, out);
		_one_effect(max_pixs - 1, vs, out);

		sort(out.begin(), out.end());

		cout << max_cols << endl;
		vector<node>::iterator temp = out.begin();
		for (vector<node>::iterator next = temp + 1; next != out.end(); ++next)
		{
			if (next->val != temp->val)
			{
				cout << temp->val << ' ' << next->pos - temp->pos << endl;
				temp = next;
			}
		}
		if( max_pixs - temp->pos) cout << temp->val << ' ' << max_pixs - temp->pos<< endl;
		cout << "0 0" << endl;
	}
	cout << "0" << endl;
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值