Huazhong Agricultural University The fourth session of the Program Design Competition network synchronization game J

Source: Internet
Author: User

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 is 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 is positive and not more than 2000. The input would end by EOF.

Output

For each test case, output the maximum as the answer on one line .

Sample Input
51 3 5 7 1084 2 7 11 3 1 9 5
Sample Output
46
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 are 6.

Test instructions: gives you the length of the longest arithmetic progression in a sequence output sequence

Exercises

1. Violence sort and then violence enumerates each step of the pit point (note that the same number is equal to 0)

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <map>5#include <queue>6#include <stack>7 using namespacestd;8 intN;9 intmp[2005];Ten intEXM; One intans; A intQ; - intMAXN; - intMain () the { -      while(SCANF ("%d", &n)! =EOF) -     { -ans=-1; +maxn=-1; -          for(intI=1; i<= -; i++) +mp[i]=0; A          for(intI=0; i<n;i++) at         { -scanf"%d",&EXM); -mp[exm]++; -           if(ans<MP[EXM]) -ans=MP[EXM]; -           if(maxn<EXM) inmaxn=EXM; -         } to           for(intI=1; i<=maxn;i++) +          { -             if(Mp[i]) the             { *                 for(intD=1;d <=maxn;d++) $               {Panax Notoginseng                 intgg=i+D; -q=1; the                   while(GG) +                { A                  if(gg>MAXN) the                     Break; +                  if(Mp[gg]) -q++; $                  Else $                     Break; -gg=gg+D;  -                } the                  if(q>ans) -ans=Q;Wuyi                  if(gg>MAXN) the                   Break; -               } Wu             } -          }   Aboutcout<<ans<<Endl;  $     } -     return 0; -  } 

2.DP processing (Yan code) dp[i][j] indicates the number of methods with the end of I as J

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <vector>7#include <map>8 #pragmaComment (linker, "/stack:102400000,102400000")9 using namespacestd;Ten Const intN = -+ -, M = 1e6+Ten, mod = 1e9+7, INF =1e9; OnetypedefLong Longll; A   - intN,a[n]; - intDp[n][n]; the intMain () { -      while(SCANF ("%d", &n)! =EOF) { -          for(intI=1; i<=n;i++) scanf ("%d",&a[i]); -Sort (A +1, a+n+1); +          for(intj=1; j<=n;j++) -          for(intI=0; i<= -; i++) Dp[j][i] =1; +          for(intI=2; i<=n;i++) { A              for(intj=1; j<i;j++) { atDP[I][A[I]-A[J]] = max (dp[j][a[i]-a[j]]+1, dp[i][a[i]-A[j]]); -             } -         } -         intAns =0; -          for(intj=1; j<=n;j++) -          for(intI=0; i<= -; i++) { inAns =Max (ans,dp[j][i]); -          } toprintf"%d\n", ans); +     } -     return 0; the}

Huazhong Agricultural University fourth session of the Program Design Competition network synchronization game J

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.