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;
}


發佈了161 篇原創文章 · 獲贊 12 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章