zoukankan      html  css  js  c++  java
  • 北邮校赛 I. Beautiful Array(DP)

    时间限制 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 L1", 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(1T5), which indicates the number of test cases.
    For each test case:

    • The first line contains two integers n,L(1n50000,2L1000000);
    • The second line contains n integers a1,a2,...,an(2ai1000000).

    输出格式

    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+5;
    const double eps = 1e-8;
    int n,sum[N],m,cnt,k;
    int lazy[N],a[N];
    int p[N],dp[N];
    void solve(int x){
        for(int i=2;i*i<=a[x];i++){
            if(a[x]%i==0){
                if(i>=m&&p[i]!=0)dp[x]=max(dp[x],dp[p[i]]+1);
                if(a[x]/i>=m&&p[a[x]/i]!=0)dp[x]=max(dp[x],dp[p[a[x]/i]]+1);
                p[i]=x;
                p[a[x]/i]=x;
            }
        }
        if(p[a[x]]!=0&&a[x]>=m)dp[x]=max(dp[x],dp[p[a[x]]]+1);
        p[a[x]]=x;
    }
    int main() {
        int T,x,y,xx,yy;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&m);
            met(p,0);
            bool ok=false;
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
                if(a[i]>=m)ok=true;
                dp[i]=0;
            }
            for(int i=1;i<=n;i++){
                if(a[i]<m)continue;
                else dp[i]=1;
                solve(i);
            }
            int ans=0;
            for(int i=1;i<=n;i++){
                ans=max(ans,dp[i]);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    自编码变分贝叶斯(转载)
    加载Pytorch中的预训练模型及部分结构的导入
    pytorch读入图片并显示np.transpose(np_image, [1, 2, 0])
    高斯分布、多维高斯分布、各向同性的高斯分布及多元高斯分布之间的KL散度
    pytorch中的上采样(上采样,转置卷积,上池化,PixelShuffle)
    卷积算法动画演示
    PL/SQL编程基础
    PL/SQL Dev 调试
    PL/SQL Dev的安装与连接远程数据库
    Bean Form DTO VO Entity
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6789029.html
Copyright © 2011-2022 走看看