Multi-University Training Contest 4 — 1002题

来源:互联网 发布:办公室平台软件 编辑:程序博客网 时间:2024/06/08 05:45

Multi-University Training Contest 4 — 1002

Problem Killer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 13    Accepted Submission(s): 9

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

251 2 3 4 6101 1 1 1 1 1 2 3 4 5

 

 

Sample Output

46

 

 

题目大意:

给定一组数,求其中包含的最长等比级数或等差级数的项数。已知数据里边不包含0,这组数的长度范围是从110^9

因为输入的数据较多,为防止超时,用scanf读取数据,然后从头开始遍历,每次计算后边两个数的差,与前两项的差对比,若相同则计数累加,若不同,计数器归2,从新开始计数。同理再次遍历得出最长的等比级数的项数,最后两相比较,输出结果。

需要注意的是,若用int型保存数据,则计算等比级数的公比时,在除的过程中,要注意强制数据类型转换,否则结果会出错。

 

#include<iostream>

#include<stdio.h>

#include<string.h>

#include<math.h>

using namespace std;

int data[1000005];

const double e = 0.00000001;

int main()

{

int t;

scanf("%d", &t);

while (t--)

{

int n;

scanf("%d", &n);

for (int i = 1; i <= n; i++)

{

scanf("%d", &data[i]);

}

int max_ap = 1, max_temp = 2;

int gongcha_first, gongcha_last;

gongcha_first = data[2] - data[1];

for (int i = 3; i <= n; i++)

{

gongcha_last = data[i] - data[i - 1];

if (gongcha_last == gongcha_first)

max_temp += 1;

else

{

gongcha_first = gongcha_last;

if (max_temp > max_ap)

max_ap = max_temp;

max_temp = 2;

}

}

if (max_temp > max_ap)

max_ap = max_temp;

 

int max_gp = 1, max_temp2 = 2;

double gongbi_first, gongbi_last;

gongbi_first = data[2] * 1.0 / data[1];

for (int i = 3; i <= n; i++)

{

gongbi_last = data[i] * 1.0 / data[i - 1];

if (fabs(gongbi_last - gongbi_first) < e)

max_temp2 += 1;

else

{

gongbi_first = gongbi_last;

if (max_temp2>max_gp)

max_gp = max_temp2;

max_temp2 = 2;

}

}

if (max_temp2>max_gp)

max_gp = max_temp2;

if (n == 1)

printf("%d\n", 1);

else

printf("%d\n", max_ap > max_gp ? max_ap : max_gp);

}

return 0;

}

0 0