The Longest Subsequence with Equal Step 最长等差序列/最长等差数列

博客围绕给定整数数组和整数,求数组中最长等差子序列长度展开。给出了输入输出格式示例,输入为数组字符串和整数,输出为整数。还说明了 10 个测试用例的数组长度约束条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Given an array of integers X and an integer step, find the length of the longest subsequence of X where integers in this subsequence form an arithmetic sequence with difference of step.

Sample Input

The string form of an array is in the first line.

The second lind is an integer (string form).

2 4 1 7 -2 -3 -5
-3

Sample Output

An integer.

4

Explanation of Sample

The longest desired subsequence is [4, 1, -2, -5], and the length is 4.

Constraints of 10 test cases

Case 1: 0<X.length<10

Case 2-4: 100<X.length<1000

Case 5-7: 1000<X.length<10000

Case 8-10: 10000<X.length<100000

#include <cstdio>
#include <map>
using namespace std;
int main(){
    int a[100005];
    int n = 0;
    map<int,int>dp;
    while (scanf("%d",&a[n])) {
        n++;
        char ch = getchar();
        if (ch == '\n')
            break;
    }
    int tar;
    scanf("%d",&tar);
    int ans=0;
    for(int i=0;i<n;i++){
        dp[a[i]] = dp[a[i]-tar] + 1;
        if(ans<dp[a[i]])
            ans=dp[a[i]];
    }
    printf("%d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值