HDU 5328 Problem Killer
题意:给一段序列,求连续的子序列中最长的等差数列或者等比数列的长度。
解法: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的更多相关文章
- hdu 5328 Problem Killer(杭电多校赛第四场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...
- HDU 5328 Problem Killer(水题)
题意: 给一个序列,要找一个等差或等比的连续子序列,求其最长的长度. 思路: 扫两遍,判断等差或等比即可.从左往右扫,维护一个滑动窗口,考虑新加进来的数,如果满足了要求,则更新长度,否则只留最后两个数 ...
- 递推DP HDOJ 5328 Problem Killer
题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...
- 2015暑假多校联合---Problem Killer(暴力)
原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...
- 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 - ...
- 【尺取】HDU Problem Killer
acm.hdu.edu.cn/showproblem.php?pid=5328 [题意] 给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多 ...
- hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1426 Sudoku Killer(dfs 解数独)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 1426 Sudoku Killer (dfs)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- 【Entity Framework】初级篇--ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager类的介绍
本节,简单的介绍EF中的ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager这个几个比较重要的类,它们都位于System.Data ...
- chm文件打开,有目录没有内容
下载下来的一些chm文件,打开的是否提示“打开此文件前总是询问”,不理睬直接打开后,只有目录,页面内容都显示“已取消到本页的导航”.原因是该CHM文件的发行商未知,将“打开此文件前总是询问”选项取消后 ...
- P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
裸的强连通 ; type node=record f,t:longint; end; var n,m,dgr,i,u,v,num,ans:longint; bfsdgr,low,head,f:arra ...
- POJ1734 - Sightseeing trip
DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...
- svn is already locked解决方案
在出错文件夹下,鼠标右键TortoiseSVN->Clean up. SVN错误:Attempted to lock an already-locked dir 1.出现这个问题后使用“清理”功 ...
- Recommender Systems协同过滤
第一部分是学习ID3时候积累的. 一.以前写的基础知识 1.信息:是用来消除不确定性的度量,信息量的大小,由所消除的不确定性的大小来计量(香农). 2.由于不确定性是由随机性引起的,所以用概率来描述和 ...
- 读取tiled地图
原地址:http://www.unity蛮牛.com/thread-20854-1-1.html Tile是一个非常好用的地图编辑器,一直以来我都在找支持tilemap的unity2D插件,但是找 ...
- 项目图片上传存储的目录部分代码思路Calendar类获取年月日
在项目中将产品图片全部放置在一个文件夹下面的话,如果图片很多的话,加载速度会减慢,可以按照文件夹按年,月,日来分开存放,图片文件名字取系统时间long类型加上5位随机数字码避免重复. Calendar ...
- 盘点 OSX 上最佳的 DevOps 工具
[编者按]对于运维人员来说,他们往往需要各种各样的工具来应对工作需求,近日 Dustin Collins 通过「The Best DevOps Tools on OSX」一文对 OSX 平台上的工具进 ...
- cocos2d-x 多分辨率适配详解(转载),以前北京团队设计的游戏,也是用这套方案
http://blog.csdn.net/kyo7552/article/details/17163487 多种分辨率的适配一直都是一个蛋疼的问题,各家公司可能都有自己的一套方案.今天我为大家介绍的是 ...