Codeforces Round #697 (Div. 3) G. Strange Beauty
题目链接:https://codeforces.ml/problemset/problem/1475/G
题意:给定一个数组 ,问最少删去几个数组中的元素 使得数组中剩下的元素两两之间 其中一个数是另外一个数的因子
思路:考虑dp[i] 为不超过i的数的满足条件的最大值 那么每次转移 就可以只通过i的倍数来转移 即j+=i
最后的答案数 n-ans ,ans要遍历一遍dp,ans为数组中的一个满足条件集合的最大值
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=2e5+10; 4 const int mod=1e9+7; 5 #define ll long long 6 #define pb push_back 7 int cnt[maxn]; 8 int dp[maxn]; 9 10 11 int main() 12 { 13 ios::sync_with_stdio(0); 14 cin.tie(0); 15 int t; 16 cin>>t; 17 while(t--) 18 { 19 memset(cnt,0,sizeof(cnt)); 20 memset(dp,0,sizeof(dp)); 21 int n; 22 cin>>n; 23 for(int i=1;i<=n;i++) 24 { 25 int x; 26 cin>>x; 27 cnt[x]++; 28 } 29 for(int i=1;i<maxn;i++) 30 { 31 dp[i]+=cnt[i]; 32 for(int j=i+i;j<maxn;j+=i) 33 { 34 dp[j]=max(dp[j],dp[i]); 35 } 36 } 37 int ans=0; 38 for(int i=1;i<maxn;i++) 39 ans=max(ans,dp[i]); 40 cout<<n-ans<<'\n'; 41 } 42 43 44 45 46 47 48 }
时间复杂度 调和级数 o(nlogn)

浙公网安备 33010602011771号