zoukankan      html  css  js  c++  java
  • 华中农业大学第四届程序设计大赛网络同步赛 J

    Problem J: Arithmetic Sequence

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 1766  Solved: 299
    [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.

    题意: 给你一个序列 输出 序列中最长等差数列的长度

    题解:

    1.暴力 sort排序一下 然后暴力枚举每一个步长 坑点(注意相同的数  也就是等差可以为0)

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<map>
     5 #include<queue>
     6 #include<stack>
     7 using namespace std;
     8 int n;
     9 int mp[2005];
    10 int exm;
    11 int ans;
    12 int q;
    13 int maxn;
    14 int main()
    15 {
    16     while(scanf("%d",&n)!=EOF)
    17     {
    18         ans=-1;
    19         maxn=-1;
    20         for(int i=1;i<=2000;i++)
    21          mp[i]=0;
    22         for(int i=0;i<n;i++)
    23         {
    24           scanf("%d",&exm);
    25           mp[exm]++;
    26           if(ans<mp[exm])
    27           ans=mp[exm];
    28           if(maxn<exm)
    29           maxn=exm;
    30         }
    31          for(int i=1;i<=maxn;i++)
    32          {
    33             if(mp[i])
    34             {
    35                for(int d=1;d<=maxn;d++)
    36               {
    37                 int gg=i+d;
    38                 q=1;
    39                  while(gg)
    40                {
    41                  if(gg>maxn)
    42                    break;
    43                  if(mp[gg])
    44                    q++;
    45                  else
    46                    break;
    47                   gg=gg+d;    
    48                }
    49                  if(q>ans)
    50                  ans=q;
    51                  if(gg>maxn)
    52                  break;
    53               }
    54             }
    55          }  
    56          cout<<ans<<endl;   
    57     }
    58     return 0;
    59  } 
    

    2.dp处理 (yan代码) dp[i][j] 表示以i为结尾 j为等差的方法数目

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 #include<vector>
     7 #include<map>
     8 #pragma comment(linker, "/STACK:102400000,102400000")
     9 using namespace std;
    10 const int N = 2000+20, M = 1e6+10, mod = 1e9+7,inf = 1e9;
    11 typedef long long ll;
    12  
    13 int n,a[N];
    14 int dp[N][N];
    15 int main() {
    16     while(scanf("%d",&n)!=EOF) {
    17         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    18         sort(a+1,a+n+1);
    19         for(int j=1;j<=n;j++)
    20         for(int i=0;i<=2000;i++) dp[j][i] = 1;
    21         for(int i=2;i<=n;i++) {
    22             for(int j=1;j<i;j++) {
    23                 dp[i][a[i]-a[j]] =  max(dp[j][a[i]-a[j]]+1,dp[i][a[i]-a[j]]);
    24             }
    25         }
    26         int ans = 0;
    27         for(int j=1;j<=n;j++)
    28         for(int i=0;i<=2000;i++) {
    29             ans = max(ans,dp[j][i]);
    30          }
    31         printf("%d
    ",ans);
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    十进制转任意进制
    整型与字符串转换
    最长递增子序列(动态规划)
    睡眠理发师问题
    区间最值问题(RMQ)
    分解质因子
    数字统计
    After all, tomorrow is another day.
    【USB电平】电脑USB电平
    【有源滤波】滤波基础知识
  • 原文地址:https://www.cnblogs.com/hsd-/p/5495268.html
Copyright © 2011-2022 走看看