码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 4300 绝世好题——DP

时间:2018-11-01 11:50:48      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:href   style   getc   记录   http   print   ring   bsp   geo   

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

考虑 dp[ i ] 能从哪些 j 转移过来,就是那些 a[ j ] & a[ i ] != 0 的,也就是有至少1位公共的1;所以在30位上记录这一位是1的那些 a[ ] 中的 dp[ ] 最大值,就能转移了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5,M=35;
int n,a[N],bin[M],c[M],lm,ans;
int rdn()
{
  int ret=0;bool fx=1;char ch=getchar();
  while(ch>9||ch<0){if(ch==-)fx=0;ch=getchar();}
  while(ch>=0&&ch<=9) ret=(ret<<3)+(ret<<1)+ch-0,ch=getchar();
  return fx?ret:-ret;
}
int main()
{
  int tp=0;
  n=rdn();for(int i=1;i<=n;i++)a[i]=rdn(),tp=max(tp,a[i]);
  while(tp)lm++,tp>>=1; lm--;
  bin[0]=1;for(int i=1;i<=lm;i++)bin[i]=bin[i-1]<<1;
  for(int i=1;i<=n;i++)
    {
      int d=0;
      for(int j=0;j<=lm&&bin[j]<=a[i];j++)
    if(a[i]&bin[j])d=max(d,c[j]);
      d++;
      for(int j=0;j<=lm&&bin[j]<=a[i];j++)
    if(a[i]&bin[j])c[j]=max(c[j],d);
    }
  for(int j=0;j<=lm;j++)ans=max(ans,c[j]);
  printf("%d\n",ans);
  return 0;
}

 

bzoj 4300 绝世好题——DP

标签:href   style   getc   记录   http   print   ring   bsp   geo   

原文地址:https://www.cnblogs.com/Narh/p/9887220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!