原题链接

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。  我想了一下每次从上次记录的最远的位置开始找,有可能记录的位置的前一位也满足等比数列,所以在计算等比数列长度时得计算一下前一位是否满足;

开始的代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <cstring>
using namespace std;
long long a[]; int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
int tmp=;
int t,j;
for(int i=;i<n;i=t)
{
for(j=i+;j<n;j++)
{
if(a[j]!=a[i]+(a[i+]-a[i])*(j-i))
break;
}
t=j-;
tmp=max(tmp,j-i); for(j=i+;j<n;j++)
{
if(a[j-]*a[i+]%a[i]==&&a[j]==a[j-]*a[i+]/a[i])
continue;
else break;
}
tmp=max(tmp,j-i);
//cout<<"t: "<<t<<endl;
//if(j-1>i)
//t=min(t,j-1);
if(t==n-)break;
}
printf("%d\n",tmp);
}
return ;
}

后来修改过的代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <cstring>
using namespace std;
long long a[]; int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
int tmp=;
int t,j;
for(int i=;i<n;i=t)
{
for(j=i+;j<n;j++)
{
if(a[j]!=a[i]+(a[i+]-a[i])*(j-i))
break;
}
t=j-;
tmp=max(tmp,j-i); for(j=i+;j<n;j++)
{
if(a[j-]*a[i+]%a[i]==&&a[j]==a[j-]*a[i+]/a[i])
continue;
else break;
}
int f=;
if(a[i-]*a[i+]%a[i]==&&a[i]==a[i-]*a[i+]/a[i])
f=;
tmp=max(tmp,j-i+f); if(t==n-)break;
}
printf("%d\n",tmp);
}
return ;
}

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. Angularjs学习---ubuntu12.04中karma安装配置中常见的问题总结

    karma启动时出现了很多问题: 1.安装karma前提条件 安装karma首先要安装nodejs,npm然后才可以安装karma.nodejs,npm的安装过程可以参考文章:Angularjs学习- ...

  2. 如何实现 Android 应用的持续部署?

    构建一个高质量的 Android 应用 最大的挑战是什么? 在整个开发流程中,也许 Coding 时莫名的 bug,也许是 Android 开发兼容性问题,多版本多渠道自动打包问题,也有开发工具选择等 ...

  3. 每天一个linux命令(39):grep 命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  4. ubuntu下在apache部署python站点

    ubuntu下在apache部署python站点 我的是ubuntu14 32为的虚拟机,默认安装的python为3.4 环境:apache + mysql + django + python3 软件 ...

  5. KnockoutJS 3.X API 第二章 数据监控(2)监控属性数组

    监控属性数组 如果要对一个对象检测和响应变化,会使用监控属性.如果要对一个序列检测并监控变化,需要使用observableArray(监控属性数组).这在你显示或编辑多个值,需要用户界面的部分反复出现 ...

  6. 【译】采用MVC模式创建一个简单的javascript App

    原文标题:Build A Simple Javascript App The MVC Way 作者:joshcrawmer4 翻译人:huansky 初次翻译,翻译的不好,还请见谅 JavaScrip ...

  7. 理解模板引擎Razor 的原理

    Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.简洁的语法与.NET Framework 结合,广泛应用于ASP.NET MVC 项目.Razor Pad是一 ...

  8. 用命令行编译java并生成可执行的jar包

    用命令行编译java并生成可执行的jar包 1.编写源代码. 编写源文件:CardLayoutDemo.java并保存,例如:I:\myApp\CardLayoutDemo.java.程序结构如下: ...

  9. java之初识服务器跨域获取数据

    当一个项目膨大到无法进行整理时,而作为新负责维护的团队是非常苦恼的.对于想实现两个系统的数据访问,使用Ajax数据请求方式获取jsonp格式的数据 需要有前端jquery库文件. 前端代码通过jque ...

  10. 【干货】如何通过OPC自定义接口来实现客户端数据的读取?

    上篇博文分享了我的知识库,被好多人关注,受宠若惊.今天我把我在项目中封装的OPC自定义接口的程序分享一下.下面将会简单简单介绍下OPC DA客户端数据访问,以及搭配整个系统的运行环境. OPC(OLE ...