C/C++教程

Codeforces Round #697 (Div. 3)

本文主要是介绍Codeforces Round #697 (Div. 3),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

比赛链接

Codeforces Round #697 (Div. 3)

G. Strange Beauty

题目大意: 有 \(n\) 个数,从中挑选一个最大的子集,使得集合中任意两个不同的数 \(x, y\) ,有 \(x \mid y\) 或 \(y \mid x\)

输入格式

The first line contains one integer \(t(1 \leq t \leq 10)\) - the number of test cases. Then \(t\) test cases follow.
The first line of each test case contains one integer \(n\left(1 \leq n \leq 2 \cdot 10^{5}\right)-\) the length of the array \(a\)
The second line of each test case contains \(n\) numbers \(a_{1}, a_{2}, \ldots, a_{n}\left(1 \leq a_{i} \leq 2 \cdot 10^{5}\right)-\) elements of the array \(a\).

输出格式

For each test case output one integer - the minimum number of elements that must be removed to make the array \(a\) beautiful.

解题思路

dp

不难发现,值域较小,可将所有出现的值作为桶,统计每个数出现的次数

  • 状态表示:\(f[i]\) 表示最大值为 \(i\) 的满足条件的最长上升子序列的长度

  • 状态计算:\(f[i]+=a[i]\)
    分析:值 \(i\) 作为最大值时,应加上值为 \(i\) 出现的次数,注意这里不是 \(f[i]=a[i]\),因为前面可能会有 \(i\) 的约数也作为最长长度的贡献

  • 时间复杂度:\(O(nlogn)\)

代码

// Problem: G. Strange Beauty
// Contest: Codeforces - Codeforces Round #697 (Div. 3)
// URL: https://codeforces.com/contest/1475/problem/G
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e5+5;
int t,n,f[N],a[N];
int main()
{
    for(cin>>t;t;t--)
    {
    	cin>>n;
    	memset(f,0,sizeof f);
    	memset(a,0,sizeof a);
    	for(int i=1;i<=n;i++)
    	{
    		int x;
    		cin>>x;
    		a[x]++;
    	}
    	int res=0;
    	for(int i=1;i<N;i++)
    	{
    		f[i]+=a[i];
    		res=max(res,f[i]);
    		for(int j=i;j<N;j+=i)f[j]=max(f[j],f[i]);
    	}
    	cout<<n-res<<'\n';
    }
    return 0;
}
这篇关于Codeforces Round #697 (Div. 3)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!