Recaman's Sequence
Time Limit: 3000ms
Memory Limit: 60000KB
This problem will be judged on PKU. Original ID: 208164-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.
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.
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
解题:离线搞。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <stack> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <ctime> 12 #define LL long long 13 #define INF 0x3f3f3f3f 14 #define pii pair<int,int> 15 16 using namespace std; 17 const int maxn = 500010; 18 int dp[maxn]; 19 bool vis[5000000]; 20 struct node { 21 int k,ans,o; 22 }; 23 node inp[1000000]; 24 bool cmp1(const node &x,const node &y) { 25 return x.k < y.k; 26 } 27 bool cmp2(const node &x,const node &y) { 28 return x.o < y.o; 29 } 30 int main() { 31 int tot = 0,tmp,cnt; 32 for(int i = 1; i < maxn; ++i) { 33 dp[i] = dp[i-1]-i; 34 if(dp[i] <= 0 || vis[dp[i]]) 35 dp[i] = dp[i-1]+i; 36 vis[dp[i]] = true; 37 } 38 while(~scanf("%d",&tmp)&&(~tmp)) { 39 inp[tot].k = tmp; 40 inp[tot].o = tot++; 41 } 42 sort(inp,inp+tot,cmp1); 43 for(int i = cnt = 0; i < maxn; ++i) 44 while(i == inp[cnt].k) inp[cnt++].ans = dp[i]; 45 sort(inp,inp+tot,cmp2); 46 for(int i = 0; i < tot; ++i) 47 printf("%d ",inp[i].ans); 48 return 0; 49 }