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 ...
随机推荐
- linux tail
tail 命令从指定点开始将文件写到标准输出,使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...
- linux驱动系列之makefile
在linux环境下做嵌入式无论是编写应用程序还是驱动程序等等,都需要用make来进行程序的编译,就需要学会自己编写Makefile.Makefile主要的作用有3点:1.决定编译哪些文件 2.怎样编译 ...
- SQLSERVER数据库管理数据库原理
数据库的存储结构: 1.逻辑存储结构:是指数据库有那些性质的信息组成,即一个数据库由若干用户可视的各种数据库对象构成,如:表,视图,索引,存储过程等 2.物理存储结构:表现为存储数据的各类操作系 ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- 3141: [Hnoi2013]旅行 - BZOJ
Description Input 第一行为两个空格隔开的正整数n, m,表示旅行的城市数与旅行所花的月数.接下来n行,其中第 i行包含两个空格隔开的整数Ai和Bi,Ai表示他第i个去的城市编号.Bi ...
- POJ - 1741 Tree
DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001).Defin ...
- spring的配置模式与注解模式基础
“依赖注入”是spring的核心特征,在Web服务器(如Tomcat)加载时,它会根据Spring的配置文件中配置的bean或者是通过注解模式而扫描并装载的bean实例自动注入到Application ...
- 如何将word中上下两行文字对齐?
一.问题来源及描述 本科毕设的时候积累的问题,整理如下. 红头文件下面的署名,上下要对齐. 二.解决办法 经验证,第一次拉标尺要把标尺放在第一行的光标处,为了换行后,再次enter,tab后到与上一行 ...
- Searching in a rotated and sorted array
Given a sorted array that has been rotated serveral times. Write code to find an element in this arr ...
- VIM的高级使用
VIM的高级使用 转:http://www.cnblogs.com/itech/archive/2012/02/22/2363111.html 1)一些常用的Vim配置,在~/.vimrc中 syn ...