結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー yuruhiyayuruhiya
提出日時 2020-10-10 16:26:09
言語 Crystal
(1.11.2)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 1,465 bytes
コンパイル時間 20,901 ms
コンパイル使用メモリ 251,884 KB
実行使用メモリ 93,052 KB
最終ジャッジ日時 2023-09-13 12:09:19
合計ジャッジ時間 41,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
68,544 KB
testcase_01 AC 77 ms
67,712 KB
testcase_02 AC 73 ms
67,660 KB
testcase_03 AC 72 ms
67,844 KB
testcase_04 AC 73 ms
67,816 KB
testcase_05 AC 71 ms
68,384 KB
testcase_06 AC 72 ms
67,904 KB
testcase_07 AC 72 ms
67,752 KB
testcase_08 AC 71 ms
67,740 KB
testcase_09 AC 73 ms
67,852 KB
testcase_10 AC 76 ms
67,800 KB
testcase_11 AC 71 ms
67,696 KB
testcase_12 AC 72 ms
67,776 KB
testcase_13 AC 72 ms
67,832 KB
testcase_14 AC 73 ms
67,952 KB
testcase_15 AC 72 ms
67,788 KB
testcase_16 AC 71 ms
67,796 KB
testcase_17 AC 72 ms
67,944 KB
testcase_18 AC 125 ms
88,984 KB
testcase_19 AC 123 ms
90,544 KB
testcase_20 AC 103 ms
81,320 KB
testcase_21 AC 68 ms
56,452 KB
testcase_22 AC 102 ms
80,020 KB
testcase_23 AC 66 ms
55,864 KB
testcase_24 AC 112 ms
86,880 KB
testcase_25 AC 103 ms
83,980 KB
testcase_26 AC 83 ms
78,112 KB
testcase_27 AC 65 ms
56,088 KB
testcase_28 AC 129 ms
90,952 KB
testcase_29 AC 132 ms
92,032 KB
testcase_30 AC 130 ms
91,728 KB
testcase_31 AC 130 ms
92,136 KB
testcase_32 AC 130 ms
91,256 KB
testcase_33 AC 129 ms
91,944 KB
testcase_34 AC 129 ms
91,880 KB
testcase_35 AC 131 ms
92,840 KB
testcase_36 AC 132 ms
92,240 KB
testcase_37 AC 130 ms
91,500 KB
testcase_38 AC 130 ms
92,120 KB
testcase_39 AC 131 ms
92,920 KB
testcase_40 AC 132 ms
93,052 KB
testcase_41 AC 131 ms
91,764 KB
testcase_42 AC 134 ms
92,636 KB
testcase_43 AC 73 ms
67,768 KB
testcase_44 AC 71 ms
67,664 KB
権限があれば一括ダウンロードができます

ソースコード

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