拼多多笔试312
第四题双堆 一直47% 大佬帮忙看看错哪了
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int cnt=in.nextInt();
int[] days=new int[cnt];
for (int i=0;i<cnt;i++){
days[i]=in.nextInt();
}
float sum=0;
int[] ave=new int[cnt];
int[] mid=new int[cnt];
//小顶堆 右边数据
PriorityQueue<Integer> queueA=new PriorityQueue<>();
//大顶堆 左边数据
PriorityQueue<Integer> queueB=new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for (int i=0;i<cnt;i++){
sum+=days[i];
ave[i]=Math.round(sum/(i+1));
if (i%2==0){
queueB.offer(days[i]);
int max=queueB.poll();
queueA.add(max);
mid[i]=queueA.peek();
}
else {
queueA.offer(days[i]);
int min=queueA.poll();
queueB.add(min);
mid[i]=(int)Math.round((queueA.peek()+ queueB.peek())/2.0);
}
}
for (int i=0;i<cnt;i++){
System.out.print(ave[i]+" ");
}
System.out.println();
for (int i=0;i<cnt;i++){
System.out.print(mid[i]+" ");
}
}
#拼多多##笔试#