Submission #1648990


Source Code Expand

Copy
lines = $stdin.read
array = lines.split("\n")

def ceil_index(array, l, r, key)
  while r-l > 1
    m = l + (r-l)/2
    if array[m] >= key
      r = m
    else
      l = m
    end
  end
  r
end

def get_lis(arr)

  tail = Array.new(arr.length)
  len = 1
  tail[0] = arr.first

  for i in 1...arr.length
    #puts tail.to_s

    if arr[i] < tail[0]
      # new smallest value
      tail[0] = arr[i]
    elsif arr[i] > tail[len-1]
      # arr[i] extends largest subsequence
      tail[len] = arr[i]
      len = len + 1
    else
      # option(a): use binary search
      # but, ruby's Array#bsearch_index requires
      # all array's elements sorted
      #idx = tail[0..i].compact.bsearch_index do |x|
      #  x >= arr[i]
      #end

      # option(b): implement binary search yourself
      idx = ceil_index(tail, -1, len-1, arr[i])
      tail[idx] = arr[i]
    end
  end

  #puts tail.to_s
  len
end

N = array[0].to_i
a = array[1..N].map(&:to_i).to_a

puts N - get_lis(a)

Submission Info

Submission Time
Task D - トランプ挿入ソート
User hiroyuking
Language Ruby (2.3.3)
Score 100
Code Size 1026 Byte
Status
Exec Time 49 ms
Memory 3836 KB

Test Cases

Set Name Score / Max Score Test Cases
smallA 10 / 10 test_01_ABC.txt, test_04_ABC.txt, test_07_ABC.txt, test_10_ABC.txt, test_13_ABC.txt, test_16_ABC.txt, test_19_ABC.txt, test_22_ABC.txt, test_25_ABC.txt, test_28_ABC.txt, test_31_ABC.txt, test_32_ABC.txt, test_35_ABC.txt, test_38_ABC.txt, test_41_ABC.txt, test_44_ABC.txt, test_47_ABC.txt, test_50_ABC.txt, test_53_ABC.txt
smallB 40 / 40 test_01_ABC.txt, test_02_AB.txt, test_04_ABC.txt, test_05_AB.txt, test_07_ABC.txt, test_08_AB.txt, test_10_ABC.txt, test_11_AB.txt, test_13_ABC.txt, test_14_AB.txt, test_16_ABC.txt, test_17_AB.txt, test_19_ABC.txt, test_20_AB.txt, test_22_ABC.txt, test_23_AB.txt, test_25_ABC.txt, test_26_AB.txt, test_28_ABC.txt, test_29_AB.txt, test_31_ABC.txt, test_32_ABC.txt, test_33_AB.txt, test_35_ABC.txt, test_36_AB.txt, test_38_ABC.txt, test_39_AB.txt, test_41_ABC.txt, test_42_AB.txt, test_44_ABC.txt, test_45_AB.txt, test_47_ABC.txt, test_48_AB.txt, test_50_ABC.txt, test_51_AB.txt, test_53_ABC.txt, test_54_AB.txt
all 50 / 50 test_01_ABC.txt, test_02_AB.txt, test_03_A.txt, test_04_ABC.txt, test_05_AB.txt, test_06_A.txt, test_07_ABC.txt, test_08_AB.txt, test_09_A.txt, test_10_ABC.txt, test_11_AB.txt, test_12_A.txt, test_13_ABC.txt, test_14_AB.txt, test_15_A.txt, test_16_ABC.txt, test_17_AB.txt, test_18_A.txt, test_19_ABC.txt, test_20_AB.txt, test_21_A.txt, test_22_ABC.txt, test_23_AB.txt, test_24_A.txt, test_25_ABC.txt, test_26_AB.txt, test_27_A.txt, test_28_ABC.txt, test_29_AB.txt, test_30_A.txt, test_31_ABC.txt, test_32_ABC.txt, test_33_AB.txt, test_34_A.txt, test_35_ABC.txt, test_36_AB.txt, test_37_A.txt, test_38_ABC.txt, test_39_AB.txt, test_40_A.txt, test_41_ABC.txt, test_42_AB.txt, test_43_A.txt, test_44_ABC.txt, test_45_AB.txt, test_46_A.txt, test_47_ABC.txt, test_48_AB.txt, test_49_A.txt, test_50_ABC.txt, test_51_AB.txt, test_52_A.txt, test_53_ABC.txt, test_54_AB.txt, test_55_A.txt
Case Name Status Exec Time Memory
sample_01.txt 7 ms 1788 KB
sample_02.txt 7 ms 1788 KB
sample_03.txt 7 ms 1788 KB
test_01_ABC.txt 7 ms 1788 KB
test_02_AB.txt 8 ms 1788 KB
test_03_A.txt 49 ms 3836 KB
test_04_ABC.txt 7 ms 1788 KB
test_05_AB.txt 8 ms 1788 KB
test_06_A.txt 11 ms 1916 KB
test_07_ABC.txt 7 ms 1788 KB
test_08_AB.txt 8 ms 1788 KB
test_09_A.txt 49 ms 3836 KB
test_10_ABC.txt 7 ms 1788 KB
test_11_AB.txt 7 ms 1788 KB
test_12_A.txt 29 ms 2940 KB
test_13_ABC.txt 7 ms 1788 KB
test_14_AB.txt 8 ms 1788 KB
test_15_A.txt 49 ms 3836 KB
test_16_ABC.txt 7 ms 1788 KB
test_17_AB.txt 7 ms 1788 KB
test_18_A.txt 9 ms 1788 KB
test_19_ABC.txt 7 ms 1788 KB
test_20_AB.txt 8 ms 1788 KB
test_21_A.txt 48 ms 3836 KB
test_22_ABC.txt 7 ms 1788 KB
test_23_AB.txt 8 ms 1788 KB
test_24_A.txt 39 ms 3324 KB
test_25_ABC.txt 7 ms 1788 KB
test_26_AB.txt 8 ms 1788 KB
test_27_A.txt 47 ms 3836 KB
test_28_ABC.txt 7 ms 1788 KB
test_29_AB.txt 8 ms 1788 KB
test_30_A.txt 40 ms 3452 KB
test_31_ABC.txt 7 ms 1788 KB
test_32_ABC.txt 7 ms 1788 KB
test_33_AB.txt 7 ms 1788 KB
test_34_A.txt 24 ms 3836 KB
test_35_ABC.txt 7 ms 1788 KB
test_36_AB.txt 7 ms 1788 KB
test_37_A.txt 10 ms 2044 KB
test_38_ABC.txt 7 ms 1788 KB
test_39_AB.txt 7 ms 1788 KB
test_40_A.txt 22 ms 3836 KB
test_41_ABC.txt 7 ms 1788 KB
test_42_AB.txt 7 ms 1788 KB
test_43_A.txt 9 ms 1916 KB
test_44_ABC.txt 7 ms 1788 KB
test_45_AB.txt 8 ms 1788 KB
test_46_A.txt 37 ms 3836 KB
test_47_ABC.txt 7 ms 1788 KB
test_48_AB.txt 7 ms 1788 KB
test_49_A.txt 11 ms 1916 KB
test_50_ABC.txt 7 ms 1788 KB
test_51_AB.txt 7 ms 1788 KB
test_52_A.txt 27 ms 3836 KB
test_53_ABC.txt 7 ms 1788 KB
test_54_AB.txt 7 ms 1788 KB
test_55_A.txt 27 ms 3836 KB