public class SequenceOr { public static void main(String[] args) { try (Scanner in = new Scanner(System.in)) { while (in.hasNext()) { int n = in.nextInt(); int[] nums = new int[n]; int maxOr = 0; for (int i = 0; i < n; i++) { int num = in.nextInt(); nums[i] = num; maxOr |= num; } System.out.println(min(nums, n, maxOr)); } } } private static int min(int[] nums, int n, int maxOr) { // dp[i][j] 表示从 arr[i] 到 arr[j] 的&nbs***bsp; dp[i][j] = dp[i][j-1]|arr[j] // 降了一维,二维溢出 int[] dp = new int[n]; int minLen = Integer.MAX_VALUE; for (int step = 0; step < n; step++) { for (int i = n - 1 - step; i >= 0; i--) { int j = i + step; int pre = j - 1 < i ? 0 : dp[j - 1]; dp[j] = pre | nums[j]; if (dp[j] >= maxOr) { minLen = Math.min(minLen, j - i + 1); return minLen; } } } return minLen; } }
全部评论
(0) 回帖