原题链接

Problem Description
You are a "Problem Killer", you want to solve many problems. 
Now you have n problems, the i-th problem's difficulty is represented by an integer ai (1≤ai≤109).
For some strange reason, you must choose some integer l and r (1≤l≤r≤n), and solve the problems between the l-th and the r-th, and these problems' difficulties must form an AP (Arithmetic Progression) or a GP (Geometric Progression). 
So how many problems can you solve at most?

You can find the definitions of AP and GP by the following links:
https://en.wikipedia.org/wiki/Arithmetic_progression
https://en.wikipedia.org/wiki/Geometric_progression

 
Input
The first line contains a single integer T, indicating the number of cases. 
For each test case, the first line contains a single integer n, the second line contains n integers a1,a2,⋯,an.

T≤104,∑n≤106

 
Output
For each test case, output one line with a single integer, representing the answer.
 
Sample Input
2
5
1 2 3 4 6
10
1 1 1 1 1 1 2 3 4 5
 
Sample Output
4
6

题意:输入n个数,求其中最长的区间的长度(区间满足等差数列或者等比数列);

思路:两重循环暴力从每个数开始找,但是这样会超时,可以优化一下,记录从当前数开始的等差数列最远的位置,下次从上次记录的最远的位置开始找,这样复杂度会下降很多;

我发现了一个很有意思的事情,我开始写的程序超时了,后来我删掉了一些东西AC了,但我感觉有bug,于是我想了组数据n=5  5个数4 ,8 ,12 , 18 , 27   后面4个数满足等比数列,最长区间应该是4,而我的程序输出是3,  同样这组数据n=6  6个数8 ,16 ,24 , 36 , 54 , 81  后面5个数满足等比数列,输出应该是5,而我的程序输出是4。  我想了一下每次从上次记录的最远的位置开始找,有可能记录的位置的前一位也满足等比数列,所以在计算等比数列长度时得计算一下前一位是否满足;

开始的代码如下:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <cmath>
  6. #include <cstring>
  7. using namespace std;
  8. long long a[];
  9.  
  10. int main()
  11. {
  12. int T;
  13. cin>>T;
  14. while(T--)
  15. {
  16. int n;
  17. scanf("%d",&n);
  18. for(int i=;i<n;i++)
  19. scanf("%lld",&a[i]);
  20. int tmp=;
  21. int t,j;
  22. for(int i=;i<n;i=t)
  23. {
  24. for(j=i+;j<n;j++)
  25. {
  26. if(a[j]!=a[i]+(a[i+]-a[i])*(j-i))
  27. break;
  28. }
  29. t=j-;
  30. tmp=max(tmp,j-i);
  31.  
  32. for(j=i+;j<n;j++)
  33. {
  34. if(a[j-]*a[i+]%a[i]==&&a[j]==a[j-]*a[i+]/a[i])
  35. continue;
  36. else break;
  37. }
  38. tmp=max(tmp,j-i);
  39. //cout<<"t: "<<t<<endl;
  40. //if(j-1>i)
  41. //t=min(t,j-1);
  42. if(t==n-)break;
  43. }
  44. printf("%d\n",tmp);
  45. }
  46. return ;
  47. }

后来修改过的代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <cmath>
  6. #include <cstring>
  7. using namespace std;
  8. long long a[];
  9.  
  10. int main()
  11. {
  12. int T;
  13. cin>>T;
  14. while(T--)
  15. {
  16. int n;
  17. scanf("%d",&n);
  18. for(int i=;i<n;i++)
  19. scanf("%lld",&a[i]);
  20. int tmp=;
  21. int t,j;
  22. for(int i=;i<n;i=t)
  23. {
  24. for(j=i+;j<n;j++)
  25. {
  26. if(a[j]!=a[i]+(a[i+]-a[i])*(j-i))
  27. break;
  28. }
  29. t=j-;
  30. tmp=max(tmp,j-i);
  31.  
  32. for(j=i+;j<n;j++)
  33. {
  34. if(a[j-]*a[i+]%a[i]==&&a[j]==a[j-]*a[i+]/a[i])
  35. continue;
  36. else break;
  37. }
  38. int f=;
  39. if(a[i-]*a[i+]%a[i]==&&a[i]==a[i-]*a[i+]/a[i])
  40. f=;
  41. tmp=max(tmp,j-i+f);
  42.  
  43. if(t==n-)break;
  44. }
  45. printf("%d\n",tmp);
  46. }
  47. return ;
  48. }

2015暑假多校联合---Problem Killer(暴力)的更多相关文章

  1. 2015暑假多校联合---CRB and His Birthday(01背包)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...

  2. 2015暑假多校联合---Expression(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...

  3. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

  4. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  5. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  6. 2015暑假多校联合---Friends(dfs枚举)

    原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...

  7. 2015暑假多校联合---Assignment(优先队列)

    原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...

  8. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  9. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

随机推荐

  1. EF架构~在T4模版中自定义属性的getter和setter

    回到目录 T4模版为我们在ORM操作上提供了便捷,它很方便的可以对实体进行全局性的修改,之前我介绍过通过T4来为属性加默认性,而今天我主要告诉大家如何使用T4模版将getter,setter块改为自己 ...

  2. EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~再续~添加对各只读服务器的心跳检测

    回到目录 上一讲中基本实现了对数据库的读写分离,而在选择只读数据库上只是随机选择,并没有去检测数据库服务器是否有效,如服务器挂了,SQL服务停了,端口被封了等等,而本讲主要对以上功能进行一个实现,并对 ...

  3. MyBatis学习总结(五)——实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  4. [开发工具]Java开发常用的在线工具

    注明: 本文转自http://www.hollischuang.com/archives/1459.作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工 ...

  5. C#学习系列-抽象方法与虚拟方法的区别

    参考:http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=9851&m=9838&ct=31054 如 ...

  6. C#获取文本文件的编码,自动区分GB2312和UTF8

    C# 获取文本文件的编码,自动区分GB2312和UTF8 以下是获取文件编码的一个类 using System; using System.IO; using System.Text; /// < ...

  7. salesforce 零基础学习(十七)Trigger用法

    看本篇之前可以相应阅读以下Trigger相关文章: 1.https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigge ...

  8. iOS-UICollectionView

    1--------------------------------------------------------------------------------------------------- ...

  9. VS报错:The build tools for v140 (Platform Toolset = 'v140') cannot be found

    VS低版本打开高版本常会出现的错: The build tools for v140 (Platform Toolset = 'v140') cannot be found. To build usi ...

  10. UML基础系列:用例图

    1. 概述 用例图(Use Case Diagram)描述“用户.需求.系统功能单元”之间的关系,是参与者所能观察和使用到的系统功能模型图. 用例图用于需求分析阶段 用例图包含6个基本元素:参与者(A ...