1019: Arithmetic Sequence
2016-05-29 01:06
447 查看
Arithmetic Sequence
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 78 Solved: 29
[Submit][Status][Web Board]
Description
Giving a number sequence A with length n, you should choosing m numbers from A(ignore the order) which can form an arithmetic sequence and make m as large as possible.
Input
There are multiple test cases. In each test case, the first line contains a positive integer n. The second line contains n integers separated by spaces, indicating the number sequence A. All the integers are positive and not more than 2000. The input will end by EOF.
Output
For each test case, output the maximum as the answer in one line.
Sample Input
5
1 3 5 7 10
8
4 2 7 11 3 1 9 5
Sample Output
4
6
HINT
In the first test case, you should choose 1,3,5,7 to form the arithmetic sequence and its length is 4.
In the second test case, you should choose 1,3,5,7,9,11 and the length is 6.
Source
题意大概是从一组数中无序的取出m个数构成等差数列,求m的最大值,用dp求解,dp[i][d]为i以前包括i以d为公差的构成的最大个数的等差数列,j从0到i-1遍历所有节点,考察每一个节点是否与i构成公差为d的数列
废话少说,上代码==
#include <iostream> #include <cstdio> #include <algorithm> #include<cstring> using namespace std; const int MAXN = 2016; int n; int dp[MAXN][MAXN]; int a[MAXN]; int main() { while (~scanf("%d", &n)) { for (int i = 1; i <= n; ++i) { scanf("%d", a+i); } sort(a+1, a+1+n); memset(dp, 0, sizeof dp); int res = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j < i; ++j) { dp[i][a[i]-a[j]] = max(dp[i][a[i]-a[j]], dp[j][a[i]-a[j]]+1); res = max(res, dp[i][a[i]-a[j]]); } } printf("%d\n", res+1); } }
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题