poj 2081Recaman's Sequence

来源:互联网 发布:无主之地2年度版淘宝 编辑:程序博客网 时间:2024/05/16 04:48
/*  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;}


原创粉丝点击