链接
https://codeforces.com/contest/1475/problem/G
题意
长度为 n n n 的数组 a a a,删去一些数使数组内任意两个数的较大值为较小值的倍数,求最少删去多少数。
思路
记 t o t i tot_i toti 为 i i i 在数组 a a a 中的个数,
将数组 a a a 排序去重后,
记 d p i dp_i dpi 为以第 i i i 大的数为最大数的满足要求的数组的长度:
d p i = max ( d p j + t o t a i ) ( a j ∣ a i ) dp_i=\max(dp_j+tot_{a_i})(a_j \mid a_i) dpi=max(dpj+totai)(aj∣ai)
代码
#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long double LD;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const int N=2e5+5;
int a[N],dp[N],tot[N],pos[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--) {
int n;
cin>>n;
VI a(n);
memset(tot,0,sizeof tot);
memset(pos,-1,sizeof pos);
for(int i=0;i<n;i++) cin>>a[i],++tot[a[i]];
sort(ALL(a));
a.resize(unique(ALL(a))-a.begin());
for(int i=0;i<SZ(a);i++) pos[a[i]]=i;
for(int i=0;i<SZ(a);i++) dp[i]=0;
for(int i=0;i<SZ(a);i++) {
int t=0;
for(int j=1;j*j<=a[i];j++)
if(a[i]%j==0) {
if(pos[j]!=-1) t=max(t,dp[pos[j]]+tot[a[i]]);
if(pos[a[i]/j]!=-1) t=max(t,dp[pos[a[i]/j]]+tot[a[i]]);
}
dp[i]=t;
}
int mx=0;
for(int i=0;i<SZ(a);i++) mx=max(mx,dp[i]);
cout<<n-mx<<'\n';
}
return 0;
}