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 mjh at 2004-11-22 22:37:21 on Problem 2061
#include <iostream>
using namespace std;
int num[2][2000];
int nnum[2];
int B,nd,dig[101],T;
int backward(int k)
{
	int dt=k%2;
	int st=(dt+1)%2;
	int pos=0;
	int i;
	num[dt][0]=dig[nd-1-k];
	for (i=0;i<nnum[st];++i)
	{
		if (num[dt][pos]>num[st][i])
		{
			num[dt][pos+1]=num[st][i]+B-num[dt][pos];
			if (i!=nnum[st]-1)
			{
				if (num[st][i+1]!=1) return -1;
			}
			++i;
			++pos;
		}
		else
		{
			num[dt][pos+1]=num[st][i]-num[dt][pos];
			++pos;
		}
	}
	nnum[dt]=pos+1;
	return 1;
}
void forward(int i,int T)
{
	int dt,st;
	int k;
	st=i%2;
	dt=(st+1)%2;
	int pos;
	int t;
	for (k=2;k<=T;++k)
	{
		pos=0;
		int j;
		for (j=0;j<nnum[st]-1;++j)
		{
			t=num[st][j]+num[st][j+1];
			if (t>=B)
			{
				num[dt][pos]=t%B;
				num[dt][pos+1]=1;
				pos+=2;
			}
			else
			{
				num[dt][pos]=t;
				++pos;
			}
		}
		nnum[dt]=pos;
		if (nnum[dt]==1&&k!=T) 
		{
			cout<<"unpredictable"<<endl;
			return;
		}
		t=st;
		st=dt;
		dt=t;
	}
	cout<<num[st][0]<<endl;
	return;
}
int main()
{
	int N;
	cin>>N;
	while (N--)
	{
		cin>>B>>nd;
		int i;
		nnum[0]=nnum[1]=0;
		for (i=0;i<nd;++i)
		{
			cin>>dig[i];
		}
		cin>>T;
		num[0][0]=dig[nd-1];
		nnum[0]=1;
		int stop=0;
		for (i=1;i<nd&&!stop;++i)
		{
			if (backward(i)==-1) 
			{
				stop=1;
				cout<<"impossible"<<endl;
			}
		}
		if (nd==1&&T>1) cout<<"unpredictable"<<endl;
		else
		{
			if (stop!=1)
			{
				--i;
				forward(i,T);
			}
		}
	}
}

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