zoukankan      html  css  js  c++  java
  • Poj 2081 Recaman's Sequence之解题报告

                                                                                                          Recaman's Sequence
    Time Limit: 3000MS   Memory Limit: 60000K
    Total Submissions: 22363   Accepted: 9605

    Description

    The 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.

    Input

    The 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.

    Output

    For each k given in the input, print one line containing ak to the output.

    Sample Input

    7
    10000
    -1

    Sample Output

    28

    18658

     

    对于这题的正确做法就是模拟加暴力Dp;

    在DP的时候一定记得考虑时间复杂度的问题;

    代码:

     

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 
     6 using namespace std;
     7 
     8 const int maxn = 500000+10;
     9 int a[maxn];
    10 bool used[3012500];
    11 
    12 int  main(void)
    13 {
    14      int k,i,j;
    15      
    16      a[0] = 0;
    17      memset(used,0,sizeof(used));
    18      used[0] = 1;
    19      for(i=1;i<=500000;++i)
    20      {
    21         if(a[i-1]-i>0&&!used[a[i-1]-i])  a[i] = a[i-1]-i;
    22         else    a[i] = a[i-1]+i;
    23         used[a[i]] = 1;
    24      }
    25      while(scanf("%d",&k),k!=-1)
    26      {
    27          
    28          cout<<a[k]<<endl;
    29      }
    30      
    31      return 0;
    32 }
  • 相关阅读:
    双管齐下采用压缩传输加快网页显示速度
    努力奋斗第一天
    cefSharp在XP下使得程序崩溃记录
    SVN记住用户名和密码后如何修改
    如果把编程语言比作武器
    cefSharp 设置运行时系统语言
    C# 检测机器是否有声卡设备
    C# 中判断程序是否启动使用Mutex使用异常
    chm 字体修改
    最近两年的生活
  • 原文地址:https://www.cnblogs.com/Bincoder/p/5071957.html
Copyright © 2011-2022 走看看