Timus 1079. Maximum 要求输出指定数列中的最大值。


1079. Maximum

Time Limit: 2.0 second
Memory Limit: 16 MB

Consider the sequence of numbers ai, i = 0, 1, 2, …, which satisfies the following requirments:

  • 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 (0 < N < 100000) finds the largest number among the numbers a0, a1, …, aN.

Input

Input contains not more than 10 lines containing one number N. The last line contains 0.

Output

For every N in the input write the coresponding maximum value found.

Sample

input output
5
10
0
3
4

Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament


解法一:

 1 using System;
 2 using System.IO;
 3 
 4 namespace Skyiv.Ben.Timus
 5 {
 6   // http://acm.timus.ru/problem.aspx?space=1&num=1079
 7   class T1079a
 8   {
 9     static void Main()
10     {
11       new T1079a().Run(Console.In, Console.Out);
12     }
13 
14     void Run(TextReader reader, TextWriter writer)
15     {
16       for (; ; )
17       {
18         int n = int.Parse(reader.ReadLine());
19         if (n == 0) break;
20         writer.WriteLine(((n < 3) ? 1 : GetMaximum(n, 3, 1, 1)));
21       }
22     }
23 
24     int GetMaximum(int n, int x, int s1, int s2)
25     {
26       int s0 = s1 + s2;
27       int x1 = x * 2 - 1;
28       if (n < x1) return s0;
29       int x2 = x1 + 2;
30       int t1 = (n < x1) ? 0 : GetMaximum(n, x1, s1, s0);
31       int t2 = (n < x2) ? 0 : GetMaximum(n, x2, s0, s2);
32       return (t1 > t2) ? t1 : t2;
33     }
34   }
35 }

解法二:

 1 using System;
 2 using System.IO;
 3 using System.Drawing;
 4 using System.Collections.Generic;
 5 
 6 namespace Skyiv.Ben.Timus
 7 {
 8   // http://acm.timus.ru/problem.aspx?space=1&num=1079
 9   class T1079b
10   {
11     static void Main()
12     {
13       new T1079b().Run(Console.In, Console.Out);
14     }
15 
16     void Run(TextReader reader, TextWriter writer)
17     {
18       Point[] list = GetList(100000);
19       for (; ; )
20       {
21         int n = int.Parse(reader.ReadLine());
22         if (n == 0) break;
23         writer.WriteLine(GetMaximum(list, n));
24       }
25     }
26 
27     int[] GetSequence(int n)
28     {
29       int[] a = new int[n];
30       a[1] = 1;
31       for (int i = 2; i < n; i++)
32       {
33         int k = i >> 1;
34         if ((i & 1) == 0) a[i] = a[k];
35         else a[i] = a[k] + a[k + 1];
36       }
37       return a;
38     }
39 
40     Point[] GetList(int n)
41     {
42       List<Point> list = new List<Point>();
43       int[] sequence = GetSequence(n);
44       int max = int.MinValue;
45       for (int i = 1; i < sequence.Length; i++)
46       {
47         if (sequence[i] > max)
48         {
49           max = sequence[i];
50           list.Add(new Point(i, max));
51         }
52       }
53       return list.ToArray();
54     }
55 
56     int GetMaximum(Point[] list, int key)
57     {
58       int low = 0, high = list.Length - 1;
59       int mid = 0, key0 = 0;
60       while (low <= high)
61       {
62         mid = (low + high) / 2;
63         key0 = list[mid].X;
64         if (key > key0) low = mid + 1;
65         else if (key < key0) high = mid - 1;
66         else return list[mid].Y;
67       }
68       if (key < key0) mid--;
69       return list[mid].Y;
70     }
71   }
72 }

在解法二中,对 (0 < N < 105) 先求出所有的 a[n],然后求出此范围内的最大值项,共 103 项,如下:

(    1    1) (    3    2) (    5    3) (    9    4) (   11    5) (   19    7) (   21    8) (   35    9)
(   37   11) (   43   13) (   69   14) (   73   15) (   75   18) (   83   19) (   85   21) (  139   23)
(  147   26) (  149   29) (  165   30) (  171   34) (  277   37) (  293   41) (  299   47) (  331   49)
(  339   50) (  341   55) (  555   60) (  587   67) (  595   69) (  597   76) (  661   79) (  683   89)
( 1109   97) ( 1173  108) ( 1189  109) ( 1195  123) ( 1323  128) ( 1355  129) ( 1363  131) ( 1365  144)
( 2219  157) ( 2347  175) ( 2379  178) ( 2387  181) ( 2389  199) ( 2645  207) ( 2709  208) ( 2731  233)
( 4437  254) ( 4691  257) ( 4693  283) ( 4757  287) ( 4779  322) ( 5291  335) ( 5419  337) ( 5451  338)
( 5459  343) ( 5461  377) ( 8875  411) ( 9387  458) ( 9515  465) ( 9547  467) ( 9555  474) ( 9557  521)
(10581  542) (10837  545) (10923  610) (17749  665) (18771  674) (18773  741) (19029  752) (19093  753)
(19115  843) (21163  877) (21675  882) (21803  883) (21835  885) (21843  898) (21845  987) (35499 1076)
(37547 1199) (38059 1217) (38187 1220) (38219 1223) (38227 1241) (38229 1364) (42325 1419) (43349 1427)
(43605 1428) (43691 1597) (70997 1741) (75091 1765) (75093 1940) (76117 1969) (76373 1973) (76459 2207)
(84651 2296) (86699 2309) (87211 2311) (87339 2312) (87371 2317) (87379 2351) (87381 2584)

最后,对每一个输入的 N 值 (0 < N < 105,最多10个,时间限制是 2.0 秒) 用二分查找法查上面的表就行了。

Timus 1396. Maximum. Version 2 是同样的问题,但是要求 (0 < N < 1018) 且输入可多达 10000 个,时间限制是 1.0 秒。

Timus 1079. Maximum相关推荐

  1. Ural(Timus) 1146. Maximum Sum

    DP,最大子矩阵和:先按列压缩为一维i,在用最大连续子序列和来求.在枚举列压缩求和的时候,为了提高速度,可以在输入的时候先保存下来,就不用每次都去计算,不过再代码中没有写 另外这题不允许空矩阵,即至少 ...

  2. LightOJ - 1079 Just another Robbery —— 概率、背包

    题目链接:https://vjudge.net/problem/LightOJ-1079 1079 - Just another Robbery     PDF (English) Statistic ...

  3. 【C++】C++11 STL算法(六):最小/最大操作(Minimum/maximum operations)、比较运算(Comparison operations)

    目录 最小/最大操作(Minimum/maximum operations) 一.max 1.原型: 2.说明: 3.官方demo 二.max_element 1.原型: 2.说明: 3.官方demo ...

  4. Lintcode42 Maximum Subarray II solution 题解

    [题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The ...

  5. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  6. UVA11059 Maximum Product

    问题链接:UVA11059 Maximum Product.基础级练习题,用C语言编写程序. 题意简述:输入n个整数序列,有正有负,求这个序列中最大连续累乘的子序列,其最大的值为多少.如果结果为负数, ...

  7. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  8. 贪心 ---- E. Maximum Subsequence Value[位运算]

    E. Maximum Subsequence Value 题目大意:有点难解释..建议自己看题.我这里就粗略解释:给定一个数组aaa,要求选出具有最大价值的子序列.假设此子序列的长度为kkk,那么最大 ...

  9. Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 CF1401D Maximum Distributed Tree(树上贪心) 给定一棵 nnn 个节点 ...

最新文章

  1. 【Android 插件化】插件化原理 ( 类加载器 )
  2. 在win7下将CapsLock按键变成esc
  3. kafka数据不丢失不重复_超高速底层系统数据复制,安全精准不丢失
  4. linux vim 快速定位位置,vim快速移动定位的一些操作命令
  5. [转载]压岁钱年年涨的行情不要太纠结
  6. matlab-自控原理 已知x~=Ax+Bu中的AB矩阵和X0,求单位输入下的时间响应
  7. RegExp-1 【转义符号与字符、正则基础、修饰/元字符】
  8. java 内嵌chrome_[Java教程]Jcef内嵌浏览器windows版本的编译及使用
  9. 小D课堂-nexus
  10. CCNA中英对照题库(285道选择题)
  11. 工作半年就迷茫,给你一盏明灯
  12. 计算机excel还原,Excel文件恢复方法
  13. 基于X86汇编语言的简易打字游戏实现
  14. win8笔记本做wifi热点设置教程
  15. 2021年9款优秀的大数据可视化BI软件
  16. 传奇世界私服务器端制作,关于内网架设传奇世界私服问题的一些解答
  17. windows安装sonarqube7.4+sonar-scanner-cli【JDK8+MySQL】
  18. 怎么把图片转换成BMP格式
  19. 2.命名空间实现机制
  20. 【Java】获取某年某月有多少天

热门文章

  1. 淘宝TFS文件系统配置
  2. 使用SQL语句添加和删除约束
  3. [转]Zend Framework + Smarty 应用实例
  4. cisco学习笔记(6)-acl
  5. C++指针探讨 (二) 函数指针
  6. java(15)-策略模式(Strategy Pattern)
  7. linux ls -l 命令 详解
  8. android Gallery实现异步加载网络图片
  9. .net OADate 转javascript的Datetime js 5位 日期 转换
  10. Laravel框架学习笔记(一)——phpstudy下的安装配置