题目链接 : UVA1526
题目大意就是给出像素信息,每个像素变化为与周围像素的绝对值的最大值,问变化后的信息是怎样的。
如果单纯的去枚举R*C个像素点, 编程非常容易,但是由于点太多容易超时。
发现题目中的像素点是以像素条的形式给出的, 这给我们处理数据提供了一些思路。
可以发现真正发现变化的像素点就是那些处于像素条边缘以及这些点周围的点, 而处于像素条中间的点的值就是由那些边界的点决定的, 所以我们可以枚举像素条的临界点, 由于像素条的个数 < 1000, 所以枚举的点的个数不会超过1k*9个。 然后保存信息是以那些边界点保存的, 但是在枚举的过程中枚举边界点周围的8个点然后要暴力枚举么? 显然我们可以用二分去解决, 其实代码很简单, 但是我写的时候给自己挖了好几个坑。。
//UVA
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1010;
struct node{
int x;
int pos, len;
}a[N], ans[N*10];
int n, w, sum;
bool cmp(node a, node b){
return a.len < b.len;
}
bool check(int x, int y){
if(x < 0) return 0;
if(y < 0 || y >= w) return 0;
if(x*w + y >= sum) return 0;
return 1;
}
int foo(int pos){
int l = 1, r = n;
while(l < r){
int mid = (l + r) >> 1;
if(a[mid].pos > pos) r = mid;
else l = mid + 1;
}
return a[l-1].x;
}
int gao(int pos){
int ret = 0;
int x1 = foo(pos);
int r = pos / w, c = pos % w;
for(int i = r - 1 ; i <= r + 1; ++i){
for(int j = c - 1; j <= c + 1; ++j){
int tmp = i*w + j;
if(check(i, j) && tmp != pos){
int x2 = foo(tmp);
if(abs(x2 - x1) > ret)
ret = abs(x2 - x1);
}
}
}
return ret;
}
int main(){
while(~scanf("%d", &w)){
if(w == 0) break;
scanf("%d%d", &a[0].x, &a[0].len);
a[0].pos = 0;
n = 1;
while(~scanf("%d%d", &a[n].x, &a[n].len)){
if(a[n].x == 0 && a[n].len == 0) break;
a[n].pos = a[n-1].pos + a[n-1].len;
++n;
}
a[n].pos = a[n-1].pos + a[n-1].len;
sum = a[n].pos;
int t = 0;
for(int i = 0; i <= n; ++i){
int r = a[i].pos / w;
int c = a[i].pos % w;
for(int j = r - 1; j <= r + 1; ++j){
for(int k = c - 1; k <= c + 1; ++k){
int tmp = j*w + k;
if(check(j, k)){
ans[t].len = tmp;
ans[t++].x = gao(tmp);
}
}
}
}
sort(ans, ans + t, cmp);
printf("%d\n", w);
node tmp = ans[0];
for(int i = 0; i < t; ++i){
if(ans[i].x == tmp.x) continue;
printf("%d %d\n", tmp.x, ans[i].len - tmp.len);
tmp = ans[i];
}
printf("%d %d\n", tmp.x, sum - tmp.len);
printf("0 0\n");
}
printf("0\n");
return 0;
}