poj 2081Recaman's Sequence

/*
  Name: poj 2081 Recaman's Sequence
  Author: Unimen
  Date: 07-05-11 03:12
  Description: 动规水题
*/

/*
解题报告:动规水题
唯一的注意点为bApper数组的应用:通过开辅助数组记前面已经出现的数,把时间复杂度降到常数 
*/
#include <iostream>
#include <cstring>
using namespace std;

int anResult[500009];
bool bApper[6266900];

void dp()
{
	anResult[0] = 0;
	bApper[0] = true;
	int i,j;
	for(i=1; i<=500000; ++i)
	{
		if(anResult[i-1]-i >0 && !bApper[anResult[i-1]-i])
		{
			anResult[i] = anResult[i-1] - i;
		}
		else
		{
			anResult[i] = anResult[i-1] + i;
		}
		bApper[anResult[i]] = true;
	}
		
}

int main()
{
	int k;
	dp();
	while(cin>>k && k!=-1)
	{
		cout<<anResult[k]<<endl;
	}
	return 0;
}


你可能感兴趣的:(poj 2081Recaman's Sequence)