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

奉献一个质量烂的代码 没仔细优化过

Posted by Scopi at 2010-07-08 14:07:57 on Problem 1009
#include<stdio.h>
#include<cstring>
int line[3][1029][2]={0};
int linep;
int sum,row,valuetemp,numtemp;
int i,j;
bool first=0,last=0,r=0;
int p[12]={0};
struct link
{
	int value;
	int num;
	link*next;
};
struct linkhead
{
	int row;
	link*linklink;
	linkhead*next;
};
int min(int,int,int);
int abs(int);
int cal(int);
int quick(int);
int main()
{
	int fullline=0;
	int value,num;
	linkhead*head=new linkhead;
	linkhead*headp=head;
	linkhead*headq=NULL;
	head->linklink=NULL;
	head->next=NULL;
	link*linkp=NULL;
	link*linkq=NULL;
	for(;;)
	{
		scanf("%d",&row);
		if(row==0)break;
		headq=new linkhead;
		headq->row=row;
		headq->next=NULL;
		headp->next=headq;
		headp=headq;
		headq=NULL;
		fullline=1;
		linep=0;
		sum=0;
		first=1;
		last=0;
		for(;;)
		{
			scanf("%d%d",&value,&num);
			if(value==0&&num==0)break;
			while(num>0)
			{	
				if(num+sum<row)
				{	
					sum+=num;
					line[fullline][linep][0]=value;
					line[fullline][linep][1]=sum;
					linep++;
					break;
				}
				if(num+sum>=row)
				{
					line[fullline][linep][0]=value;
					line[fullline][linep][1]=row;
					num-=row-sum;
					linep=0;
					sum=0;
					fullline++;
				}
				if(fullline==3)
				{
					memset( p , 0 , sizeof ( p ) );							//处理每一行
					for(i=1;i<=row;)
					{
						p[1]=p[2];
						p[2]=p[3];
						if( line[0][p[2]][1]>=i+1 || i==row )
							p[3]=p[2];
						else p[3]=p[2]+1;
						p[4]=p[5];
						p[5]=p[6];
						if( line[1][p[5]][1]>=i+1 || i==row )
							p[6]=p[5];
						else p[6]=p[5]+1;
						p[7]=p[8];
						p[8]=p[9];
						if( line[2][p[8]][1]>=i+1 || i==row )
							p[9]=p[8];
						else p[9]=p[8]+1;
						valuetemp=cal(i);
						numtemp=quick(i);			///横向快速优化
						i+=numtemp;
						if(linkp==NULL)
						{
							linkp=new link;
							linkp->value=valuetemp;
							linkp->num=numtemp;
							linkp->next=NULL;
							headp->linklink=linkp;
						}
						else
						{
							if(valuetemp==linkp->value)linkp->num+=numtemp;
							else
							{
								linkq=new link;
								linkq->value=valuetemp;
								linkq->num=numtemp;
								linkq->next=NULL;
								linkp->next=linkq;
								linkp=linkq;
								linkq=NULL;
							}
						}
					}
					first=0;
					for(i=0;line[1][i-1][1]!=row;i++)
					{
						line[0][i][0]=line[1][i][0];
						line[0][i][1]=line[1][i][1];
					}
					for(i=0;line[2][i-1][1]!=row;i++)
					{
						line[1][i][0]=line[2][i][0];
						line[1][i][1]=line[2][i][1];
					}
					fullline=2;
				}
				if(r)
				{
					if(linkp->value==0)linkp->num+=num-num%row-row;
					else
					{
						linkq=new link;
						linkq->value=0;
						linkq->num=num-num%row-row;
						linkq->next=NULL;
						linkp->next=linkq;
						linkp=linkq;
						linkq=NULL;
					}
					num=num%row+row;
					r=0;
				}
				if(num>=3*row&&row<350000000)r=1;
			}
		}
		last=1;
		memset( p , 0 , sizeof ( p ) );
		for(i=1;i<=row;)
		{
			p[1]=p[2];
			p[2]=p[3];
			if( line[0][p[2]][1]>=i+1 || i==row )
				p[3]=p[2];
			else p[3]=p[2]+1;
			p[4]=p[5];
			p[5]=p[6];
			if( line[1][p[5]][1]>=i+1 || i==row )
				p[6]=p[5];
			else p[6]=p[5]+1;
			p[7]=p[8];
			p[8]=p[9];
			if( line[2][p[8]][1]>=i+1 || i==row )
				p[9]=p[8];
			else p[9]=p[8]+1;
			valuetemp=cal(i);
			numtemp=quick(i);			///横向快速优化
			i+=numtemp;
			if(linkp==NULL)
			{
				linkp=new link;
				linkp->value=valuetemp;
				linkp->num=numtemp;
				linkp->next=NULL;
				headp->linklink=linkp;
			}
			else
			{
				if(valuetemp==linkp->value)linkp->num+=numtemp;
				else
				{
					linkq=new link;
					linkq->value=valuetemp;
					linkq->num=numtemp;
					linkq->next=NULL;
					linkp->next=linkq;
					linkp=linkq;
					linkq=NULL;
				}
			}
		}
		last=0;
		linkp=NULL;
	}
	for(headp=head->next;headp!=NULL;headp=headp->next)
	{
		printf("%d\n",headp->row);
		linkp=headp->linklink;
		for(;linkp!=NULL;linkp=linkp->next)
			printf("%d %d\n",linkp->value,linkp->num);
		printf("0 0\n");
	}
	printf("0\n");
	return 0;
}
int abs( int a )
{
	if( a<0 )return -a;
	else return a;
}
int cal(int a)
{
	int temp[8];
	int max;
	temp[0]=abs(line[0][p[1]][0]-line[1][p[5]][0]);
	temp[1]=abs(line[0][p[2]][0]-line[1][p[5]][0]);
	temp[2]=abs(line[0][p[3]][0]-line[1][p[5]][0]);
	temp[3]=abs(line[1][p[4]][0]-line[1][p[5]][0]);
	temp[4]=abs(line[1][p[6]][0]-line[1][p[5]][0]);
	temp[5]=abs(line[2][p[7]][0]-line[1][p[5]][0]);
	temp[6]=abs(line[2][p[8]][0]-line[1][p[5]][0]);
	temp[7]=abs(line[2][p[9]][0]-line[1][p[5]][0]);
	if(first){temp[0]=temp[1]=temp[2]=0;}
	if(last){temp[5]=temp[6]=temp[7]=0;}
	for(int i=1;i<8;i++)
		if(temp[i]>temp[0])
			temp[0]=temp[i];
	return temp[0];
}
int quick(int i)
{
	int a,c;
	a=line[0][p[1]][1]-i;
	c=line[2][p[7]][1]-i;
	if(first)a=2000000000;
	if(last)c=2000000000;
	int temp=min(a,line[1][p[4]][1]-i,c);
	if(temp<2)return 1;
	else return temp; 
}
int min(int a , int b , int c)
{
	a=a<b?a:b;
	a=a<c?a:c;
	return a;
}

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