zoukankan      html  css  js  c++  java
  • BZOJ 4300: 绝世好题 动态规划

    4300: 绝世好题

    题目连接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=4300

    Description

    给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len)。

    Input

    输入文件共2行。
    第一行包括一个整数n。
    第二行包括n个整数,第i个整数表示ai。

    Output

    输出文件共一行。
    包括一个整数,表示子序列bi的最长长度。

    Sample Input

    3

    1 2 3

    Sample Output

    2

    Hint

    n<=100000,ai<=2*10^9

    题意

    题解

    因为两个&为0的话,必须得所有位置都至少有一个为0

    那么这一位,两个数都是1的话,就不会为0了

    令dp[i]表示第i位为1的最长长度,然后转移就好了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    int dp[50],n,a[maxn],tmp,ans;
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++){
            tmp = 0;
            for(int j=0;j<=30;j++)
                if(a[i]&(1<<j))
                    tmp=max(tmp,dp[j]+1);
            for(int j=0;j<=30;j++)
                if(a[i]&(1<<j))
                    dp[j]=tmp;
            ans=max(ans,tmp);
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    CCCC练习即感
    1003 我能通过
    录制开讲啦杂感
    OOP第三次上机
    关于C++随机函数
    蓝桥杯杂感。
    CF502C The Phone Number
    It's a secret
    2017-06-22
    2017-05-12
  • 原文地址:https://www.cnblogs.com/qscqesze/p/6042216.html
Copyright © 2011-2022 走看看