-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0326.kt
44 lines (41 loc) · 1.1 KB
/
0326.kt
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
fun main( args : Array<String> ) {
val n = readLine()!!.toInt()
val k = readLine()!!.toInt()
val base = (1..n).map { it }.toMutableList()
(1..k).map {
val swapPoint = readLine()!!.split(" ").first().toInt() - 1
val so = base[swapPoint]
val ta = base[swapPoint+1]
base[swapPoint] = ta
base[swapPoint+1] = so
}
val ans = readLine()!!.split(" ").mapIndexed { i,it ->
Pair(it.toInt(), i )
}
val ansMap = ans.toMap()
// here is 直前
val basePair = base.map {
Pair(it, ansMap[it]!!)
}.toMutableList()
//println( base )
//println( basePair )
val swapPoints = mutableListOf<Int>()
loop@while(true) {
for( i in (0..basePair.size-2) ) {
if( basePair[i].second > basePair[i+1].second ) {
val a1 = basePair[i+1]
val a2 = basePair[i]
basePair[i] = a1
basePair[i+1] = a2
swapPoints.add( i )
}
if( basePair.map { it.second } == basePair.map { it.second }.sortedBy { it } )
break@loop
}
}
//println( basePair )
println( swapPoints.size )
swapPoints.map {
println("${it + 1} ${it + 2}")
}
}