解题报告 之 HDU5328 Problem Killer

You are a "Problem Killer", you want to solve many problems. 
Now you have  n n problems, the  i i-th problem's difficulty is represented by an integer  ai ai ( 1ai109 1≤ai≤109). 
For some strange reason, you must choose some integer  l l and  r r ( 1lrn 1≤l≤r≤n), and solve the problems between the  l l-th and the  r 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 T, indicating the number of cases. 
For each test case, the first line contains a single integer  n n, the second line contains  n n integers  a1,a2,,an a1,a2,⋯,an

T104,n106 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

题意为 : 找一段数中最长的等比等差数列,输出其中最长的一个长度。

暴力 注意 6 5 4 1 1 1 最后的判断,与等比数列采用double存等比。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;

const int INF=0X3f3f3f3f;
const int N=1e6+5;
int n,p[N];
int a[N];
double c[N];
int maxa,maxc;
void find()
{
	int i;
	int tempa=2,tempc=2; 
	for(i=1;i<n-1;i++)
	{
		if(a[i]==a[i-1])
		{
		tempa++;
		}
		else 
		{
			maxa=maxa>tempa?maxa:tempa;
			tempa=2;
		}
		
		if(c[i]==c[i-1]) tempc++;
		else 
		{
			maxc=maxc>tempc?maxc:tempc;
			tempc=2;
		}
	} 
		maxa=maxa>tempa?maxa:tempa;
			maxc=maxc>tempc?maxc:tempc;
}

int main()
{
	int T,i,j;
	scanf("%d",&T);
	while(T--)
	{
		maxa=0;
		maxc=0;
	 scanf("%d",&n);
	 for(i=0;i<n;i++)
	 {
	 	scanf("%d",&p[i]);
	 }
	 if(n<=2) 
	 {
	 	printf("%d\n",n);
	 	continue;
	 }
	 for(i=1;i<n;i++)
	 {
	 	a[i-1]=p[i]-p[i-1];
	 	c[i-1]=(double)p[i]/p[i-1];
	 }
	 find();
	 maxa=maxa>maxc?maxa:maxc;
	 printf("%d\n",maxa);
	 } 
	return 0;
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值