AcWing 106. Java 简介代码版动态中位数
原题链接
中等
作者:
henhen敲
,
2020-06-13 17:51:12
,
所有人可见
,
阅读 543
import java.io.*;
import java.util.*;
class Main{
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt()throws Exception{
in.nextToken();
return (int)in.nval;
}
public static void main(String[] args)throws Exception{
int P = nextInt();
Queue<Integer> qu1 = new PriorityQueue<>((o1, o2)->o2-o1);
Queue<Integer> qu2 = new PriorityQueue<>();
while(P--!=0){
int n = nextInt();
int M = nextInt();
qu1.clear();qu2.clear();
out.print(n + " " + (M+1)/2);
out.println("");
int cnt = 0;
boolean flag = true;
for(int i=0; i<M; i++){
int cur = nextInt();
if(qu2.isEmpty()||cur>qu2.peek()) qu2.offer(cur);
else qu1.offer(cur);
if(qu1.size()>qu2.size()) qu2.offer(qu1.poll());
if(qu2.size()-qu1.size()>1) qu1.offer(qu2.poll());
if(flag){
out.print(qu2.peek() + " ");
cnt++;
if(cnt%10==0) out.println("");
}
flag = !flag;
}
if(cnt%10!=0)out.println("");
}
out.close();
}
}