//============================================================================
// 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;
}
poj1009
最新推荐文章于 2021-10-20 20:19:04 发布