一道dp[不太好写]

http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2281

Description

An arithmetic progression is a sequence of numbers a1, a2, ..., ak where the difference of consecutive members ai + 1 − ai is a constant 1 ≤ i ≤ k − 1 . For example, the sequence 5, 8, 11, 14, 17 is an arithmetic progression of length 5 with the common difference 3.

In this problem, you are requested to find the longest arithmetic progression which can be formed selecting some numbers from a given set of numbers. For example, if the given set of numbers is {0, 1, 3, 5, 6, 9}, you can form arithmetic progressions such as 0, 3, 6, 9 with the common difference 3, or 9, 5, 1 with the common difference -4. In this case, the progressions 0, 3, 6, 9 and 9, 6, 3, 0 are the longest.

Input

The input consists of a single test case of the following format.

n
v1 v2 ... vn

n is the number of elements of the set, which is an integer satisfying 2 ≤ n ≤ 5000 . Each vi(1 ≤ i ≤ n) is an element of the set,which is an integer satisfying 0 ≤ vi ≤ 109.vi's are all different, i.e.,vi ≠ vj if i ≠ j

Output

Output the length of the longest arithmetic progressions which can be formed selecting some numbers from the given set of numbers.

Sample Input

6
0 1 3 5 6 9

Sample Output

4
题意:求出序列从小到大排序之后能形成的最长等差数列的长度。
题解:dp,dp[i][j]表示i和j作为前两项时的数列长度,如果先枚举第一项(O(N)),然后第二项如果和第三项在第一项的同一侧的话就是(O(N^2)),整体复杂度是(O(N^3)),难以接受,而如果先枚举第二项,然后第一项和第三项在第二项两侧,根据a[j]+a[k]=2*a[i]可以将复杂度降为O(N^2)
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int a[5005],dp[5005][5005];
 7 int main(){
 8     int n;
 9     scanf("%d",&n);
10     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
11     for(int i=1;i<=n;i++){
12         for(int j=i;j<=n;j++){
13             if(i!=j)dp[i][j]=2;
14             else dp[i][j]=1;
15         }
16     }
17     sort(a+1,a+1+n);
18     int ans=2;
19     for(int i=n-1;i>=2;i--){
20         int j=i-1;
21         int k=i+1;
22         while(j>=1&&k<=n){
23             if(a[j]+a[k]==2*a[i]){dp[j][i]=dp[i][k]+1;ans=max(ans,dp[j][i]);k++;j--;}
24             else if(a[j]+a[k]<2*a[i]){k++;}
25             else {j--;}
26         }
27     }
28     cout<<ans<<endl;
29     return 0;
30 }
View Code

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/10611585.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树形动态规划(Tree DP)是一种解决树状结构问题的算法思想。它利用了树这种特殊的数据结构的性质进行求解,常用来解决树的最优路径、最大值、最小值等类型的问题。 在夜深人静的时候算法,我通常会采用以下步骤来完成树形dp的实现: 第一步是定义状态。我们首先需要确定问题的状态表示方式。对于树形dp来说,常用的状态表示方式是以节点为单位进行表示。我们可以定义dp[i]表示以节点i为根的子树的某种性质,比如最大路径和、最长路径长度等。 第二步是确定状态转移方程。根据问题的特点,我们需要找到状态之间的关系,从而确定状态转移方程。在树形dp中,转移方程常常与节点的子节点相关联。我们可以通过遍历节点的子节点,利用它们的状态来更新当前节点的状态,从而得到新的状态。 第三步是确定初始条件。在动态规划中,我们需要确定初始状态的值。对于树形dp来说,我们可以选择将叶节点作为初始状态,然后逐步向上更新,最终得到整棵树的最优解。 第四步是确定计算顺序。树形dp的计算通常是从根节点开始,自顶向下逐步计算,直到达到叶节点。因为树形dp的计算过程中需要利用到子节点的状态来更新当前节点的状态,所以必须按照计算顺序进行。 夜深人静时,算法树形dp是相对较复杂的算法,需要仔细思考问题的状态表示方式,转移方程以及初始条件。在实现过程中,可以采用递归的方式进行代码编,或者利用栈等数据结构进行迭代实现。 总的来说,夜深人静算法树形dp需要耐心和细心,经过思考和实践,才能顺利解决树状结构问题。但是,一旦理解并掌握了树形dp的思想和方法,就能够高效地解决各种树形结构问题,提升算法的效率和准确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值