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 }
  • 相关阅读:
    Html2Text
    分析文件上传过程中的HTTP头部
    去除html标签
    .NET/C#中的索引器
    MSB与LSB
    大流量网站的底层系统架构
    经典SQL语句,可以让行的数据当列来显示
    在页面弹出漂亮的提示框右下角弹出,方正的框
    ASP.NET读取XML某节点返回DataTable实例
    读取EXECL文件内容,可以支持分布
  • 原文地址:https://www.cnblogs.com/Bincoder/p/5071957.html
Copyright © 2011-2022 走看看