Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

TLE的普通算法,写这个都够费力的,先写出来理清思路,再考虑speed up

Posted by iscxy at 2005-02-14 00:41:41 on Problem 1009
#include<stdio.h>
#include<iostream.h>
#define N 32768
int value[2][N];
int edge[2][N];
int le,lc; //准备输出的边缘值RLE pair。
int i;

inline swap(int &a,int &b){
	int c=a;
	a=b;
	b=c;
}
inline update(int i1,int j1,int i2,int j2){//更新相领的两个象素边缘强度值
	int e=value[i1][j1]-value[i2][j2];
	if (e<0) e=-e;
	if (e>edge[i1][j1]) edge[i1][j1]=e;
	if (e>edge[i2][j2]) edge[i2][j2]=e;
}
inline output(int w,int r){
	for (i=0;i<w;i++){
		while(i<w && edge[r][i]==le) {i++;lc++;}
		if (i<w){ //不是行尾剩下的就输出
			if (lc>0)
				cout<<le<<" "<<lc<<endl;
			le = edge[r][i];
			lc = 1;
		}
	}
}

inline ACM(int w){
	int v;	//当然读入的颜色
	int l;	//当然读入的游长
	int row,col;//当前行号列号(从0开始)
	int r0,r1;//当前行pairs数量
	int srow;
	
	cin>>v>>l;
	row=0;col=0;
	r0=1;//上一行
	r1=0;//当前一行,这两行替换使用
	le=-1;
	lc=0;
	while(l!=0){	//read a pair RLE
		srow=0;
		while (l>0){
			int n=l;
			if (col+n>w) n= w-col;
			if (l>w && srow>3){	//特殊加速:超长RLE pair,不要这段也可运行,时间超长
				if (le!=0){
					cout<<le<<" "<<lc<<endl;
					le=0;lc=0;
				}
				col=l%w;	//余下n个不处理,只处理整行0
				if (col==0) 
					lc+=l-w;
				else
					lc+=l-w-col;
				l=col+w;
				for (i=0;i<w;i++) edge[r1][i]=edge[r0][i]=0;
				continue;
				//置最上一行
				//有l/w个edge值全0行
			}

			if (row==0)	//第0行
				for (i=col;i<col+n;i++){
					value[r1][i]=v;
					edge[r1][i]=0;
					if (i>0) update(r1,i,r1,i-1);
				}
			else{			//非第0行
				for (i=col;i<col+n;i++){
					value[r1][i]=v;
					edge[r1][i]=0;
					update(r1,i,r0,i);
					if (i>0) {update(r1,i,r0,i-1);update(r1,i,r1,i-1);}
					if (i<w-1) update(r1,i,r0,i+1);
				}
			}
			if (i==w){ //满一行
				col =0;	//换行到下一行开头
				row++;
				srow++;
				l -= n;
				if (row>1)	output(w,r0);
				swap(r0,r1);
			}
			else{
				col+=l;	//前进l长度
				l = 0;
			}
		}
		cin>>v>>l;
	}
	output(w,r0);
	cout<<le<<" "<<lc<<endl;
}

//数据结构:定义两行,每行足够长
//算法:循环读入和填入,并计算和输出edge值
int main()
{
	int w;	//宽度
	cin>>w;//图像宽度
	while(w!=0){
		cout<<w<<endl;
		ACM(w);
		cout<<"0 0"<<endl;
		cin>>w;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator