1. 程式人生 > 實用技巧 >B - Arithmetic Progressions(待解決

B - Arithmetic Progressions(待解決

https://vjudge.net/contest/394066#problem/B

https://vjudge.net/problem/Gym-102082B/origin

資料量大不能用陣列查

用了二分,在test57的地方OT了,應該是演算法有問題(待補題

附未通過程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;




ll a[5005] = {0};
bool find(ll key ,ll n)
{
    int left = 1,right = n;
    while(left <= right)
    {
        int mid = (left + right) / 2;
        if(a[mid] == key)
            return 1;
        else if(a[mid] > key)
            right = mid - 1;
        else
            left = mid + 1;
    }
    return 0;
}


int main()
{
    ll n = 0;
    cin >> n;
    for(ll i = 0;i<n;i++)
    {
        cin >> a[i];
    }
    sort(a,a+n);

    ll max = 0;
    for(ll i = 0;i<n-1;i++)
    {
        for(ll j = i+1;j<n;j++)
        {
            ll counter = 0;
            ll x = a[i] > a[j] ? a[i] : a[j];
            ll y = a[i] < a[j] ? a[i] : a[j];
            ll d = x - y;
            ll temp = x + d;
            while(find(temp,n) && temp <= 1e9)
            {
                temp += d;
                counter += 1;
            }
            //cout<<x << " "<<y<<" "<<counter<<endl;
            if(counter > max)max = counter;
        }
    }
    cout << max+2;
    return 0;
}