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

Posted by zhb_msqx at 2007-09-22 22:55:30 on Problem 3378
In Reply To:为什么会Output Limit Exceeded? Posted by:200530010505 at 2007-09-21 20:11:10
#include<stdio.h>
#define MAX 50000
struct Record
{
	long int value;
	long int position;
};


long int findOut(long int *p,long int n,struct Record *q)
{
	long int i,j,m,sum=0;       
	q[0].value=p[0];
	q[0].position=0;
	i=1;
	j=1;
	while(1)
	{
		for(;i<n;i++)          //寻找
	        if(q[j-1].value<p[i])
			{
			    q[j].value=p[i];
			    q[j].position=i;
		        j++;
			}
	    if(j>=5)    //计算
		    sum+=j*(j-1)*(j-2)*(j-3)*(j-4)/120;
		while(j>0)    //退位
		{
			j--;   
		    for(m=q[j].position+1;m<n&&(p[m]<q[j-1].value||p[m]>q[j].value);m++);
			if(m<n)
			{
				q[j].value=p[m];
			    q[j].position=m;
				i=m+1;
				j++;
				break;
			}
		}
     	if(j==0)
		{
			for(m=q[0].position+1;m<n-4&&p[m]>q[j].value;m++);
			if(m<n-4)
			{
				q[0].value=p[m];
			    q[0].position=m;
				i=m+1;
				j++;
			}
			else break;
		}
	}
	return(sum);
}



void main()
{
	long int i,N,sum;
	long int inPutNum[MAX];
	struct Record record[MAX];
	
	while(scanf("%ld",&N)!=EOF)
	{
		for(i=0;i<N;i++)
			scanf("%ld",&inPutNum[i]);
		sum=findOut(inPutNum,N,record);
		printf("%ld\n",sum);
	}		
}

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