Codeforces633D【暴力】

本文介绍了一种通过枚举前两个元素来寻找最长斐波那契序列的算法实现,利用了map和multiset等数据结构进行高效查找与判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:
斐波那契的长度应该很小,所以枚举前两个直接判,暴力。
没有仔细想过还需要标记一下初始两位相同的,因为也没试过map标记,1e6对存在是否可行(现在可行啦,以后大胆用 ————T4
multiset.lower_bound()没有注意到细节 multiset.end() ————T95

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;

const int Maxn = 1e5 + 10;
map<pair<int,int>,int>mp;
vector<int>a, b;
multiset<int>st;
int n, x;
bool jud(int pm,int pn){
    if(mp.find(make_pair(pm, pn)) != mp.end()) return false;
    mp[make_pair(pm, pn)] = 1;
    return true;
}

int get(int m){
    if((int)st.size() == 0) return 0;
    multiset<int>::iterator pos;
    pos = st.lower_bound(m);
    if(pos == st.end()) return false;
    if((*pos) == m){
        b.push_back(m);
        st.erase(pos);
        return 1;
    }
    return 0;
}

int main()
{
    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%d", &x);
        a.push_back(x);
        st.insert(x);
    }
    int pre1, pre2, pre3;
    int ans = 2, num;
    sort(a.begin(), a.end());
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i == j) continue;
            if(!jud(a[i], a[j])) continue;
            b.clear();
            pre1 = a[i];
            pre2 = a[j];
            pre3 = a[i] + a[j];
            get(pre1);
            get(pre2);
            num = 2;
            while(get(pre3)){
                num++;
                pre1 = pre2;
                pre2 = pre3;
                pre3 = pre1 + pre2;
            }
            for(int k=0;k<(int)b.size();k++)
                st.insert(b[k]);

            ans = ans > num ? ans : num;
        }
    }
    printf("%d\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值