HZAU 21——Arithmetic Sequence——————【暴力 or dp】

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

  

时间: 2024-08-05 20:55:35

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

HDOJ 5400 Arithmetic Sequence 暴力枚举

Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 382    Accepted Submission(s): 196 Problem Description A sequence b1,b2,?,bn are called (d1,d2)-arithmetic sequence if and on

Arithmetic Sequence(dp)

Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 19[Submit][Status][Web Board] Description Giving a number sequence A with length n, you should choosingm numbers from A(ignore the order) which can form an arithmetic sequ

hdu 5400 Arithmetic Sequence(模拟)

Problem Description A sequence b1,b2,?,bn are called (d1,d2)-arithmetic sequence if and only if there exist i(1≤i≤n) such that for every j(1≤j<i),bj+1=bj+d1 and for every j(i≤j<n),bj+1=bj+d2. Teacher Mai has a sequence a1,a2,?,an. He wants to know h

Arithmetic Sequence

Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description A sequence b1,b2,?,bn are called (d1,d2)-arithmetic sequence if and only if

[HDOJ5890]Eighty seven(暴力,dp,bitset)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5890 题意:50个数,10W个询问,每次问删掉第i,j,k个数后,是否存在一种选10个数和为87的方案,只需要输出 ’Yes’ 或者 ’No’ 题解:暴力:不同的询问大概2W个,每个暴力bitset DP,抠一抠能卡着过.优化1:先求出一组解,如果询问和解没交就是’Yes’,否则暴力,不同的询问大概1W个:优化2:先预处理出所有询问的答案,能方便的复用之前的DP数组,不用每次从头开始重新求. 学了一

HDOJ 1560 DNA sequence 状压dp 或 IDA*

http://acm.hdu.edu.cn/showproblem.php?pid=1560 题意: 给不超过8个子串,每个子串最多5位,且都只包含ATCG,求最短的母串长度. 分析: 又是上个月写的,所以有点忘了..正解是IDA*.然后可以状压dp,记忆化搜索.dp[i],i用6进制表示,每位表示对应的子串匹配那么多长度所需要的最短母串长度.比如两个子串,13=2*6^1+1*6^0,dp[13]就表示第一个串匹配了第一位,第二个串匹配前两位所需要的最短母串长度. 状态讲完了,不过实际上程序里

LeetCode 1027. Longest Arithmetic Sequence

dp问题,一维是没法dp的,因为我们还要记录一个diff才能判断是否是Arithmetic Sequence. dp[i][diff] 表示以 A[i] 结尾,等差为 diff 的最大长度.从这种角度来看本题和 LeetCode 300. Longest Increasing Subsequence / 354. Russian Doll Envelopes 极为相似. class Solution { public: int longestArithSeqLength(vector<int>

hdu 5400 Arithmetic Sequence

click here~~ ***Arithmetic Sequence*** Problem Description A sequence b1,b2,?,bn are called (d1,d2)-arithmetic sequence if and only if there exist i(1≤i≤n) such that for every j(1≤j<i),bj+1=bj+d1 and for every j(i≤j<n),bj+1=bj+d2. Teacher Mai has a

【leetcode】1027. Longest Arithmetic Sequence

题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is