結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー yuruhiyayuruhiya
提出日時 2020-10-10 16:26:09
言語 Crystal
(1.14.0)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,465 bytes
コンパイル時間 11,182 ms
コンパイル使用メモリ 296,484 KB
実行使用メモリ 94,376 KB
最終ジャッジ日時 2024-06-30 21:16:00
合計ジャッジ時間 27,760 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

class CulSum(T)
  def initialize(a : Array(T))
    @n = a.size
    @s = Array(T).new(@n + 1, T.zero)
    @n.times do |i|
      @s[i + 1] = @s[i] + a[i]
    end
  end

  def initialize(@n : Int32, &f : Int32 -> T)
    @s = Array(T).new(@n + 1, T.zero)
    @n.times do |i|
      @s[i + 1] = @s[i] + yield(i)
    end
  end

  def initialize(a, &f)
    @n = a.size
    @s = Array(T).new(@n + 1, T.zero)
    @n.times do |i|
      @s[i + 1] = @s[i] + yield(a[i])
    end
  end

  def [](l : Int32, r : Int32)
    l < r ? @s[r] - @s[l] : 0
  end

  def [](i : Int32)
    @s[i]
  end

  def [](r : Range(Int32, Int32))
    @s[r.exclusive? ? r.end : r.end + 1] - @s[r.begin]
  end

  def to_a
    (0...@n).map { |i| self[i..i] }
  end
end

n = read_line.to_i
a = read_line.split.map(&.to_i)
b = read_line.split.map(&.to_i64)

MAX_A = 2 * 10**6
c = Array(Int64).new(MAX_A + 1, 0)
n.times { |i| c[a[i] + MAX_A // 2] += b[i] }
c_sum = CulSum.new(c)

val_left = Array(Int64).new(MAX_A + 1, 0)
(1..MAX_A).each do |i|
  val_left[i] = val_left[i - 1] + c_sum[0...i]
end
val_right = Array(Int64).new(MAX_A + 1, 0)
(0...MAX_A).reverse_each do |i|
  val_right[i] = val_right[i + 1] + c_sum[i + 1..MAX_A]
end

min_val, min_index = (0..MAX_A).map { |i| val_left[i] + val_right[i] }.each_with_index.min
puts [min_index - MAX_A // 2, min_val].join(' ')
0