给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len)。
标签:bbs arc des util output submit ble 包括 style
Problem 4300. -- 绝世好题
n<=100000,ai<=2*10^9
贪心,用$f[i]$表示结尾数字第i位上为1的最长子序列长度。
1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String args[]) { 7 8 Scanner in = new Scanner(System.in); 9 10 int n = in.nextInt(); 11 int f[] = new int[35]; 12 13 Arrays.fill(f, 0); 14 15 for (int i = 1; i <= n; ++i) { 16 17 int num = in.nextInt(), tmp = 0; 18 19 for (int j = 0; j <= 30; ++j) 20 if (((num >> j) & 1) == 1) 21 tmp = Math.max(tmp, f[j]); 22 23 for (int j = 0; j <= 30; ++j) 24 if (((num >> j) & 1) == 1) 25 f[j] = Math.max(f[j], tmp + 1); 26 27 } 28 29 int ans = 0; 30 31 for (int i = 0; i <= 30; ++i) 32 ans = Math.max(ans, f[i]); 33 34 System.out.println(ans); 35 36 in.close(); 37 38 } 39 40 }
@Author: YouSiki
标签:bbs arc des util output submit ble 包括 style
原文地址:http://www.cnblogs.com/yousiki/p/6284874.html