fork download
  1. import java.util.*
  2.  
  3. data class Task(val num: Int, val priority: Int)
  4.  
  5. fun main() {
  6. val sc = Scanner(System.`in`)
  7.  
  8. val sb = StringBuilder()
  9. val cnt = sc.nextLine().toInt()
  10. repeat(cnt) {
  11. val pq = PriorityQueue<Int>(compareByDescending({ it }))
  12. val q = LinkedList<Task>()
  13. val (n, m) = sc.nextLine().split(" ").map { it.toInt() }
  14. val token = StringTokenizer(sc.nextLine())
  15. repeat(n) {
  16. val priority = token.nextToken().toInt()
  17. pq.add(priority)
  18. q.addFirst(Task(num = it, priority = priority))
  19. }
  20.  
  21. var current = 1
  22. while (q.isNotEmpty()) {
  23. val task = q.removeLast()
  24. if (task.priority == pq.peek()) {
  25. pq.poll()
  26. if (task.num == m) {
  27. sb.append(current).append("\n")
  28. break
  29. }
  30. current++
  31. } else {
  32. q.addFirst(task)
  33. }
  34. }
  35. }
  36.  
  37. println(sb)
  38. }
  39.  
Success #stdin #stdout 0.17s 42860KB
stdin
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
stdout
1
2
5