您的位置:首页 > 其它

poj 1009 Edge Detection 模拟

2015-10-23 13:11 351 查看
//poj 1009
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
const int maxN=1024;

struct RLE
{
int v,pos;
}rle_in[maxN],rle_out[maxN*8];

int n,m,p,q,sum;

int cmp(RLE x,RLE y)
{
return x.pos<y.pos;
}

int get_pos(int i,int j)
{
int tpos=i*m+j;
int ans,l=0,r=p;
while(l<r){
int mid=(l+r)/2;
if(rle_in[mid].pos<=tpos){
ans=mid;
l=mid+1;
}else
r=mid;
}
return ans;
}

void update(int row,int col)
{
if(row<0||row>=n||col<0||col>=m)
return ;
int maxx=0;
int center=rle_in[get_pos(row,col)].v;
for(int i=row-1;i<=row+1;++i)
for(int j=col-1;j<=col+1;++j)
if(i>=0&&i<n&&j>=0&&j<m&&!(i==row&&j==col))
maxx=max(maxx,abs(center-rle_in[get_pos(i,j)].v));
rle_out[q].v=maxx;
rle_out[q++].pos=row*m+col;

}

int main()
{
while(scanf("%d",&m)==1&&m){
int a,b;
sum=p=0;
while(scanf("%d%d",&a,&b)==2){
if(a==0&&b==0)
break;
rle_in[p].v=a;
rle_in[p++].pos=sum;
sum+=b;
}
n=sum/m;
q=0;
rle_in[p].pos=sum;
for(int k=0;k<=p;++k){
int row=rle_in[k].pos/m;
int col=rle_in[k].pos%m;
for(int i=row-1;i<=row+1;++i)
for(int j=col-1;j<=col+1;++j)
update(i,j);
}
sort(rle_out,rle_out+q,cmp);
int pre=0;
RLE tmp=rle_out[0];
printf("%d\n",m);
for(int i=1;i<q;++i)
if(rle_out[i].v!=tmp.v){
printf("%d %d\n",tmp.v,rle_out[i].pos-tmp.pos);
tmp=rle_out[i];
}
printf("%d %d\n",tmp.v,sum-tmp.pos);
puts("0 0");
}
puts("0");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 算法