結果

問題 No.3128 Isosceles Triangle
ユーザー Nanashi.
提出日時 2025-04-25 22:46:28
言語 Ruby
(3.4.1)
結果
AC  
実行時間 839 ms / 2,500 ms
コード長 2,214 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 8,064 KB
実行使用メモリ 42,140 KB
最終ジャッジ日時 2025-04-25 22:46:43
合計ジャッジ時間 14,831 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:20: warning: assigned but unused variable - in_n
Main.rb:45: warning: 'frozen_string_literal' is ignored after any tokens
Main.rb:80: warning: 'frozen_string_literal' is ignored after any tokens
Syntax OK

ソースコード

diff #

# Kept libraries:
#   (none)
# Expanded libraries:
# - nanacl/bsearch_right
# - nanacl/array_sum
# Errored libraries:
#   (none)
# Removed libraries:
#   (none)
#
# ------------------------------------------------------------------------------

# frozen_string_literal: true
# This file is expanded by nanacl.

main = -> do # =================================================================
# require "nanacl/bsearch_right" # (expanded: L32)
# require "nanacl/array_sum" # (expanded: L67)

in_n = gets.chomp.to_i
in_a = gets.chomp.split.map(&:to_i)

def nc2(n)
  n * (n - 1) / 2
end

a = in_a.sort.chunk { |x| x }.map { |k, v| [k, v.length] }
lengths = a.map(&:last)

result = 0
a.each_with_index do |(k, v), i|
  next if v == 1
  max_index = a.bsearch_index_right { |(x, _)| x < k * 2 }
  last_edge = lengths.sum_in_range(0..max_index) - v

  result += last_edge * nc2(v)
end

puts result

end # --------------------------------------------------------------------------

# === dependencies -------------------------------------------------------------
# == nanacl/bsearch_right from main --------------------------------------------
# frozen_string_literal: true

class Array
  def bsearch_right(&)
    index = bsearch_index_right(&)
    index && self[index]
  end

  def bsearch_index_right(&block)
    right = bsearch_index { |elem| !block.call(elem) }
    if right.nil?
      size - 1
    elsif right == 0
      nil
    else
      right - 1
    end
  end
end

class Range
  def bsearch_right(&block)
    right = bsearch { |elem| !block.call(elem) }
    if right.nil?
      last
    elsif right == first
      nil
    else
      right - 1
    end
  end
end


# == nanacl/array_sum from main ------------------------------------------------
# frozen_string_literal: true
class Array
  def sum_in_range(range)
    unless @sums
      @sums = [0]
      sum = 0
      each { |v| @sums << (sum += v) }
    end
    if range.end.nil?
      @sums[length] - @sums[range.begin]
    elsif range.exclude_end?
      @sums[range.end] - @sums[range.begin]
    else
      @sums[range.end + 1] - @sums[range.begin]
    end
  end
end


# ==============================================================================

main.call
0