码迷,mamicode.com
首页 > 其他好文 > 详细

51nod 1062 序列中最大的数(打表预处理)

时间:2017-07-31 09:54:58      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:clu   ref   alt   时间   image   text   51nod   tps   file   

题目来源: Ural 1079
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
技术分享 收藏
技术分享 关注
技术分享 取消关注
有这样一个序列a:
a[0] = 0
a[1] = 1
a[2i] = a[i]
a[2i+1] = a[i] + a[i+1]
 
输入一个数N,求a[0] - a[n]中最大的数。
a[0] = 0, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 1, a[5] = 3, a[6] = 2, a[7] = 3, a[8] = 1, a[9] = 4, a[10] = 3。
例如:n = 5,最大值是3,n = 10,最大值是4。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10)
第2 - T + 1行:T个数,表示需要计算的n。(1 <= n <= 10^5)
Output
共T行,每行1个最大值。
Input示例
2
5
10
Output示例
3
4

直接看代码吧

技术分享
 1 #include <iostream>
 2 using namespace std;
 3 int ans[100005];
 4 int a[100005];
 5 int n,t;
 6 void init()
 7 {
 8     ans[0]=0;
 9     ans[1]=1;
10     a[0]=0;
11     a[1]=1;
12     for(int i=2;i<=100000;i++)
13     {
14         if(i%2==0)
15             a[i]=a[i/2];
16         else
17             a[i]=a[i/2]+a[i/2+1];
18         ans[i]=max(ans[i-1],a[i]);
19     }
20 }
21 int main()
22 {
23     ios::sync_with_stdio(false);
24     cin>>t;
25     init();
26     while(t--)
27     {
28         cin>>n;
29         cout<<ans[n]<<endl;
30     }
31     return 0;
32 }
View Code

 

 

51nod 1062 序列中最大的数(打表预处理)

标签:clu   ref   alt   时间   image   text   51nod   tps   file   

原文地址:http://www.cnblogs.com/onlyli/p/7261320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!