题意:给一段序列,求连续的子序列中最长的等差数列或者等比数列的长度。

解法:O(n)的扫两遍一次判等差一次判等比就好了。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int a[1000005];
bool equal(int a1, int a2, int b1, int b2)
{
int r1 = __gcd(a1, a2), r2 = __gcd(b1, b2);
a1 /= r1;
a2 /= r1;
b1 /= r2;
b2 /= r2;
return a1 == b1 && a2 == b2;
}
int main()
{
int T;
while(~scanf("%d", &T))
{
while(T--)
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int ans = 1;
int flag = 0;
int res = 2;
for(int i = 2; i <= n; i++)
{
if(!flag)
{
res = 2;
flag = 1;
ans = max(res, ans);
}
else
{
if(a[i] - a[i - 1] == a[i - 1] - a[i - 2])
{
res++;
ans = max(ans, res);
}
else
{
flag = 0;
i--;
}
}
}
flag = 0;
res = 2;
for(int i = 2; i <= n; i++)
{
if(!flag)
{
res = 2;
flag = 1;
ans = max(res, ans);
}
else
{
if(equal(a[i], a[i - 1], a[i - 1], a[i - 2]))
{
res++;
ans = max(ans, res);
}
else
{
flag = 0;
i--;
}
}
}
printf("%d\n", ans);
}
}
return 0;
}

  

HDU 5328 Problem Killer的更多相关文章

  1. hdu 5328 Problem Killer(杭电多校赛第四场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...

  2. HDU 5328 Problem Killer(水题)

    题意: 给一个序列,要找一个等差或等比的连续子序列,求其最长的长度. 思路: 扫两遍,判断等差或等比即可.从左往右扫,维护一个滑动窗口,考虑新加进来的数,如果满足了要求,则更新长度,否则只留最后两个数 ...

  3. 递推DP HDOJ 5328 Problem Killer

    题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...

  4. 2015暑假多校联合---Problem Killer(暴力)

    原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...

  5. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

  6. 【尺取】HDU Problem Killer

    acm.hdu.edu.cn/showproblem.php?pid=5328 [题意] 给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多 ...

  7. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU 1426 Sudoku Killer(dfs 解数独)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...

  9. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. 泛形集合List<T>

    public class Person { /// <summary> /// 姓名 /// </summary> private string name; public st ...

  2. 【BZOJ 1202】 [HNOI2005]狡猾的商人

    Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...

  3. python学习笔记21(正则表达式)

    正则表达式模式: 模式 描述 ^ 匹配的开始的 $ 匹配行尾 . 匹配除换行符的任何单个字符.使用-m选项允许其匹配换行符也是如此. [...] 匹配括号内任何单个字符 [^...] 匹配非单个字符集 ...

  4. POJ - 1741 Tree

    DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001).Defin ...

  5. 解决 Eclipse build workspace 慢,validation javascript 更慢的问题

    鸣谢:http://zuoming.iteye.com/blog/1430925 ------------------------------------------------ 如果用到js插件或者 ...

  6. Recommender Systems协同过滤

    第一部分是学习ID3时候积累的. 一.以前写的基础知识 1.信息:是用来消除不确定性的度量,信息量的大小,由所消除的不确定性的大小来计量(香农). 2.由于不确定性是由随机性引起的,所以用概率来描述和 ...

  7. 自助Linux之问题诊断工具strace

    转  http://www.cnblogs.com/bangerlee/archive/2012/02/20/2356818.html 引言 “Oops,系统挂死了..." “Oops,程序 ...

  8. Tiny6410 交叉编译helloworld程序

    在工作目录下建立helloworld.c文件 #include <stdio.h> main() { printf("helloworld!\n"); } 保存关闭后. ...

  9. MVC 国内架构设计

    http://www.cnblogs.com/guomingfeng/p/mvc-ef-query.html http://www.cnblogs.com/haogj/p/3127795.html

  10. mvc+entity framework database first,生成的model每次更新一个表会更新所有的model

    在使用Entity Framework 的Database frist或model first时,直接加attribute到modle类上是太现实也不合理的,因为model类是自动生成的,重新生成后会 ...