結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー yakamotoyakamoto
提出日時 2020-06-26 21:39:47
言語 Kotlin
(1.9.10)
結果
AC  
実行時間 1,295 ms / 2,000 ms
コード長 3,341 bytes
コンパイル時間 21,354 ms
コンパイル使用メモリ 435,708 KB
実行使用メモリ 111,544 KB
最終ジャッジ日時 2023-09-18 03:36:29
合計ジャッジ時間 41,025 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
53,532 KB
testcase_01 AC 298 ms
53,544 KB
testcase_02 AC 279 ms
49,936 KB
testcase_03 AC 301 ms
53,936 KB
testcase_04 AC 295 ms
53,580 KB
testcase_05 AC 311 ms
55,804 KB
testcase_06 AC 300 ms
53,500 KB
testcase_07 AC 298 ms
53,600 KB
testcase_08 AC 303 ms
55,404 KB
testcase_09 AC 302 ms
53,652 KB
testcase_10 AC 302 ms
53,572 KB
testcase_11 AC 308 ms
53,684 KB
testcase_12 AC 300 ms
53,548 KB
testcase_13 AC 443 ms
56,812 KB
testcase_14 AC 453 ms
56,968 KB
testcase_15 AC 457 ms
56,912 KB
testcase_16 AC 462 ms
56,876 KB
testcase_17 AC 451 ms
56,876 KB
testcase_18 AC 452 ms
56,924 KB
testcase_19 AC 443 ms
56,956 KB
testcase_20 AC 458 ms
57,128 KB
testcase_21 AC 447 ms
57,192 KB
testcase_22 AC 452 ms
56,812 KB
testcase_23 AC 1,161 ms
102,984 KB
testcase_24 AC 1,295 ms
110,652 KB
testcase_25 AC 1,126 ms
102,836 KB
testcase_26 AC 1,286 ms
111,544 KB
testcase_27 AC 1,137 ms
100,696 KB
testcase_28 AC 901 ms
99,040 KB
testcase_29 AC 877 ms
99,268 KB
testcase_30 AC 1,088 ms
101,208 KB
testcase_31 AC 1,185 ms
110,936 KB
testcase_32 AC 1,164 ms
110,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007L

class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    val N = ni()
    val A = nal(N)
    val set = TreeSet<Long>()
    val B = Array(N){Array(2){LongArray(2){-1} } }
    for (i in 0 until N) {
      val mn = if (set.isEmpty()) null else set.first()
      if (mn != null && mn < A[i]) B[i][0][0] = mn
      val gt = set.higher(A[i])
      if (gt != null && gt > A[i]) B[i][1][0] = gt
      set += A[i]
    }

    set.clear()
    for (i in N - 1 downTo 0) {
      val mn = if (set.isEmpty()) null else set.first()
      if (mn != null && mn < A[i]) B[i][0][1] = mn
      val gt = set.higher(A[i])
      if (gt != null && gt > A[i]) B[i][1][1] = gt
      set += A[i]
    }

    val INF = 1e18.toLong()
    var ans = INF
    for (i in 0 until N) {
      if (B[i][0][0] != -1L && B[i][0][1] != -1L) {
        ans = min(B[i][0].sum() + A[i], ans)
      }
      if (B[i][1][0] != -1L && B[i][1][1] != -1L) {
        ans = min(B[i][1].sum() + A[i], ans)
      }
    }

    if (ans == INF) out.println(-1)
    else out.println(ans)
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return map(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun map(n: Int, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i)
    }
    return res
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  private fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: BooleanArray) {
    debug { a.map { if (it) 1 else 0 }.joinToString("") }
  }

  private fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0