北邮校赛 I. Beautiful Array(DP)

时间:2021-11-30 00:39:48

I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync

时间限制 1000 ms 内存限制 65536 KB

题目描述

We call an array "beautiful array of level L", when all its adjacent number pairs have common factor greater than or equal to L. Obviously, a "beautiful array of level L" is also a "beautiful array of level L−1", and so on. Now you are required to remove some elements from a given array of length n, in order to make it become "beautiful array of level L". Try to figure out the maximum length of the new array.

输入格式

The first line contains only one integer T(1≤T≤5), which indicates the number of test cases.
For each test case:

  • The first line contains two integers n,L(1≤n≤50000,2≤L≤1000000);
  • The second line contains n integers a1,a2,...,an(2≤ai≤1000000).

输出格式

For each test case, output a number in a single line, indicating the maximum number of elements retained in the array.

输入样例

1
5 6
7 16 9 24 6

输出样例

3
【题意】给你一个数组,然你删除尽量少的数,使得剩下的数,相邻的两个数的GCD>=K,数的相对位置不变,问你剩下的数最多有几个。
【分析】我们可以从左往右遍历,对于每个数,我们更新一下它的所有因子最近出现的位置。然后对于当前数,我们找到它的所有因子
然后查找这个因子最近出现的位置,然后从那个地方DP过来,然后更新这个因子的位置。
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define met(a,b) memset(a,b,sizeof a)
#define inf 10000000
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 1e6+;
const double eps = 1e-;
int n,sum[N],m,cnt,k;
int lazy[N],a[N];
int p[N],dp[N];
void solve(int x){
for(int i=;i*i<=a[x];i++){
if(a[x]%i==){
if(i>=m&&p[i]!=)dp[x]=max(dp[x],dp[p[i]]+);
if(a[x]/i>=m&&p[a[x]/i]!=)dp[x]=max(dp[x],dp[p[a[x]/i]]+);
p[i]=x;
p[a[x]/i]=x;
}
}
if(p[a[x]]!=&&a[x]>=m)dp[x]=max(dp[x],dp[p[a[x]]]+);
p[a[x]]=x;
}
int main() {
int T,x,y,xx,yy;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
met(p,);
bool ok=false;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>=m)ok=true;
dp[i]=;
}
for(int i=;i<=n;i++){
if(a[i]<m)continue;
else dp[i]=;
solve(i);
}
int ans=;
for(int i=;i<=n;i++){
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}