CF 184 C.Ivan and Powers of Two
https://codeforces.com/problemset/problem/305/C
给你一个有序数组 a,长度不超过 1e5,0<=a[i]<=2e9。
请你求出 2^a[0]+2^a[1]+…+2^a[n-1] 的二进制中的 0 的个数。(^ 表示幂)
输入 a=[0,1,1,1]
输出 0
解释 和为 1+2+2+2=7 (111)
输入 a=[3]
输出 3
解释 和为 8 (1000)
思路:
使用一个哈希表遍历一遍数组统计哪些位为1,并找到最高位
static final int N = 100010;static int[] a = new int[N];static int n;static void solve() {n = ni();for (int i = 0; i < n; i++)a[i] = ni();Set<Integer> set = new HashSet<>();long res = 0;for (int i = 0; i < n; i++) {int x = a[i];while (set.contains(x)) {set.remove(x);x++;}res = Math.max(res, x);set.add(x);}System.out.println(res - set.size() + 1);}
