http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2281

Description

An arithmetic progression is a sequence of numbers a1, a2, ..., ak where the difference of consecutive members ai + 1 − ai is a constant 1 ≤ i ≤ k − 1 . For example, the sequence 5, 8, 11, 14, 17 is an arithmetic progression of length 5 with the common difference 3.

In this problem, you are requested to find the longest arithmetic progression which can be formed selecting some numbers from a given set of numbers. For example, if the given set of numbers is {0, 1, 3, 5, 6, 9}, you can form arithmetic progressions such as 0, 3, 6, 9 with the common difference 3, or 9, 5, 1 with the common difference -4. In this case, the progressions 0, 3, 6, 9 and 9, 6, 3, 0 are the longest.

Input

The input consists of a single test case of the following format.

n
v1 v2 ... vn

n is the number of elements of the set, which is an integer satisfying 2 ≤ n ≤ 5000 . Each vi(1 ≤ i ≤ n) is an element of the set,which is an integer satisfying 0 ≤ vi ≤ 109.vi's are all different, i.e.,vi ≠ vj if i ≠ j

Output

Output the length of the longest arithmetic progressions which can be formed selecting some numbers from the given set of numbers.

Sample Input

6
0 1 3 5 6 9

Sample Output

4
题意:求出序列从小到大排序之后能形成的最长等差数列的长度。
题解:dp,dp[i][j]表示i和j作为前两项时的数列长度,如果先枚举第一项(O(N)),然后第二项如果和第三项在第一项的同一侧的话就是(O(N^2)),整体复杂度是(O(N^3)),难以接受,而如果先枚举第二项,然后第一项和第三项在第二项两侧,根据a[j]+a[k]=2*a[i]可以将复杂度降为O(N^2)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[],dp[][];
int main(){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
if(i!=j)dp[i][j]=;
else dp[i][j]=;
}
}
sort(a+,a++n);
int ans=;
for(int i=n-;i>=;i--){
int j=i-;
int k=i+;
while(j>=&&k<=n){
if(a[j]+a[k]==*a[i]){dp[j][i]=dp[i][k]+;ans=max(ans,dp[j][i]);k++;j--;}
else if(a[j]+a[k]<*a[i]){k++;}
else {j--;}
}
}
cout<<ans<<endl;
return ;
}

一道dp[不太好写]的更多相关文章

  1. dp表模型-如何写出for循环动态规划

    题目很肤浅.. 但是这件事我们要做.. 那么有一种方法叫做刷表法.. 当你发现这个问题具有最优子结构,重叠子问题时 那么这是一个dp问题是使用本方法的前提 画出该dp状态所对应的矩阵 画出转移关系线. ...

  2. 值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)

    这是一道数据范围和评测时间水的可怕的题,只是思路有点难想,BUT假如你的思路清晰,完全了解怎么该做,那就算你写一个反LLL和反SLE都能A,如此水的一道题,你不心动吗? 下面贴出题目 Descript ...

  3. Kickstart Round D 2017 problem A sightseeing 一道DP

    这是现场完整做出来的唯一一道题Orz..而且还调了很久的bug.还是太弱了. Problem When you travel, you like to spend time sightseeing i ...

  4. 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  5. 一道DP

    也是校赛学长出的一道题~想穿了很简单..但我还是听了学长讲才明白. 观察力有待提高. Problem D: YaoBIG’s extra homeworkTime LimitMemory Limit1 ...

  6. nyoj16矩形嵌套(第一道dp关于dag的题目)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=16 题意:有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c, ...

  7. 2018微软实习笔试一道dp题目总结

    题意大概是说在一维数轴上起点和终点的距离是d,现在我们要从起点走到终点.每走一个单位长度消耗一个单位能量,初始时有K单位能量.同时在起点和终点之间分布一些加油站a1,a2,...an,给你加油站数量. ...

  8. python中一行字符串太多写不下时怎么写

    ‘123456789‘ 见此博客    https://www.cnblogs.com/wanderingzj/p/5244451.html str1=('123 '455' '789') 这样的话, ...

  9. dp 单调性优化总结

    对于单调性优化其实更多的是观察dp的状态转移式子的单调性 进而用优先队列 单调队列 二分查找什么的找到最优决策 使时间更优. 对于这道题就是单调性优化的很好的例子 首先打一个暴力再说. f[i][j] ...

随机推荐

  1. Vue + Element UI 实现权限管理系统(优化登录流程)

    完善登录流程 1. 丰富登录界面 1.1 从 Element 指南中选择组件模板丰富登录界面,放置一个登录界面表单,包含账号密码输入框和登录重置按钮. <template> <el- ...

  2. UVa 3602 - DNA Consensus String 水题 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  3. zookeeper:springboot+dubbo配置zk集群并测试

    1.springboot配置zk集群 1.1:非主从配置方法 dubbo: registry: protocol: zookeeper address: ,, check: false 1.2:主从配 ...

  4. 删除Mac OS X中Finder文件打开方式列表的重复程序或失效的

    清理列表, 可以在终端中输入下面提供的一行命令: /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices ...

  5. 第三组 通信一班 030 IPv6 RIPng (PT)

    实验拓扑 地址规划 设备 接口 IPV6  地址/掩码 PC0 / 2001:DB8:30:2:201:42FF:FE8A:7688/64 PC1 / 2001:DB8:30:1:230:A3FF:F ...

  6. 第三篇 功能实现(2) (Android学习笔记)

    第三篇 功能实现(2) ●Activity的四种启动模式 Activity的启动模式有四种,分别是standard.singleTop.singleTask和singleInstance. 在Andr ...

  7. python学习二三事儿(转,整)

    Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...

  8. DevExpress ASP.NET Bootstrap Controls v18.2新功能详解(二)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Boot ...

  9. SharePoint Framework 企业向导(七)

    博客地址:http://blog.csdn.net/FoxDave 企业中的SPFx SharePoint是最成功的企业协作平台之一,能够成功的其中一点是它能够进行扩展并作为一个应用集成平台.SP ...

  10. C点滴成海------Dev C++怎么修改成简体中文

    第一步:选择菜单中的Tools 第二步:选择Tools中的“Envirnoment Options”,即第二个选项 第三步:选择中文并保存 将"1"的语言改成中文就行了