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

ural 1079. Maximum

时间:2015-05-05 21:55:30      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:acm算法   algorithm   amp   iostream   namespace   



1079. Maximum

Time limit: 2.0 second
Memory limit: 64 MB
Consider the sequence of numbers ai, i = 0, 1, 2, …, which satisfies the following requirements:
  • a0 = 0
  • a1 = 1
  • a2i = ai
  • a2i+1 = ai + ai+1
for every i = 1, 2, 3, … .
Write a program which for a given value of n finds the largest number among the numbers a0, a1, …, an.

Input

You are given several test cases (not more than 10). Each test case is a line containing an integer n (1 ≤ n ≤ 99 999). The last line of input contains 0.

Output

For every n in the input write the corresponding maximum value found.

Sample

input output
5
10
0
3
4
Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna ‘2001 Informatics Tournament


给你一个n,找出一到n里面a[i]最大值,,当然普通暴力必然超时,另外开一个数组存所有的最大值便好了 。
复杂度是O(n)。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 100000
int a[MAXN];
int sum[MAXN];
int main()
{
    int i;
    int ans=3;
    a[0]=0;
    a[1]=1;
    a[2]=1;
    a[3]=2;
    a[4]=1;
    a[5]=3;
    sum[0]=0;
    sum[1]=1;
    sum[2]=1;
    sum[3]=2;
    sum[4]=2;
    sum[5]=3;
    for(i=6; i<100000; i++)
    {
        if(i&1)
            a[i]=a[i/2]+a[i/2+1];
        else
            a[i]=a[i/2];
        ans=max(ans,a[i]);
        sum[i]=ans;
    }
    int n;
    while(cin>>n,n)
        cout<<sum[n]<<endl;
    return 0;
}

ural 1079. Maximum

标签:acm算法   algorithm   amp   iostream   namespace   

原文地址:http://blog.csdn.net/sky_miange/article/details/45507481

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