zoukankan      html  css  js  c++  java
  • Arithmetic Sequence(dp)

    Arithmetic Sequence

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 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 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 nintegers separated by spaces, indicating the number sequenceA. 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.

    题解:等差数列最长长度;开个二维dp,记录下就好了;一维dp也能过。。。
    java超时;c过;
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int dp[2010][2010];
    int a[2010];
    int main(){
        int N;
            while(~scanf("%d", &N)){
                for(int i = 0; i < N; i++){
                    scanf("%d", a + i);
                }
                sort(a, a + N);
                int ans = 1; 
                for(int i = 0; i < N; i++){
                    for(int k = 0; k <= 2000; k++){
                            dp[i][k] = 0;
                    }
                }
                for(int i = 0; i < N; i++){
                    for(int j = 0; j < i; j++){
                        int d = a[i] - a[j];
                        dp[i][d] = dp[j][d] + 1;
                        ans = max(ans, dp[i][d] + 1);
                    }
                }
                printf("%d
    ", ans);
                }
        return 0;
    }

    java:

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class  ArithmeticSequence{
        static int[][] dp = new int[2010][2010];
        public static void main(String[] args){
            Scanner cin = new Scanner(System.in);
            int N;
            while(cin.hasNext()){
                N = cin.nextInt();
                int[] a = new int[N];
                for(int i = 0; i < a.length; i++){
                    a[i] = cin.nextInt();
                }
                Arrays.sort(a);
                int ans = 1; 
                for(int i = 0; i < N; i++){
                    for(int k = 0; k <= 2000; k++){
                            dp[i][k] = 0;
                    }
                }
                for(int i = 0; i < a.length; i++){
                    for(int j = 0; j < i; j++){
                        int d = a[i] - a[j];
                        dp[i][d] = dp[j][d] + 1;
                        ans = Math.max(ans, dp[i][d] + 1);
                    }
                }
                System.out.println(ans);
            }
        }
    }

     dp;

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int dp[2010];
    int a[2010];
    int main(){
        int N;
            while(~scanf("%d", &N)){
                for(int i = 0; i < N; i++){
                    scanf("%d", a + i);
                }
                sort(a, a + N);
                int ans = 1; 
                    for(int i = 0; i <= 2000; i++){
                        for(int k = 0; k <= 2000; k++){
                            dp[k] = 0;
                    }
                    for(int j = 0; j < N; j++){
                        if(a[j] >= i)dp[a[j]] = max(dp[a[j] - i] + 1, dp[a[j]]);
                        else dp[a[j]] = 1;
                            ans = max(ans, dp[a[j]]);
                    }
                }
                printf("%d
    ", ans);
                }
        return 0;
    }
  • 相关阅读:
    ref与out的区别(C#)
    用MS SQL Server 2008修改数据库表时提示“不允许保存更改”的解决方法
    测试的职责
    性能测试新手误区(三):用户数与压力
    JAVA + LR实现apache流媒体的性能测试(LR部分)
    性能测试新手误区(二):为什么我模拟的百万测试数据是无效的
    JAVA + LR实现apache流媒体的性能测试(JAVA部分)
    性能测试新手误区(六):性能监控
    性能测试新手误区(五):这是性能问题么
    性能测试新手误区(四):一切来自录制
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5502651.html
Copyright © 2011-2022 走看看