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. Activiti BPMN 2.0 designer eclipse插件安装

    官方网是这样说的: https://www.activiti.org/userguide/index.html#springSpringBoot The following installation ...

  2. 在javascript中对于this指向的再次理解

    总所周知,function () {}函数体内的this对象指向的是调用该函数的对象,那么我们看一下这个例子 <script> var length = 3; function fn () ...

  3. HTML一些标记

    4)a标签也可以转换样式为按钮 <a class="btn btn-primary" href="#" role="button"&g ...

  4. 紫书 例题8-13 UVa 11093 (反证法)

    这道题发现一个性质就解决了 如果以i为起点, 然后一直加油耗油, 到p这个地方要去p+1的时候没油了, 那么i, i+1, --一直到p, 如果以这些点 为起点, 肯定也走不完. 为什么呢? 用反证法 ...

  5. Java基础学习总结(45)——JAVA单元测试工具比较

    1.简介 jtest是parasoft公司推出的一款针对java语言的自动化白盒测试工具,它通过自动实现java的单元测试和代码标准校验,来提高代码的可靠性.Jtest先分析每个java类,然后自动生 ...

  6. 【Android】资源系列(二) -- 文件原样保留的资源assets和res/raw文件夹

    这两个文件夹都能够存放文件.而在打包的时候被原样保留. 那用这两个文件夹可以做什么事呢? 1.放一个apk,要用的时候调出来.免得去下载server下载. 2.放一个sql,当app数据库非常大的时候 ...

  7. mysql-创建和操作表

    一.建表 为了用程序创建表,我们可以使用SQL的create table 语句.如下: 每个列之间用逗号隔开,每列的定义以列名开始,后跟列的数据类型. 表的主键可以在创建表时用primary key关 ...

  8. H5学习_番外篇_PHP数据库操作

    1. 文件操作 1.1 打开关闭文件 fopen() resource fopen ( string filename, string mode [, bool use_include_path [, ...

  9. Linux 经常使用快捷键

    桌面下: Alt+F5   取消最大化窗体 Alt+F9   最小化窗体  Alt+F10  最大化窗体  Alt+空格 打开窗体的控制菜单 (点击窗体左上角图标出现的菜单)     ctl+r   ...

  10. EL中的param和params

    转自:https://blog.csdn.net/javamoo/article/details/55667449 ${param.name}等价于request.getParameter(" ...