1921: B
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 249 Solved: 22
SubmitStatusWeb BoardDescription
給定一個長度爲n的數字序列a,從中選取一個長爲m的子序列b滿足 b[i]&b[i-1] ! = 0 (2 < = i < = m)
求最大的m。
Input
第一行輸入一個整數t,代表有t組測試數據。
每組數據第一行輸入一個整數n代表a序列的長度,接下來一行輸入n個正整數表示ai(0 < i < = n)。
1<=t<=20,0<=n<=100000,0<=ai<=1e9。
Output
一個整數表示最大的m。
Sample Input
1
3
1 1 1
Sample Output
3HINT
Source
haut
SubmitStatusWeb Board
DP題方程 mark = max ( dp[i][j-1]+1 ,mark)
更新或者不更新的問題 用一維數組優化了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int x[40];//存放以該二進制位結尾爲1的最多的子序列個數
int n;
int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d",&n);
int ans=0;
int a;int mark=0;
memset(x,0,sizeof(x));
for(int i = 1; i <= n; i++){
scanf("%d",&a); int k=a;
int j=1; //位數
mark=0;
while(a){
if(a&1){
mark=max(mark,x[j]+1);//這個數的該點爲1就要更新最長的長度
}
a>>=1;
j++;
}
j=1;
while(k){
if(k&1)
x[j]=mark;//讓所有在這個位爲1的也都更新
j++; k>>=1;
}
ans=max(mark,ans);//更新最長子序列長度
}
printf("%d\n",ans);
}
return 0;
}