ZCMU2993 Fibonacci-ish(排序+二分)

本文介绍了一种算法,旨在通过重新排列给定整数序列,使其具有尽可能长的斐波那契前缀。首先对序列进行排序,然后选择两个数作为斐波那契序列的起始值,使用二分查找在原序列中寻找后续的斐波那契数,确保已选数不再重复使用。此方法能找出最长的斐波那契似序列。

Description

Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if

  1. the sequence consists of at least two elements
  2. f0 and f1 are arbitrary
  3. fn+2=fn+1+fn for all n≥0.

You are given some sequence of integers a1,a2,...,an. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence.

Input

 The first line of the input contains a single integer n (2≤n≤1000)− the length of the sequence ai.

The second line contains n integers a1,a2,...,an (|ai|≤109).

Output

 Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.

Sample Input

3

1 2 -1

Sample Output

3

HINT

 In the first sample, if we rearrange elements of the sequence as -1, 2, 1, the whole sequence ai would be Fibonacci-ish.

思路

斐波那契数列的第一项、第二项影响整个数列,如何选择第一二两项决定数列的长度。

斐波那契数列是一个非递减数列,不妨现将所给的n个数排序。然后从中选择两个数作为第一、二项,然后根据其特性用二分在原序列中查找F(n)(F(n)=F(n-1)+F(n-2))...F(n+1)...F(n+2)...

注意,已选择过的数不能再次选择

代码

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long LL;
#define MAX 1005
LL Array[MAX],Mark[MAX],x,y,n;
int Binary_search(int v,int l,int r)
{
    int left=l,right=r,mid,temp;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(Array[mid]==v)
        {
            if(Mark[mid]==1)//若Array[mid]已被选择,则尝试在剩余的数中找与它大小相同的数
            {
                temp=Binary_search(v,left,mid-1);
                if(temp!=-1) return temp;
                temp=Binary_search(v,mid+1,right);
                if(temp!=-1) return temp;
                return -1;
            }
            else
                return mid;
        }
        else if(v>Array[mid])
            left=mid+1;
        else if(v<Array[mid])
            right=mid-1;
    }
    return -1;
}
int solve()
{
    int re=2,flag=1;
    while(flag)
    {
        int pos=Binary_search(x+y,0,n-1);
        if(pos!=-1&&Array[pos]==x+y)
        {
            x=y;
            y=Array[pos];
            Mark[pos]=1;
            re+=1;
        }
        else
            flag=0;
    }
    return re;
}
int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
            scanf("%lld",&Array[i]);
        sort(Array,Array+n);
        int ans=2;
        memset(Mark,0,sizeof(Mark));
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i!=j)
                {
                    x=Array[i]; y=Array[j];
                    Mark[i]=Mark[j]=1;
                    int Return=solve();
                    memset(Mark,0,sizeof(Mark));
                    if(Return>ans) ans=Return;
                }
                if(ans==n) break;
            }
            if(ans==n) break;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值