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;
}

  

 

HZAU 21——Arithmetic Sequence——————【暴力 or dp】的更多相关文章

  1. Arithmetic Sequence(dp)

    Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 19[Submit][Status][We ...

  2. hdu 5400 Arithmetic Sequence(模拟)

    Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...

  3. [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence

    Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...

  4. (模拟)Arithmetic Sequence -- HDU -- 5400

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  5. LeetCode 1027. Longest Arithmetic Sequence

    原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...

  6. hdu 5400 Arithmetic Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...

  7. 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;

    1020: Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MB Submit:  ->打开链接<- Descriptio ...

  8. 【leetcode】1027. Longest Arithmetic Sequence

    题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...

  9. Arithmetic Sequence

    Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

随机推荐

  1. C#统计英文文本中的单词数并排序

    思路如下:1.使用的Hashtable(高效)集合,记录每个单词出现的次数2.采用ArrayList对Hashtable中的Keys按字母序排列3.排序使用插入排序(稳定) public void S ...

  2. docker中安装Jenkins

    1.获取Jenkins镜像(不要直接docker pull jenkin,这样获取的不是最新的版本,后续安装部分插件会不成功) docker pull jenkins/jenkins 创建文件夹 su ...

  3. js常用的校验代码 (整理)

    /* 用途:检查输入手机号码是否正确 输入:str:字符串 返回:如果通过验证返回true,否则返回false */ function checkMobile(str){ var regu =/^[1 ...

  4. C#加密解密总览

    C#SHA加密 C#MD5加密 C#RSA加密解密 C#DES加密和解密 C#AES加密和解密

  5. FTP ftp部署遇到问题

    FTP  ftp部署遇到问题 一. 二.

  6. KVM虚拟化之windows虚拟机性能调整

    通过KVM安装WindowsXP/2003/7/2008操作系统后,由于默认的磁盘驱动(IDE)性能与网卡驱动(RTL8139100M)的性能都极其低下,需要调整,通过加载Redhatvirtio驱动 ...

  7. Servlet实现Cookie读写

    前言 程序员老婆让程序员去外面买东西,说,老公,你出去买一笼包子吧,遇到卖西瓜的就买一个.老公高兴的出去了,一会儿带着一个包子回来了,老婆很是郁闷问,怎么只买了一个包子,程序员说:因为我看到卖西瓜的了 ...

  8. 洛谷P5265 【模板】多项式反三角函数

    题面 传送门 题解 我数学好像学得太差了 据说根据反三角函数求导公式 \[{d\over dx}\arcsin x={1\over \sqrt{1-x^2}}\] \[{d\over dx}\arct ...

  9. [转贴]VC编译器版本号_MSC_VER and _MSC_FULL_VER

    Visual Studio version and discrimination macros Abbreviation Product name [Visual Studio version] †1 ...

  10. CSS的nth-of-type和nth-child的区别

    <!--源代码--><!DOCTYPE html> <html lang="en"> <head> <meta charset ...