BZOJ 4300: 绝世好题 二进制

   这是道动规的题,还好。昨晚在睡前想了一下,结合了之前解题的经验,首先有关二进制运算的很多都是独立的,所以我们可以把这道题弄成这样,设 a[i] 为 如果 j 的二进制第 i 位为一所能取得的最大长度,然后在 j 的所有二进制为 1 的数中取 a[i] 最大的,并更新其他的二进制位。就这样了,加油啊!还挺顺利的。

这个跑了48毫秒

 1 #include<cstdio>
 2 #include<iostream>
 3 #define rep(i,j,k) for(int i = j; i <= k; i++)
 4 #define maxn 100005
 5 using namespace std;
 6 int a[35] = {0};
 7 
 8 int read()
 9 {
10     int s = 0, t = 1; char c = getchar();
11     while( !isdigit(c) ){
12         if( c == '-' )t = -1; c = getchar();
13     }
14     while( isdigit(c) ){
15         s = s * 10 + c - '0'; c = getchar();
16     }
17     return s * t;
18 }
19 
20 int main()
21 {
22     int n = read();
23     rep(i,1,n){
24         int x = read();
25         int y = x, tot = 0, maxl = 0;
26         while( y ){
27             if( y&1 ) {
28                 if( a[tot] + 1 > maxl ) maxl = a[tot] + 1;
29             }
30             y >>= 1, tot++;
31         }
32         y = x, tot = 0;
33         while( y ){
34             if( y&1 ){
35                 a[tot] = maxl;
36             }
37             y >>= 1, tot++;
38         }
39     }
40     int ans = 0;
41     rep(i,0,31) if( a[i] > ans ) ans = a[i];
42     cout<<ans<<endl;
43     return 0; 
44 }

 

4300: 绝世好题

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 654  Solved: 350
[Submit][Status][Discuss]

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

 

对于100%的数据,1<=n<=100000,ai<=10^9。


 

 

Source

posted on 2015-12-28 13:30  83131  阅读(208)  评论(0编辑  收藏  举报

导航