POJ 2081 Recaman's Sequence -- from lanshui_Yang
2012-08-10 17:17
309 查看
DescriptionThe Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence, otherwise am = am−1 + m.The first few numbers in the Recaman's Sequence is 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 ...Given k, your task is to calculate ak.InputThe input consists of several test cases. Each line of the input contains an integer k where 0 <= k <= 500000.The last line contains an integer −1, which should not be processed.OutputFor each k given in the input, print one line containing ak to the output.Sample Input
7 10000 -1Sample Output
20 18658
题目大意 很简单 这里 就不多说 啦~
但是要 注意 的是,此题采用一般的在线方法 ,很容易TLE,所以 要采用离线做法,即先把数组a[1000000]初始化。
具体讲解见代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <map>using namespace std;int a[1000000];int main(){map<int , int> mymap; //定义map,意在 判断 数据 是否出现过,具体运用如下:mymap.clear();a[0]=0;a[1]=1;a[2]=3;mymap[a[0]] =1; // 把出现过的值统统 标记为 1mymap[a[1]] =1;mymap[a[2]] =1;int i;for(i=3; i<=500006; i++){a[i]=a[i-1]-i;if(mymap.find(a[i])!=mymap.end()||a[i]<=0){a[i]=a[i-1]+i;}mymap[a[i]] = 1;}int k;while (scanf("%d",&k)!=EOF){if(k==-1)break;printf("%d\n",a[k]); // 初始化数组a[]后,直接输出a[k]}}[/code]
相关文章推荐
- POJ 2081 Recaman's Sequence
- POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
- POJ 1828 Monkeys' Pride (易 TLE 题目) -- from lanshui_Yang
- POJ2081 2081 Recaman's Sequence
- POJ 1904 King's Quest - from lanshui_Yang
- 【poj2081】Recaman's Sequence (动态规划DP)
- POJ 2081 Recaman's Sequence 解题报告
- poj 2081 Recaman's Sequence【hash】
- POJ2081 Recaman's Sequence
- Poj 2081 Recaman's Sequence之解题报告
- POJ-2081:Recaman's Sequence
- POJ-2081-Recaman's Sequence-Hash思想解题
- POJ 2411 Mondriaan's Dream(状压 dp)
- POJ 2488 A Knight's Journey
- poj 1659 Frogs' Neighborhood(判断序列可图)
- POJ 1300 Door Man - from lanshui_Yang
- POJ 2262 ----Goldbach's Conjecture
- POJ 2965 (超简单代码47毫秒AC) The Pilots Brothers' refrigerator
- POJ 2528 Mayor's posters(离散化 线段树 贴海报)
- poj 2081 Recaman's Sequence