bzoj 4300 绝世好题 —— 思路

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4300

记录一下 mx[j] 表示以第 j 位上是1的元素结尾的子序列长度最大值,转移即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const xn=1e5+5,xm=32;
int n,a[xn],mx[xm],bin[xm];
int rd()
{
  int ret=0,f=1; char ch=getchar();
  while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
  while(ch>='0'&&ch<='9')ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar();
  return f?ret:-ret;
}
void init()
{
  bin[0]=1;
  for(int i=1;i<=31;i++)bin[i]=(bin[i-1]<<1);
}
int main()
{
  n=rd(); int ans=0; init();
  for(int i=1,x;i<=n;i++)
    {
      x=rd(); int t=0;
      for(int j=0;j<=31;j++)
    if(x&bin[j])t=max(t,mx[j]+1);
      ans=max(ans,t);
      for(int j=0;j<=31;j++)
    if(x&bin[j])mx[j]=max(mx[j],t);
    }
  printf("%d\n",ans);
  return 0;
}

 

posted @ 2018-11-01 08:29  Zinn  阅读(141)  评论(0编辑  收藏  举报