G. Strange Beauty

G. Strange Beauty

解题思路:动态规划。设dp[i]表示以i为结束时可以有最长有多少个,然后再将后面符合条件的换成大值。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 2e5+10;
const ll mod = 20000023;
const double PI = 3.1415926535;
const double eps = 1e-4;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
inline string readstring(){
    string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){str+=s;s=getchar();}return str;
}
int random(int n){
    return (int)(rand()*rand())%n;
}

int a[N],n,dp[N];

void solve(){
    n = read();
    memset(dp,0,sizeof dp);
    memset(a,0,sizeof a);
    for(int i = 1;i <= n;i++){
        int x = read();
        a[x]++;
    }
    int ans = 0;
    for(int i = 1;i < N;i++){
        dp[i] += a[i];
        for(int j = i+i;j < N;j += i){
            dp[j] = max(dp[i],dp[j]);
        }
        ans = max(ans,dp[i]);
    }
    cout<<n-ans<<endl;
}
int main(){
    srand((unsigned)time(NULL));
    int t = read();
    while(t--){
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42868863/article/details/113186962