2015暑假多校联合---Problem Killer(暴力)
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
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
题意:输入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(暴力)的更多相关文章
- 2015暑假多校联合---CRB and His Birthday(01背包)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...
- 2015暑假多校联合---Expression(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...
- 2015暑假多校联合---Zero Escape(变化的01背包)
题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 2015暑假多校联合---Friends(dfs枚举)
原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...
- 2015暑假多校联合---Assignment(优先队列)
原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
随机推荐
- DOM_05之DOM、BOM常用对象
1.HTML DOM常用对象之Table:①创建:createTHead():createTBody():createTFoot():②删除:deleteTHead():deleteTFoot():③ ...
- TypeScript实例
interface Person { firstName: string, lastName: string } function greeter(person: Person) { return p ...
- 新浪微博SDK开发(2):上传图片的技术难点
在微博模块中,有一个API是可以发表带一张图片的微博的,当然提交方式是POST.在封装的时候,可能会遇到一个难点——如何上传图片? 要POST微博的同时带有图片,POST的内容必须为MultiPart ...
- 修改NLS_DATE_FORMAT的四种方式
一. 在用户环境变量中指定(LINUX) 在用户的.bash_profile中增加两句: export NLS_LANG=AMERICAN ---这一句必须指定,否则下一句不生效.export NLS ...
- Dijkstra算法(二)之 C++详解
本章是迪杰斯特拉算法的C++实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法图解 3. 迪杰斯特拉算法的代码说明 4. 迪杰斯特拉算法的源码 转载请注明出处:http://www.cnbl ...
- 面向对象编程语言中的接口(Interface)
在大多面向对象的编程语言中都提供了Interface(接口)的概念.如果你事先学过这个概念,那么在谈到“接口测试”时,会不会想起这个概念来!?本篇文章简单介绍一下面向对象编程语言中的Interface ...
- [Linux] LD_LIBRARY_PATH
该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径(该路径在默认路径之前查找).若共享库不在缺省路径/lib或者/usr/lib下,就需要指定其他路径.实践中的一种解决方案是, ...
- [java] 汇率换算器实现(2)
[java] 汇率换算器实现(2) // */ // ]]> // */ // ]]> [java] 汇率换算器实现(2) Table of Contents 1 系列文章地址 2 前 ...
- angularjs中 *.min.js.map 404的问题
初次使用AngularJS,在chrom调试的时候,出现如下问题: GET http://localhost:63342/luosuo/visitor/js/lib/angular-animate.m ...
- group by 和聚合函数
group by 的基本用法 group by做为分组来使用,后面为条件,可以有多个条件,条件相同的为一组,配合聚合函数进行相关统计.在不同数据库中用法稍有不同,这里只测试mysql和oracle. ...