題意:求最長的連續的等差 或者 等比序列
思路: 暴力,輸入的時候將它們的差和比分別存在兩個數組中
ps:此題動不動就超時 很無語, 必須用傳統的輸入輸出, 不能加memset, 如果wa了
考慮下n==2時答案是否爲2;
代碼:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 1000000 + 5
#define mod 100000000
using namespace std;
void mys(int& res)
{
int flag=0;
char ch;
while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-'))
if(ch==EOF) res=INF;
if(ch=='-') flag=1;
else if(ch>='0'&&ch<='9') res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
res=flag?-res:res;
}
void myp(int a)
{
if(a>9)
myp(a/10);
putchar(a%10+'0');
}
/*************************THE END OF TEMPLATE************************/
double arr[N] = {0};
double a1[N] = {0};//from 1 to n-1
double a2[N] = {0};//from 1 to n-1
int main()
{
int t, n ,x ,y;
s(t);
w(t--){
s(n);
if(n == 2){
ss(x,y);
printf("2\n");
}
else{
scanf("%lf",&arr[1]);
int ans1 = -1, ans2 = -1, bg1 = 0, bg2 = 0;
for(int i=2; i<=n; i++){
scanf("%lf",&arr[i]);
a1[i-1] = arr[i] - arr[i-1];
a2[i-1] = (arr[i] / arr[i-1]);
if(i>2){
if(a1[i-1] == a1[i-2]) bg1 ++;
if(a1[i-1] != a1[i-2] || i==n){
ans1 = max(ans1, bg1);
bg1 = 0;
}
if(a2[i-1] == a2[i-2]) bg2 ++;
if(a2[i-1] != a2[i-2] || i==n){
ans2 = max(ans2, bg2);
bg2 = 0;
}
}
}
printf("%d\n", max(ans1, ans2) + 2);
}
}
return 0;
}