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; }
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析
- C#获取关键字附近文字算法实例