C. Almost Arithmetical Progression
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as:

  • a1 = p, where p is some integer;
  • ai = ai - 1 + ( - 1)i + 1·q (i > 1), where q is some integer.

Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.

Sequence s1,  s2,  ...,  sk is a subsequence of sequence b1,  b2,  ...,  bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1  ≤  i1  <  i2  < ...   <  ik  ≤  n), that bij  =  sj. In other words, sequence s can be obtained from b by crossing out some elements.

Input

The first line contains integer n (1 ≤ n ≤ 4000). The next line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106).

Output

Print a single integer — the length of the required longest subsequence.

Examples
input
2
3 5
output
2
input
4
10 20 10 30
output
3
Note

In the first test the sequence actually is the suitable subsequence.

In the second test the following subsequence fits: 10, 20, 10.

dp[i][j]表示以a[i]为尾,a[j]是这个序列前一个数的最长长度

dp[i][j]=max(dp[j][k]+1){0<=k<j&&a[k]==a[i]}但这样复杂度是O^3

但是dp[j][k]中的k肯定是选择最靠近j的那个,假如有k1<k2<……<kx<j,dp[j][kx]>=dp[j][k(1~x-1)]

这样的话,复杂度就变成O^2了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
typedef long long ll;
#define X first
#define Y second
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define sd(x) scanf("%d",&(x))
#define Pi acos(-1.0)
#define sf(x) scanf("%lf",&(x))
#define ss(x) scanf("%s",(x))
#define maxn 10000000
#include <ctime>
const int inf=0x3f3f3f3f;
const long long mod=;
using namespace std;
int dp[][];
int num[];
int main()
{
#ifdef local
freopen("in","r",stdin);
//freopen("data.txt","w",stdout);
int _time=clock();
#endif
int n;
cin>>n;
for(int i=;i<=n;i++)
sd(num[i]);
int ans=;
int la;
for(int i=;i<=n;i++)
{
la=;
for(int j=;j<i;j++)
{
dp[i][j]=dp[j][la]+;
if(num[i]==num[j])la=j;
ans=max(dp[i][j],ans);
}
}
cout<<ans<<endl;
#ifdef local
printf("time: %d\n",int(clock()-_time));
#endif
}

cf255C Almost Arithmetical Progression的更多相关文章

  1. Codeforces Round #156 (Div. 2)

    A. Greg's Workout 模3求和,算最大值. B. Code Parsing 最后左半部分为x,右半部分为y,那么从中间不断去掉xy,直到其中一种全部消去. C. Almost Arith ...

  2. OUC_OptKernel_oshixiaoxiliu_好题推荐

    poj1112 Team Them Up! 补图二分图+dp记录路径codeforces 256A Almost Arithmetical Progression dp或暴力 dp[i][j] = d ...

  3. Codeforces Round #Pi (Div. 2) C. Geometric Progression map

    C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. CodeForces 567C Geometric Progression

    Geometric Progression Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  5. hdu 5278 Geometric Progression 高精度

    Geometric Progression Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contes ...

  6. hdu 5429 Geometric Progression(存个大数模板)

    Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...

  7. 文献导读 | Single-Cell Sequencing of iPSC-Dopamine Neurons Reconstructs Disease Progression and Identifies HDAC4 as a Regulator of Parkinson Cell Phenotypes

    文献编号:19Mar - 11 2019年04月23日三读,会其精髓: 相信这种方法的话,那么它的精髓是什么,如何整合出这个core gene set. 首先要考虑样本的选择,样本里是否存在明显的分层 ...

  8. Codeforces 758F Geometrical Progression

    Geometrical Progression n == 1的时候答案为区间长度, n == 2的时候每两个数字都可能成为答案, 我们只需要考虑 n == 3的情况, 我们可以枚举公差, 其分子分母都 ...

  9. The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE)

    The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE) Today I tried ...

随机推荐

  1. CSS3选择器~一看吓一跳,这么多不会

    复习CSS时发现很多选择器不会,因为平时很少用到.现在干脆一不做二不修,全部温习一遍.本文参考http://css.doyoe.com/. 一.元素选择器 图片来自:http://css.doyoe. ...

  2. UILabel的讲解

    首先,我先自定义几个名词,方便接下来的讲解工作.如下图所示: 接下来,通过五个方面来讲解我们能对UILabel做出哪些改变或者称之为设置: 1.文字 1.1普通文字:内容text.字体大小font.字 ...

  3. sqlplus连接oracle问题

    第一次在自己电脑上连接远端数据库,尝试了一上午居然还没成功,后来在网上找了一堆的方法终于是连接上了    -_-!! 总结一下:简单操作:一步到位 我主要是用的oracle 11g的数据库,sqlpl ...

  4. 搭建MySQL高可用负载均衡集群

    1.简介 使用MySQL时随着时间的增长,用户量以及数据量的逐渐增加,访问量更是剧增,最终将会使MySQL达到某个瓶颈,那么MySQL的性能将会大大降低.这一结果也不利于软件的推广. 那么如何跨过这个 ...

  5. 银盛支付ecshop,shopex,shopnc在线支付接口,php版本支付接口开发

    最近应一个客户的要求,给他的一个ecshop商城开发银盛支付在线支付接口.银盛支付服务股份有限公司(简称银盛支付)成立于2009年7月,总注册资本14000万元人民币,员工队伍持续壮大.2011年5月 ...

  6. 将linux的HOME目录下的文件夹名字改回英文

    为了使用起来方便,装了Ubuntu中文版,自然在home文件里用户目录的“桌面”.“图片”.“视频”.“音乐”……都是中文的.很多时候都喜欢在桌面上放一些要操作的文件,linux里命令行操作又多,难免 ...

  7. 初识bd时的一些技能小贴士

    既然小豆腐如此给力,而且充分的利用主动学习的优势,已经有了迅速脑补,压倒式的优势,不过这只是表面而已,一切才刚刚开始,究竟鹿死谁手,还有待验证. 以上可以看到,小豆腐为什么拼命的要teach我们了么, ...

  8. Xamarin GitHub 下载的源码运行不了

     初学Xamarin ,各种折腾,大概这公司破电脑配置差,老是很多问题. GitHub 真是个好东西,可以参考别人做的,不过下载来运行不了就各种折腾了,为此我重装电脑两次了,反正win10安装就十几分 ...

  9. Android中调用文件管理器并返回选中文件的路径

    实际项目中经常需要调用文件管理器,选择下载路径或者上传的本地文件路径.今天就给大家做个demo示范该功能的实现过程. 一.实现效果预览 以下为三星S6的样机测试效果,当然不同手机调用后的效果不一样. ...

  10. python——面向对象相关

    其他相关 一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 1 2 3 4 5 6 class Foo(object):     pass   obj = Foo( ...