結果

問題 No.1425 Yet Another Cyclic Shifts Sorting
ユーザー 箱星
提出日時 2020-08-29 13:34:12
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 572 ms / 2,000 ms
コード長 1,757 bytes
コンパイル時間 13,401 ms
コンパイル使用メモリ 434,660 KB
実行使用メモリ 90,696 KB
最終ジャッジ日時 2024-07-04 20:33:13
合計ジャッジ時間 34,804 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextInt()
    val a = Array(n) { sc.nextInt() }
    // a[i],...,a[n-1]がソートされているかどうか
    val isSorted = Array(n) { false }
    // a[0],...,a[i]における減少回数
    val decrease = Array(n) { 0 }
    // a[0],...,a[i]の最大値
    val max = Array(n) { a[0] }
    isSorted[n - 1] = true
    for (i in n - 2 downTo 0) {
        if (a[i] <= a[i + 1]) {
            isSorted[i] = true
        } else {
            break
        }
    }
    if (isSorted[0]) {
        println(0)
        return
    }
    for (i in 1 until n) {
        decrease[i] = decrease[i - 1]
        if (a[i - 1] > a[i]) decrease[i]++
        max[i] = maxOf(max[i - 1], a[i])
    }
    for (i in 0 until n) {
        if (decrease[i] == 1 && a[i] <= a[0] && i == n - 1) {
            println(1)
            return
        }
        if (decrease[i] == 1 && a[i] <= a[0] && isSorted[i + 1] && max[i] <= a[i + 1]) {
            println(1)
            return
        }
    }
    println(2)
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}
0