Loading

【dp每日一题】CF566F. Clique in the Divisibility Graph

大意:

给出一个长度为n的排好序的数组,要求选出一个子数组,每一对数都是整除关系,问该数组长度最大是多少

数据范围:\(1\leq n\leq 1e^6,1\leq a_i\leq 1e^6\)

思路:

因为数据的长度和范围都是\(1e^6\),所以可以想到开辟一个\(pos\)数组,存储\(a_i\)的位置,这样就可以利用类似筛素数的方法进行转移,复杂度\(O(n*\log{n})\)

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n,a[N],pos[N],dp[N],maxn,res;
int main(){
    cin >> n;
    for (int i = 1; i <= n;i++){
        scanf("%d", &a[i]);
        pos[a[i]] = i;
        dp[i] = 1;
    }
    maxn = a[n];
    for (int i = 1; i <= n;i++){
        for (int j = a[i] * 2; j <= maxn;j+=a[i]){
            if(pos[j]==0){
                continue;
            }
            int x = pos[j];
            dp[x] = max(dp[i] + 1, dp[x]);
        }
    }
    for (int i = 1; i <= n;i++){
        res = max(res, dp[i]);
    }
    cout << res << endl;
    return 0;
}
posted @ 2020-12-02 09:33  dyhaohaoxuexi  阅读(86)  评论(0编辑  收藏  举报