Codeforces Round #697 (Div. 3) G. Strange Beauty

链接

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)(ajai)

代码

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值