codeforces 566F - Clique in the Divisibility Graph (dp)
阿新 • • 發佈:2020-10-25
題目連結: 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; }