/*
*题目大意:
*        给定一个长度最多为1000的序列,其中数的范围为-1000000000到1000000000,然后
*        求其中最长的等差数列(可以乱序);
*解题思路:
*        先对序列排序,方便后面的处理,减少复杂度。然后枚举这个序列,再枚举可能的公差,
*        然后再用红黑树之类的具有O(logn)性质的数据结构检查出满足的等差数列。以这样的方式
*        去求肯定会TLE的。最后还要分析:因为等差数列,其实当一串序列的最长等差数列为2,而
*        数又不大于1000000000,这样的序列最多也才30个左右,而题目给定的长度为1000,这就证明
*        会有很多子问题。所以,只需要以公差为记录,开一个标志数组来标志去重即可。
*/
View Code
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <iomanip>
#include <sstream>

#define abs(x) ((x)>0?(x):-(x))
#define sqr(x) ((x)*(x))

#define all(x) x.begin(), x.end()
#define rall(v) v.rbegin(),v.rend()

#define ll long long
#define ull unsigned long long

#define FOR(i,a,b)        for(int i=(a); i<(b);i++)
#define FF(i,a)            for(int i=0; i<(a);i++)
#define FFD(i,a)        for(int i=(a)-1; i>=0;i--)
#define CC(m,what)        memset(m,what,sizeof(m))
#define SZ(a)            ((int)a.size())
#define viewPP(a,n,m)    {puts("---");FF(i,n){FF(j,m) cout<<a[i][j] <<' ';puts("");}}
#define viewP(a, n)     {FF(i, n) {cout<<a[i]<<" ";} puts("");}

#define read            freopen("in.txt","r",stdin)
#define write            freopen("out.txt","w",stdout)

const double eps = 1e-11;
const int inf = 0x7fffffff;
const int hinf = 0x3f3f3f3f;
const double pi = 3.1415926535897932;

int dx[] = {-1, 0, 1, 0};//up Right down Left
int dy[] = {0,  1, 0, -1};

using namespace std;
const int maxn = 1005;

int arr[maxn];
map<int, int> vst;
multimap<int, int> num;

int main(void) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    int n;
    while(scanf("%d", &n) == 1) {
        num.clear(), vst.clear();
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            num.insert(make_pair(arr[i], 1));
        }
        
        sort(arr, arr+n);
        int Max = -inf;
        FF(i, n) {
            for(int j = i+1; j < n; j++) {
                int cut = arr[j] - arr[i];
                int ct = 2;
                if(!cut)
                    continue;
                if(vst.count(cut) != 0 && vst[cut] >= arr[j]) {
                    continue;
                }
                else {
                    int nxt = arr[j];
                    while(1) {
                        nxt += cut;
                        if(num.find(nxt) != num.end())
                            ct++;
                        else break;
                    };
                    vst[cut] = nxt-cut;
                    if(ct > Max) Max = ct;
                }
            }
        }
        FF(i, n) {
            int ct = num.count(arr[i]);
            //cout << arr[i] << " " << ct << endl;
            if(ct > Max)
                Max = ct;
        }
        printf("%d\n", Max);
    }
    return 0;
}

 

posted on 2013-04-17 18:59  cchun  阅读(507)  评论(0编辑  收藏  举报