poj 2081 Recaman's Sequence

题目链接:http://poj.org/problem?id=2081

解题思路:简单dp

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: poj 2081
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 typedef long long LL;
26 const double PI=acos(-1.0);
27 ///////////////////////////////////////////////////////////////////////////
28 
29 ///////////////////////////////////////////////////////////////////////////
30 //Add Code:
31 const int maxn=500000;
32 int a[maxn+5];
33 bool flag[maxn*10+5];
34 ///////////////////////////////////////////////////////////////////////////
35 
36 int main(){
37     ///////////////////////////////////////////////////////////////////////
38     //Add code:
39     int i,k;
40     memset(flag,0,sizeof(flag));
41     a[0]=0,flag[0]=1;
42     for(i=1;i<=maxn;i++){
43         if(a[i-1]-i>0 && !flag[a[i-1]-i]){
44             a[i]=a[i-1]-i;
45             flag[a[i]]=1;
46         }
47         else{
48             a[i]=a[i-1]+i;
49             flag[a[i]]=1;
50         }
51     }
52     while(scanf("%d",&k)!=EOF){
53         if(k==-1) break;
54         printf("%d\n",a[k]);
55     }
56     ///////////////////////////////////////////////////////////////////////
57     return 0;
58 }
59 
60 ///////////////////////////////////////////////////////////////////////////
61 /*
62 Testcase:
63 Input:
64 7
65 10000
66 -1
67 Output:
68 20
69 18658
70 */
71 ///////////////////////////////////////////////////////////////////////////

posted on 2013-08-19 10:39  SCNU20102200088  阅读(114)  评论(0编辑  收藏  举报

导航