Recaman's Sequence

Time Limit: 3000ms
Memory Limit: 60000KB

This problem will be judged on PKU. Original ID: 2081
64-bit integer IO format: %lld      Java class name: Main

 
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

20
18658

Source

 
解题:离线搞。。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#define LL long long
#define INF 0x3f3f3f3f
#define pii pair<int,int> using namespace std;
const int maxn = ;
int dp[maxn];
bool vis[];
struct node {
int k,ans,o;
};
node inp[];
bool cmp1(const node &x,const node &y) {
return x.k < y.k;
}
bool cmp2(const node &x,const node &y) {
return x.o < y.o;
}
int main() {
int tot = ,tmp,cnt;
for(int i = ; i < maxn; ++i) {
dp[i] = dp[i-]-i;
if(dp[i] <= || vis[dp[i]])
dp[i] = dp[i-]+i;
vis[dp[i]] = true;
}
while(~scanf("%d",&tmp)&&(~tmp)) {
inp[tot].k = tmp;
inp[tot].o = tot++;
}
sort(inp,inp+tot,cmp1);
for(int i = cnt = ; i < maxn; ++i)
while(i == inp[cnt].k) inp[cnt++].ans = dp[i];
sort(inp,inp+tot,cmp2);
for(int i = ; i < tot; ++i)
printf("%d\n",inp[i].ans);
return ;
}

POJ 2081 Recaman's Sequence的更多相关文章

  1. Poj 2081 Recaman's Sequence之解题报告

                                                                                                         ...

  2. poj 2081 Recaman's Sequence (dp)

    Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 22566   Accepted: 96 ...

  3. poj 2081 Recaman&#39;s Sequence

    開始还以为暴力做不出来,须要找规律,找了半天找不出来.原来直接暴力.. 代码例如以下: #include<stdio.h> int a[1000050]; int b[100000000] ...

  4. POJ 2081 Recaman&#39;s Sequence(水的问题)

    [简要题意]:这个主题是很短的叙述性说明.挺easy. 不重复. [分析]:只需要加一个判断这个数是否可以是一个数组,这个数组的范围. // 3388K 0Ms #include<iostrea ...

  5. POJ-2081 Recaman's Sequence

    Recaman's Sequence Time Limit: 3000MS Memory Limit: 60000K Total Submissions: 22392 Accepted: 9614 D ...

  6. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  7. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  8. POJ 3017 Cut the Sequence

    [题目链接] $O(n^2)$ 效率的 dp 递推式:${ dp }_{ i }=min\left( dp_{ j }+\overset { i }{ \underset { x=j+1 }{ max ...

  9. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

随机推荐

  1. ZBrush中常用3D笔触效果

    3D笔触共有6种绘制方式,分别为Dots(点).Drag Rect(拖拉矩形).Freehand(徒手绘制).Color Spray(彩色喷溅).Spray(喷溅)和Drag Dot(拖拽斑点). 1 ...

  2. NOIp2018模拟赛四十五~??

    欠的太多,咕了咕了 最近复赛临近时间紧,就不每次都写感想和题解了,只写点有意义的好题

  3. 封装cookie的获取,设置与查找

    //获取cookiefunction getCookie(key,value){ var c = document.cookie; var str = key + '=' + value; var r ...

  4. WPF 内存释放

    原文:WPF 内存释放 在WPF 项目中引用到Transitionals 实现图片转场特效,发现每次图片转场内存都会增加,在每次转场后进行内存压缩 记录 解决方案如下: GC.Collect(); G ...

  5. 11g v$wait_chains 与 hanganalyze

    11g之后,通过v$wait_chains视图诊断数据库hang和Contention   11g之前,通常我们数据库hang住了之后,我们会对数据库做hang analyze来进行分析,在11g之后 ...

  6. IOS音频架构之Audio Unit

    在前面的章节部分我们已经对IOS音频结构有了一个清晰的认识,知道Audio Unit是位于整个音频结构的最底层,这一层非常多API已经開始和硬件打交道了.所以比較复杂,有了前面的基础再来看这个部分就比 ...

  7. 二维码框架ZBarSDK的使用和自己定义二维码扫描界面方法

    假设你不知道ZBarSDK怎么用,请下载demo http://download.csdn.net/detail/u013686641/7858917 假设你已经配置好ZBarSDK .那么以下这个类 ...

  8. UE4 中的人工智能解析—ShooterGame为例

    在UE4编辑器中,打开内容浏览器,右击鼠标,创建传说中的行为树: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQTM2MzA2MjM=/font/5a6L ...

  9. bzoj1009: [HNOI2008]GT考试(kmp+矩阵乘法)

    1009: [HNOI2008]GT考试 题目:传送门 题解: 看这第一眼是不是瞬间想起组合数学??? 没错...这样想你就GG了! 其实这是一道稍有隐藏的矩阵乘法,好题! 首先我们可以简化一下题意: ...

  10. iOS - 设置导航栏之标题栏居中、标题栏的背景颜色

    本章实现效果: Untitled.gif 前言: 项目中很多需求是要求自定义标题栏居中的,本人最近就遇到这中需求,如果用系统自带的titleView设置的话,不会居中,经过尝试,发现titleview ...