acm.hdu.edu.cn/showproblem.php?pid=5328

【题意】

给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多少?

【思路】

尺取,用来解决这样的问题:需要在给的一组数据中找到不大于某一个上限的“最优连续子序列”

分别用双指针找最长的等差数列和等比数列,找最大值就可以了。

注意a 或者 a b既是等差数列,又是等比数列。

【Accepted】

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
int n;
const int maxn=1e6+;
int a[maxn];
int ans;
int AP()
{
int l=;
int r=;
int add=a[r]-a[l];
int res=;
while(l<n && r<n)
{
while(r<n && a[r]-a[r-]==add)
{
r++;
}
res=max(res,r-l);
l=r-;
add=a[r]-a[l];
}
return res;
}
int GP()
{
int l=;
int r=;
double q=(double)a[r]/(double)a[l];
int res=;
while(l<n && r<n)
{
while(r<n && (double)a[r]/(double)a[r-]==q)
{
r++;
}
res=max(res,r-l);
l=r-;
q=(double)a[r]/(double)a[l];
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n<=)
{
printf("%d\n",n);
continue;
}
ans=;
ans=max(ans,AP());
ans=max(ans,GP());
printf("%d\n",ans);
}
return ;
}

【尺取】HDU Problem Killer的更多相关文章

  1. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  2. hdu 6231 -- K-th Number(二分+尺取)

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  3. 【单调队列+尺取】HDU 3530 Subsequence

    acm.hdu.edu.cn/showproblem.php?pid=3530 [题意] 给定一个长度为n的序列,问这个序列满足最大值和最小值的差在[m,k]的范围内的最长子区间是多长? [思路] 对 ...

  4. 【尺取】HDU String

    http://acm.hdu.edu.cn/showproblem.php?pid=5672 [题意] 给定一个小写英语字母组成的字符串,求这个字符串一共包含多少个至少有m个不同字母的连续子序列 [思 ...

  5. #333 Div2 Problem B Approximating a Constant Range (尺取 && RMQ || 尺取 && multiset)

    题目链接:http://codeforces.com/contest/602/problem/B 题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值 ...

  6. A - Jessica's Reading Problem POJ - 3320 尺取

    A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...

  7. HDU 5178 pairs【二分】||【尺取】

    <题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...

  8. HDU 5672 String 【尺取】

    <题目链接> 题目大意:给定一个只由26个小写字母组成的字符串,现在问你至少包含k个不同字母的连续子序列总数有多少. 解题分析:经仔细研究,我们发现,每次尺取到符合要求的最小区间,然后将区 ...

  9. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

随机推荐

  1. Error: IO_ERROR : java.io.IOException: Error while connecting Oozie server. No of retries = 5. Exception = Connection refused (Connection refused)解决办法(图文详解)

    不多说,直接上干货! 问题详情 解决办法 Copy/Paste oozie.services property tag set from oozie-default.xml to oozie-site ...

  2. js数据类型之判断

    js有几种类型,具体是:字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未定义(Undefined). js提供了typ ...

  3. 在windows下编译出linux可执行程序

    set GOARCH=amd64 set GOOS=linux go build xx.go 会生成一个没有后缀的xx二进制文件 将该文件放入linux系统某个文件夹下 赋予权限 chmod 777 ...

  4. nodejs+multer+ajax文件上传

    前端 html代码 + ajax代码 form表单(无需指定action) <form enctype="multipart/form-data" method=" ...

  5. [转]IntelliJ IDEA 自定义方法注解模板

    IntelliJ IDEA 自定义方法注解模板 置顶2017年08月02日 18:04:36 阅读数:32592 最近没啥事开始正式用Eclipse 转入 idea工具阵营,毕竟有70%的开发者在使用 ...

  6. 洛谷P2742 【模板】二维凸包

    题意 求凸包 Sol Andrew算法: 首先按照$x$为第一关键字,$y$为第二关键字从小到大排序,并删除重复的点 用栈维护凸包内的点 1.把$p_1, p_2$放入栈中 2.若$p_{i{(i & ...

  7. Selenium2(WebDriver)开发环境搭建(java版)

    一.开发环境 1.JDK 2.Eclipse 3.Firefox 28.0 4.selenium-java-2.44.0.zip 解压后: 5.selenium-server-standalone-2 ...

  8. java实现网络监听

    Java实现网络监听 import java.net.*; import java.io.*; public class tcpServer { public static void main(Str ...

  9. PMP项目管理学习笔记(12)——范围管理之创建工作分解结构(WBS)

    创建工作分解结构过程是范围管理知识领域中最重要的过程,因为要在此过程明确所要做的全部工作 输入:收集需求和定义范围过程的输出会成为创建工作分解结构过程的输入(需求文档.组织资产过程.项目范围说明书) ...

  10. javascript中 if(变量)和if(变量==true)的区别

    if(判断表达式){执行内容} 如果判断表达式为true,则执行括号中的内容.这里,变量如果不为0,null,undefined,false,都会被处理为true.只要变量有非0的值或是某个对象,数组 ...