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

bzoj4300绝世好题

时间:2016-08-15 22:08:55      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

bzoj4300绝世好题

题意:

给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0。n≤100000,ai≤10^9。

题解:

用f[i]表示当前二进制i为1的最长子序列长度。每次求所有((1<<i)&bi)==1的f[i]最大值max,将所有((1<<i)&bi)==1的f[i]变为max+1。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 100010
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
11     while(ch>=0&&ch<=9)x=x*10+ch-0,ch=getchar();
12     return f*x;
13 }
14 int n,a,f[40],tot;
15 int main(){
16     n=read();
17     inc(i,1,n){
18         a=read(); tot=0;
19         inc(j,0,30)if(a&(1<<j))tot=max(tot,f[j]); inc(j,0,30)if(a&(1<<j))f[j]=tot+1;
20     }
21     tot=0; inc(i,0,30)tot=max(tot,f[i]); printf("%d",tot); return 0;
22 }

 

20160812

bzoj4300绝世好题

标签:

原文地址:http://www.cnblogs.com/YuanZiming/p/5774474.html

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