首页 > 技术文章 > codeforces 566F - Clique in the Divisibility Graph (dp)

tuchen 2020-10-25 17:23 原文

题目链接: https://codeforces.com/problemset/problem/566/F

最大团即找出能相互整除的最长子序列长度
\(dp[i]\)表示以 i 结尾的序列能组成的最长子序列长度
采用刷表法,枚举\(a[i]\)的倍数更新
时间复杂度:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 1000100;

int n;
int a[maxn],dp[maxn],g[maxn];

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	n = read();
	int mx = 0;
	for(int i=1;i<=n;++i) a[i] = read(),mx = max(mx,a[i]);
	
	sort(a+1,a+1+n);
	
	for(int i=1;i<=n;++i){
		for(int j=2*a[i];j<=mx;j+=a[i]){
			dp[j] = max(dp[j],dp[a[i]]+1);
		}
		++dp[a[i]];
	}
//	for(int i=1;i<=n;++i) printf("%d\n",dp[a[i]]);
	
	int ans = 0;
	for(int i=1;i<=n;++i){
		ans = max(ans,dp[a[i]]);
	}
	
	printf("%d\n",ans);
	
	return 0;
}

推荐阅读