题干:

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方程,抱着冒险的心态o(n^2)交一发,TLE41了,,,把离散化的数组提前预处理了一下,,,又莽了一发,,AC了。。小惊喜。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 1e6+5;
int dp[4004][4004];
int a[4004],b[4004];
int cnt[MAX];
int pos[MAX];
int len,n;
int get(int x) {return lower_bound(b+1,b+len+1,x) - b;
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i],cnt[a[i]]++;sort(b+1,b+n+1);len = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) pos[a[i]] = get(a[i]);for(int i = 1; i<=n; i++) for(int j = 1; j<=n; j++)dp[pos[a[i]]][pos[a[j]]] = 1;for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {if(a[i]!=a[j])dp[pos[a[i]]][pos[a[j]]] = dp[pos[a[j]]][pos[a[i]]]+1;}}int maxx = -1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {maxx = max(maxx,dp[pos[a[i]]][pos[a[j]]]);}}for(int i = 1; i<=n; i++) {maxx = max(maxx,cnt[a[i]]);}printf("%d\n",maxx);return 0 ;}

贴一个题解

再贴一个题解

总结:

思维过程是这样的,,刚开始想暴力(用vector存权值啊之类的乱搞),,一想肯定不行,,有太多重复操作了。。然后想各种优化后来发现就是要求个序列,,所以跟最长上升子序列联系起来了,,后来发现还真可以写出来个递推式,,但是是o(n^2)的不知道会不会T,,而且1600W的数组大小不知道会不会MLE,,交一发,WA,,造样例,,发现全是同一个数的时候就会爆炸,,我就想,,再多也不可能超过数组长度啊,,所以就像把元素相等的时候特判一下(并没想过正确性),改代码,,测样例,,没啥问题,,再交一发,TLE41,,,失望(但是还是有点小惊喜的因为跑起来了最起码说明算法的问题不大)。再想优化,开个数组预处理一波,去掉了时间复杂度的log,再交,AC。

【CodeForces - 255C】Almost Arithmetical Progression (dp,离散化)相关推荐

  1. Almost Arithmetical Progression

    Description Gena loves sequences of numbers. Recently, he has discovered a new type of sequences whi ...

  2. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

  3. 【CodeForces 1253C --- Sweets Eating】DP

    [CodeForces 1253C --- Sweets Eating]DP Description Tsumugi brought n delicious sweets to the Light M ...

  4. codeforces Palindromic characteristics(hash或者dp)

    1.动态规划 用dp(l,r)表示子串s[l..r]的回文串阶数.对于长度len为1的有dp(l,r)=1.对于长度len等于2的,看字符串左右是否相等即可.当r-l>1时,如果s[l]不等于s ...

  5. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  6. Codeforces 429B Working out:dp【枚举交点】

    题目链接:http://codeforces.com/problemset/problem/429/B 题意: 给你一个n*m的网格,每个格子上有一个数字a[i][j]. 一个人从左上角走到右下角,一 ...

  7. CodeForces - 1551F Equidistant Vertices(暴力+dp)

    题目链接:点击查看 题目大意:给出一棵 nnn 个节点组成的树,问选出 kkk 个节点满足相互之间距离相等的方案数有多少 题目分析:n=100n=100n=100,感觉数据范围越小的题目越难发现 ta ...

  8. CodeForces - 1481E Sorting Books(贪心+dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,每次操作可以将任意一本书放到序列的末尾,问最少需要操作多少次,才能使得相同的数字挨在一起 题目分析:不难看出,对每个位置的数都操作一次,是 ...

  9. CodeForces - 1509C The Sports Festival(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的数列,现在需要对其进行重排列,使得贡献之和最小 对于一个排列的贡献来说,对于每个 iii ,则 di=max(a1,a2,...,ai)−min ...

最新文章

  1. NeurIPS 2019最佳论文出炉,今年增设“新方向奖”,微软华人学者获经典论文奖...
  2. P5707 【深基2.例12】上学迟到(python3实现)
  3. day16- django
  4. 成为一名专业的前端开发人员,需要学习什么?
  5. 基于FPGA实现uart串口模块(Verilog)--------接收模块及思路总结
  6. (转)Ubuntu10.04编译FFmpeg
  7. php autoload 性能,PHP __autoload()方法真的影响性能吗?
  8. JS二维数组排序组合
  9. PBC密码学库使用指南
  10. python 自动化测试面试题及答案_自动化测试面试题及答案
  11. CMS:内容管理系统
  12. Google Colab 挂载 Google Drive
  13. oracle优质图书,Oracle经典图书之-Optimizing Oracle Performance
  14. .NET Core剪裁器Zack.DotNetTrimmer升级瘦身引擎,并支持剪裁计划的录制和回放
  15. easyui php分页,jQuery EasyUI 教程-Pagination(分页)
  16. 将数据以表格的形式保存到pdf中
  17. jquery获取已选择和未选择的checkBox项以及清空所选项
  18. W806 基于Arduino开发的GPIO操作示例,勇于尝新
  19. Matlab机器人工具箱(3):双臂操作(从模型建立到轨迹规划)
  20. 事件绑定-addEventListener()和attachEvent()的区别及用法

热门文章

  1. 动态规划——矩阵中的最短路径长度
  2. [Leedcode][JAVA][第999题][直接考虑题意]
  3. java创建两个foo方法_Java类实例化原理 - osc_foo7glsg的个人空间 - OSCHINA - 中文开源技术交流社区...
  4. 请对比html与css的异同,css3与css2的区别是什么?
  5. 拆包--缓冲区查找包头及包尾偏移
  6. java 怎么用 string method return数量_java教程之Map应该怎么用
  7. matlab计算斜方差_计算一幅图像的信噪比
  8. http status 404 – 未找到_从零开始搭建自己的网站004添加404处理页面
  9. html5 支持php标签吗,HTML5新增标签使用方法
  10. Air Data System