File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ int T = Integer . parseInt(br. readLine());
9+
10+ StringBuilder sb = new StringBuilder ();
11+ while (T -- > 0 ) {
12+ int M = Integer . parseInt(br. readLine());
13+ sb. append((M + 1 ) / 2 ). append(" \n " );
14+
15+ PriorityQueue<Integer > maxHeap = new PriorityQueue<> (Collections . reverseOrder());
16+ PriorityQueue<Integer > minHeap = new PriorityQueue<> ();
17+
18+ int count = 0 ;
19+ StringTokenizer st = null ;
20+
21+ for (int i = 0 ; i < M ; i++ ) {
22+ if (i % 10 == 0 ) {
23+ st = new StringTokenizer (br. readLine());
24+ }
25+
26+ int num = Integer . parseInt(st. nextToken());
27+
28+ if (maxHeap. size() == minHeap. size()) maxHeap. add(num);
29+ else minHeap. add(num);
30+
31+ if (! minHeap. isEmpty() && maxHeap. peek() > minHeap. peek()) {
32+ int tmp1 = maxHeap. poll();
33+ int tmp2 = minHeap. poll();
34+ maxHeap. add(tmp2);
35+ minHeap. add(tmp1);
36+ }
37+
38+ if (i % 2 == 0 ) {
39+ sb. append(maxHeap. peek()). append(" " );
40+ count++ ;
41+ if (count % 10 == 0 ) sb. append(" \n " );
42+ }
43+ }
44+ if (count % 10 != 0 ) sb. append(" \n " );
45+ }
46+ System . out. print(sb. toString());
47+ }
48+ }
49+ ```
You can’t perform that action at this time.
0 commit comments