Codeforces Round #213 (Div. 2) B. The Fibonacci Segment

OpenSoucre發表於2013-11-24
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0 ; i < n; ++ i)
        cin >> a[i];
    int maxlen = 0;
    if(n == 1) maxlen = 1;
    else if(n == 2) maxlen = 2;
    else{
        maxlen = 2;
        int l = 0, r =1;
        for(int i = 2; i < n; ++ i){
            if(a[i] == a[i-1] + a[i-2]){
                r = i;
                if(maxlen < r-l+1){
                    maxlen = r-l+1;
                }
            }
            else{
                l = i-1;
            }
        }
    }
    cout<<maxlen<<endl;
    return 0;
}

 

相關文章