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;
    }
  • 相关阅读:
    我的编辑辅助工具
    一点BREW开发的资料
    XCode 4.2.1 (iOS SDK 5.0) for Lion
    C Style SEH handling Example
    OWC中双刻度图表的实现(转自网络)
    浅谈MAXIMO项目实施(转)
    GridView 72般绝技
    MAXIMO表学习总结(转)
    一SQL语句.
    网站根目录下的 cert/ 目录中有bazs.cert文件,可是http://网站地址/cert/bazs.cert 就是提示HTTP 404 的解决方法
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6789029.html
Copyright © 2011-2022 走看看