-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q1966.java
82 lines (71 loc) · 2.01 KB
/
Q1966.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package forRank;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class Q1966 {
private static class Node implements Comparable<Node>{
int priority;
boolean isTarget;
public Node(int priority){
this.priority = priority;
isTarget = false;
}
@Override
public int compareTo(Node node){
return node.priority - priority;
}
}
private static int getCount(Queue<Node> queue, PriorityQueue<Node> pq){
int counter = 1;
while(!queue.isEmpty()){
Node cur = queue.poll();
Node highest = pq.poll();
if(highest.priority == cur.priority){
if(cur.isTarget)
return counter;
} else {
queue.add(cur);
while(true){
cur = queue.poll();
if(cur.priority == highest.priority){
if(cur.isTarget)
return counter;
break;
}
queue.add(cur);
}
}
counter++;
}
return counter;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Queue<Node> queue = new LinkedList<Node>();
PriorityQueue<Node> pq = new PriorityQueue<Node>();
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
Node newNode = new Node(Integer.parseInt(st.nextToken()));
if(j == M)
newNode.isTarget = true;
queue.add(newNode);
pq.add(newNode);
}
bw.write(getCount(queue, pq) + "\n");
}
br.close();
bw.close();
}
}