1.题目:
2.解析:
解法:
步骤一:创建大根堆,把筛选出偶数数据,然后全部放入大根堆;
步骤二:拿出堆顶元素,从sum中减掉, 减小的值,最后返回sum.
代码:
public static void main(String[] args){Scanner in = new Scanner(System.in);int n = in.nextInt(),k = in.nextInt();//创建大根堆PriorityQueue<Integer> heap = new PriorityQueue<>((a,b) -> {return b-a;});//把偶数丢进大根堆,获取偶数大根堆的和long sum = 0,num;for(int i = 0; i < n; i++){num = in.nextLong();sum += num;if(num % 2 == 0) {heap.offer((int)num);}}//拿出堆顶元素,除2,从sum中减掉减小的值while(!heap.isEmpty() && k-- != 0){long t = heap.poll() / 2;sum -= t;if(t % 2 == 0) heap.offer((int)t);}System.out.print(sum);}