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. 每个IT安全专业人员应该知道的12种根本漏洞

    每个IT安全专业人员应该知道的12种根本漏洞 每年,IT安全专业人员都面临着数千个新的软件漏洞和数百万个不同的恶意软件程序,但只有12种根本漏洞会让这些软件漏洞和恶意软件程序攻击你的设备.了解这些根本 ...

  2. watch监听

    watch: { getTitle:{ handler:function(val,oldval){ }, deep:true//对象内部的属性监听,也叫深度监听 }, },

  3. JWT的初步了解以及session、cookie机制

    1.什么是状态保持? 想要了解JWT,首先需要知道什么是状态保持,举一个例子来说:无论是在web上还是在手机app上,我们都可以以游客的身份访问,此时都会有登录/注册字眼,当我们登录之后,就会是我们的 ...

  4. java+jsp+sqlserver实现简单的增删改查操作 连接数据库代码

    1,网站系统开发需要掌握的技术 (1)网页设计语言,html语言css语言等 (2)Java语言 (3)数据库 (4)等 2,源程序代码 (1) 连接数据库代码 package com.jaovo.m ...

  5. GenIcam标准(三)

    2.6. 缓存 如果某个实现对每个写操作支持范围.实现和可用状态的检查,通常会触发一系列对相机的读操作.大多数用于有效性检查的数值很少或不会发生变化,所以可以放入缓存.相机描述文件包含所有必需的定义以 ...

  6. 洛谷—— P2663 越越的组队

    https://www.luogu.org/problem/show?pid=2663 题目描述 班级要组织一场综合能力竞赛,全班同学(N个,N是偶数)分成两队互相竞争.老师找到了越越并给了越越一张全 ...

  7. 基于SIP和RTP协议的开源VOIP之QuteCom简单介绍

    **************************************************************************************************** ...

  8. hpuoj--校赛--与学妹滑雪(最短路+精度判断)

    问题 G: 感恩节KK专场--与学妹滑雪 时间限制: 1 Sec  内存限制: 128 MB 提交: 284  解决: 25 [提交][状态][讨论版] 题目描述 这周下的雪好大好大,不过这正和KK学 ...

  9. 38.angular的scope作用域

    转自:https://www.cnblogs.com/best/tag/Angular/ 1. Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. S ...

  10. Java容器源码解析之——LinkedList

    我们直接从源码来分析LinkedList的结构: public class LinkedList<E> extends AbstractSequentialList<E> im ...