HZAU--21--Arithmetic Sequence(二维dp)
2016-05-16 11:04
417 查看
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1815 Solved: 315
[Submit][Status][Web
Board]
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.
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.
the answer in one line.
and its length is 4.
In the second test case, you should choose 1,3,5,7,9,11 and the length is 6.
找出最长的等差序列,输出长度
二维dp,第二维记录公差
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int num[2020],dp[2020][2020];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(num,0,sizeof(num));
for(int i=0;i<2020;i++)
{
for(int j=0;j<2020;j++)
dp[i][j]=1;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
}
sort(num+1,num+n+1);
int ans=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
int d=num[j]-num[i];
dp[i][d]=max(dp[i][d],dp[j][d]+1);
ans=max(dp[i][d],ans);
}
}
printf("%d\n",ans);
}
return 0;
}
Arithmetic Sequence
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1815 Solved: 315
[Submit][Status][Web
Board]
Description
Giving a number sequence A withlength 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 positiveinteger 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 asthe 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 sequenceand its length is 4.
In the second test case, you should choose 1,3,5,7,9,11 and the length is 6.
找出最长的等差序列,输出长度
二维dp,第二维记录公差
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int num[2020],dp[2020][2020];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(num,0,sizeof(num));
for(int i=0;i<2020;i++)
{
for(int j=0;j<2020;j++)
dp[i][j]=1;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
}
sort(num+1,num+n+1);
int ans=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
int d=num[j]-num[i];
dp[i][d]=max(dp[i][d],dp[j][d]+1);
ans=max(dp[i][d],ans);
}
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- iOSUIView是否包含,相交
- 浅谈getElementById / querySelector
- iOS UIView中坐标转换
- errors running builder "Integerate External Tool Builder "
- POJ 1904 King's Quest tarjan
- break与continue
- Creating a Quick Report with knitr, xtable, R Markdown, Pandoc (and some OpenBLAS Benchmark Results)
- 使用 RequireJS 优化 Web 应用前端
- android-UI(pulltorefresh)
- 生成UUID的一个工具类
- [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)
- MySQL中因为unique key 非空唯一索引存在导致修改主键失败案例
- 347. Top K Frequent Elements
- 解决tableView separate 分割线不从最左边开始问题
- hibernate提供了强大的查询系统,query.setFirstResult,query.setMaxResults
- uva 11572 unique snowflakes——yhx
- uva 1152 4 values whose sum is zero ——yhx
- uva 1605 building for UN ——yhx
- 通过Service Builder改变了原数据库的主键,所有原有记录被删除。
- NUI四种提交数据方式c