hzauoj Problem J: Arithmetic Sequence (dp)
2016-05-15 21:19
363 查看
Problem J: Arithmetic Sequence
Time Limit: 1 Sec Memory Limit:128 MB
Submit: 1806 Solved: 308
[Submit][Status][Web
Board]
Description
Giving a number sequence Awith 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.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 2020
int dp[MAXN][MAXN];
int main()
{
int n,num[MAXN];
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
sort(num+1,num+n+1);
for(int i=1;i<=n;i++)
for(int j=1;j<MAXN;j++)
{
dp[i][j]=1;
}
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
if(num[i]>num[j])
{
int d=num[i]-num[j];
dp[i][d]=max(dp[i][d],dp[j][d]+1);
if(dp[i][d]>ans)
ans=dp[i][d];
}
}
int ans2=1,pre=num[1];
for(int i=2;i<=n;i++)
{
if(pre==num[i])
{
ans2++;
ans=max(ans,ans2);
}
else
{
pre=num[i];
ans2=1;
}
}
if(n==1)
printf("1\n");
else
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- JAX-RS (REST Web Services) 2.0 requires Java 1.7 o
- android java.lang.IllegalStateException: trying to requery an already closed cursor
- 自定义UITabbarController及向UITabBar中添加自定义按钮
- HashMap根据value排序
- LintCode : Unique Paths II
- In-memory Computing with SAP HANA读书笔记 - 第七章:Business continuity and resiliency for SAP HANA
- XueXX and Chessboard(中南OJ)
- UI控件--自定义SeekBar样式
- LintCode : Unique Paths
- XueXX and P-P String(中南OJ)
- 【37】String,StringBuffer,StringBuilder区别和概念
- 【37】String,StringBuffer,StringBuilder区别和概念
- 【37】String,StringBuffer,StringBuilder区别和概念
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)
- CharSequence类型
- android UI——绘制加减号按钮
- iOS基本图形绘制--UIBezierPath
- android UI——2D绘图绘制几何图形
- 给UILabel 或者 UIButton标题加下划线
- 在spark中使用Hive报错error: not found: value sqlContext