poj1009

//============================================================================
// Name        : 1009.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
#include <map>
#include <algorithm>
#define MAX_LINE  1000
using namespace std;

typedef struct node {
	int start;
	int end;
	int pixel;
} Node;

int countct;
int width;
int pixelNumbers;
int rows;
Node input[MAX_LINE];
map<int, int> result;

int getPixel(int pos, int s, int e) {
	if (s == e - 1) {
		return input[s].pixel;
	}
	int mid = (s + e) / 2;
	Node& node = input[mid];
	if (node.start > pos) {
		return getPixel(pos, s, mid);
	} else if (node.end <= pos) {
		return getPixel(pos, mid, e);
	} else {
		return input[mid].pixel;
	}
}

int getPixel(int pos) {
	return getPixel(pos, 0, countct);
}
void outPut() {
	/*
	puts("input");
	for (int i = 0; i < countct; i++) {
		Node& node = input[i];
		printf("[%d,%d)\t%d\n", node.start, node.end, node.pixel);
	}

	 puts("pixel");
	 for (int i=0;i< pixelNumbers;i++){
	 printf("%d\t%d\n", i, getPixel(i));
	 }
	 puts("out");

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < width; j++) {
			if (j % 10 == 9)
				printf("%d|", getPixel(i * width + j));
			else
				printf("%d ", getPixel(i * width + j));
		}
		puts("");
	}
	*/
	int start = 0;
	int pixel = result.begin()->second;

	for (map<int, int>::iterator it = result.begin(); it != result.end();
			++it) {
		int index = it->first;
		int value = it->second;

		if (index < pixelNumbers) {
//			printf("value=%d index=%d\n", value, index);
			if (value != pixel) {
				printf("%d %d\n", pixel, index - start);
				pixel = value;
				start = index;
			}
		} else {
			printf("%d %d\n", pixel, pixelNumbers - start);
			break;
		}
	}
	puts("0 0");
}

void calculateMap() {
	for (map<int, int>::iterator it = result.begin(); it != result.end();
			++it) {
		int index = it->first;
		if (index < pixelNumbers) {
			int pixel = getPixel(index);
			int maxout = 0;
			int row = index / width;
			int clm = index % width;

			for (int i = row - 1; i <= row + 1; i++) {
				if (i < 0 || i == rows)
					continue;
				for (int j = clm - 1; j <= clm + 1; j++) {
					if (j < 0 || j == width)
						continue;
					int nd = i * width + j;
					int px = getPixel(nd);
					maxout = max(maxout, abs(px - pixel));
				}
			}

			it->second = maxout;
		}
	}
}

void addToResultMap(const int current) {
	int row = current / width;
	int clm = current % width;

	for (int i = row - 1; i <= row + 1; i++) {
		if (i < 0)
			continue;
		for (int j = clm - 1; j <= clm + 1; j++) {
			if (j < 0 || j == width)
				continue;
			int index = i * width + j;
//			printf("add%d\n", index);
			result[index] = 0;
		}
	}
//	puts("");
}

int main() {
	while (1) {
		scanf("%d", &width);
		if (width == 0) {
			break;
		}
		printf("%d\n", width);
		int current = 0;
		countct = 0;
		result.clear();
		while (1) {
			int pixel, number;
			scanf("%d %d", &pixel, &number);
			if (pixel == 0 && number == 0) {
				break;
			}
			Node& node = input[countct];
			node.start = current;
			node.pixel = pixel;
			addToResultMap(current);
			current += number;
			addToResultMap(current - 1);
			node.end = current;
			int er = node.end / width;
			int sr = node.start / width;
			if (er > sr) {
				addToResultMap((sr + 1) * width);
			}
			countct++;
		}
		pixelNumbers = current;
		rows = pixelNumbers / width;
		calculateMap();
		outPut();
	}
	puts("0");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值