首页 技术 正文
技术 2022年11月21日
0 收藏 491 点赞 2,743 浏览 2620 个字

Arithmetic Sequence

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1810  Solved: 311
[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.

  题目大意:给你n个数,让你从n个数中找到最长的等差数列。 解题思路:比赛时候,想到了要枚举数列第一项,然后枚举公差,然后还要枚举点什么,所以感觉时间可能会爆,然后就想dp,dp也是想不到怎么优化,当时的想法就是要n^3所以也不敢写,还是不够confident。其实暴力也很简单,只要标记一下有没有这个数字,如果有,就可以往后暴力找,如果没有,就枚举下一个公差。dp的技巧性比较强,由于dp[i][j]表示以a[i]结尾的公差为j的等差数列长度,所以需要记录前面出现的a的下标,很巧妙。  暴力做法:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e3 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL;
int cnt[maxn], a[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(cnt,0,sizeof(cnt));
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
cnt[a[i]]++;
}
sort(a+1,a+1+n);
int ans = 1;
for(int i = 1; i <= n; ++i){ //enum the first item
if(cnt[a[i]] > n-i+1){
ans = max(ans, cnt[a[i]]);
break;
}
for(int j = 1; a[i] + j <= a[n]; ++j){
int d = j, c = a[i], len = 1;
while(cnt[c+d]){
c += d;
len++;
}
ans = max(ans, len);
}
}
printf("%d\n",ans); }
return 0;
}

  

dp做法:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e3 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL;int dp[maxn*2][maxn*2], a[2*maxn], idx[2*maxn]; //dp[i][j] meaning the length that ending up with a[i], common dif is j
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int Max = 0;
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
Max = Max < a[i] ? a[i]:Max;
}
sort(a+1,a+1+n);
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= Max; ++j){
dp[i][j] = 1;
}
}
memset(idx,0,sizeof(idx));
int res = 1;
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= Max; ++j){
if(a[i] > j){
dp[i][j] = dp[idx[a[i]-j]][j] + 1;
}
res = max(res, dp[i][j]);
}
idx[a[i]] = i;
}
printf("%d\n",res);
}
return 0;
}

  

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774