萌新Jvav佬求佬解答,为什么这个题堆不行?
查看原帖
萌新Jvav佬求佬解答,为什么这个题堆不行?
732415
Molie楼主2023/9/24 16:29

佬们,我的思路是:维护两个堆,一个从大顶堆,一个小顶堆,然后与当前的数取绝对值之差,然后ans+=(两个绝对值之差最大的数)

但是这个思路只得到了十分,是我对题目理解不对吗? 求佬们解答下,下面给出了主函数

public static void main(String[] args) throws Exception {
		int n = read.nextInt();
		int ans = 0;
		PriorityQueue<Integer> queue1 = new PriorityQueue<>();
		PriorityQueue<Integer> queue2 = new PriorityQueue<>((a, b) -> b - a);
		int v = read.nextInt();
		ans += v;
		queue2.add(v);
		queue1.add(v);
		for (int i = 1; i < n; ++i) {
			v = read.nextInt();
			ans += Math.min((abs(queue1.peek() - v)), (abs(queue2.peek() - v)));
			queue1.add(v);
			queue2.add(v);
		}
		pr.write(ans + "");
		close();
	}
2023/9/24 16:29
加载中...