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;
    }
  • 相关阅读:
    终于等到你---订餐系统之负载均衡(nginx+memcached+ftp上传图片+iis)
    订餐系统之同步饿了么商家订单
    订餐系统之同步口碑外卖商家菜单与点点送订单
    基于SuperSocket的IIS主动推送消息给android客户端
    基于mina框架的GPS设备与服务器之间的交互
    订餐系统之微信支付,踩了官方demo的坑
    订餐系统之自动确认淘点点订单
    订餐系统之Excel批量导入
    移除首页->重回首页
    订餐系统之获取淘宝外卖订单
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6789029.html
Copyright © 2011-2022 走看看