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

帮忙看下程序还能怎样优化啊。。。用了2125MS

Posted by yzhw at 2009-03-05 14:34:21 on Problem 1054
In Reply To:终于AC了,说下体会吧 Posted by:whuwinnie at 2008-08-05 09:31:40
我用双重表索引,还是2s向上。。。。汗
Source Code

Problem: 1054  User: yzhw 
Memory: 24872K  Time: 2125MS 
Language: GCC  Result: Accepted 

Source Code 
# include <stdio.h>
# include <stdlib.h>
int row,col,num,maxnum=3;
char map[5001][5001],change=0;
struct point
{
	int r;
	int c;
}data[5001];
int cmp(const void *a,const void *b)
{
	struct point *aa=(struct point *)a;
	struct point *bb=(struct point *)b;
	if(aa->r!=bb->r) return aa->r-bb->r;
	else return aa->c-bb->c;
}
void deal(int id,struct point n)
{
	int i;
	if(n.r==data[id].r)
	{
		if(n.c>data[id].c)
		{
			int s=n.c-data[id].c;
			if(data[id].c-s>0||data[id].c+((col-data[id].c)/s+1)*s<=col) return;
			else
			{
				for(i=data[id].c;i<=col;i+=s)
					if(!map[data[id].r][i]) return;
				if((col-data[id].c)/s+1>=maxnum)
				{
					maxnum=(col-data[id].c)/s+1;
					change=1;
				}
			}
		}
		else
		{
			int s=data[id].c-n.c;
			if(data[id].c+s<=col||data[id].c-((data[id].c-1)/s+1)*s>0) return;
			else
			{
				for(i=data[id].c;i>=1;i-=s)
					if(!map[data[id].r][i]) return;
				if((data[id].c-1)/s+1>=maxnum)
				{
					maxnum=(data[id].c-1)/s+1;
					change=1;
				}
			}
		}
	}
	else if(n.c==data[id].c)
	{
		int s=n.r-data[id].r;
		if(data[id].r-s>0||data[id].r+s*((row-data[id].r)/s+1)<=row) return;
		for(i=data[id].r;i<=row;i+=s)
			if(!map[i][data[id].c]) return;
		if((row-data[id].r)/s+1>=maxnum)
		{
			maxnum=(row-data[id].r)/s+1;
			change=1;
		}
	}
	else if(n.c<data[id].c) /*k>0*/
	{
		int rs=n.r-data[id].r,cs=data[id].c-n.c;
		int tr=data[id].r,tc=data[id].c;
		int valid=((row-data[id].r)/rs)<((data[id].c-1)/cs)?((row-data[id].r)/rs):((data[id].c-1)/cs);
		if((data[id].r-rs>0&&data[id].c+cs<=col)||(data[id].r+rs*(valid+1)<=row&&data[id].c-cs*(valid+1)>0)) return;
		for(i=1;i<=valid;i++)
		{
			tr+=rs;
			tc-=cs;
			if(!map[tr][tc]) return;
		}
		if(valid+1>=maxnum)
		{
			maxnum=valid+1;
			change=1;
		}
	}
	else
	{
		int rs=n.r-data[id].r,cs=n.c-data[id].c;
		int tr=data[id].r,tc=data[id].c;
		int valid=((row-data[id].r)/rs)<((col-data[id].c)/cs)?((row-data[id].r)/rs):((col-data[id].c)/cs);
		if((data[id].r-rs>0&&data[id].c-cs>0)||(data[id].r+rs*(valid+1)<=row&&data[id].c+cs*(valid+1)<=col)) return;
		for(i=1;i<=valid;i++)
		{
			tr+=rs;
			tc+=cs;
			if(!map[tr][tc]) return;
		}
		if(valid+1>=maxnum)
		{
			maxnum=valid+1;
			change=1;
		}
	}
}
struct point* find(int id,int *count)
{
	int rl,ll,i,j;
	struct point *res=(struct point*)calloc(5001,sizeof(struct point));
	/*---------------k>0------------------*/
	rl=(row-data[id].r)/(maxnum-1)+data[id].r;
	ll=data[id].c-(data[id].c-1)/(maxnum-1);
	if(rl>row) rl=row;
	if(ll<1) ll=1;
	for(i=data[id].r;i<=rl;i++)
		for(j=ll;j<=data[id].c;j++)
			if(i!=data[id].r||j!=data[id].c)
				if(map[i][j]) 
				{
					res[++(*count)].r=i;
					res[*count].c=j;
				}
	/*----------------k<0--------------*/
	ll=(col-data[id].c)/(maxnum-1)+data[id].c;
	if(ll>col) ll=col;
	for(i=data[id].r;i<=rl;i++)
		for(j=data[id].c;j<=ll;j++)
			if(i!=data[id].r||j!=data[id].c)
				if(map[i][j]) 
				{
					res[++(*count)].r=i;
					res[*count].c=j;
				}
	/*----------------------------------*/
	return res;
}
	

int main()
{
	int i,j;
	memset(map,0,sizeof(map));
	scanf("%d %d %d",&row,&col,&num);
	for(i=1;i<=num;i++)
	{
		scanf("%d %d",&data[i].r,&data[i].c);
		map[data[i].r][data[i].c]=1;
	}
	qsort(data+1,num,sizeof(struct point),cmp);
	for(i=1;i<=num;i++)
	{
		int c=0;
		struct point *list=find(i,&c);
		if(c==0) 
		{
			free(list);
			continue;
		}
		else
		{
			for(j=1;j<=c;j++) 
				deal(i,list[j]);
			free(list);
		}

	}
	if(change) printf("%d\n",maxnum);
	else printf("0\n");
	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