码迷,mamicode.com
首页 > 其他好文 > 详细

解题报告 之 HDU5328 Problem Killer

时间:2015-08-27 15:23:47      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:acm   贪心   数学   hdu5328   多校   

解题报告 之 HDU5328 Problem Killer


Description

You are a "Problem Killer", you want to solve many problems. 
Now you have 技术分享 problems, the 技术分享-th problem‘s difficulty is represented by an integer 技术分享技术分享 (技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享). 
For some strange reason, you must choose some integer 技术分享 and 技术分享 (技术分享技术分享技术分享技术分享技术分享技术分享技术分享), and solve the problems between the 技术分享-th and the 技术分享-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 技术分享, indicating the number of cases. 
For each test case, the first line contains a single integer 技术分享, the second line contains 技术分享 integers 技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享

技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享
 

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
 

题目大意:给你一个数字序列,求其中 最长等差数列 和 最长等比数列 两者中更长者的长度。

分析:从左到右扫一遍,遇到无法继续接着构造等差/等比数列的数字就更新一下最大值,重新开始构造新的。注意重新开始构造不需要从上一个数列的第二个数字开始,因为没有意义,到了当前数字之后 差值/比例还是不一样;而是应该从上一个数列的最后一个作为起点,用新的差值/比例构造。

上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int INF = 0x3f3f3f3f;

int main()
{
	int kase;
	cin >> kase;
	while(kase--)
	{
		int n;
		scanf( "%d", &n );
		int AP = INF;
		double GP = INF;
		int APnum = 2, GPnum = 2;
		int APmax = 0, GPmax = 0;
		int pre, now;

		scanf( "%d", &pre );
		if(n == 1)
		{
			printf( "1\n" );
			continue;
		}

		for(int i = 1; i < n; i++)
		{
			scanf( "%d", &now );
			int APtem = now - pre;
			double GPtem = (double)now / pre;
			if(AP != APtem)
			{
				APmax = max( APmax, APnum );
				APnum = 2;
				AP = APtem;
			}
			else
			{
				APnum++;
			}
			if(GP != GPtem)
			{
				GPmax = max( GPmax, GPnum );
				GPnum = 2;
				GP = GPtem;
			}
			else
			{
				GPnum++;
			}
			pre = now;
		}
		GPmax = max( GPmax, GPnum );
		APmax = max( APmax, APnum );

		printf( "%d\n", max( GPmax, APmax ) );
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

解题报告 之 HDU5328 Problem Killer

标签:acm   贪心   数学   hdu5328   多校   

原文地址:http://blog.csdn.net/maxichu/article/details/48026981

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!