結果

問題 No.5005 3-SAT
ユーザー tomeruntomerun
提出日時 2022-04-29 14:32:25
言語 Crystal
(1.11.2)
結果
AC  
実行時間 1,580 ms / 2,000 ms
コード長 3,167 bytes
コンパイル時間 17,052 ms
実行使用メモリ 6,956 KB
スコア 739
最終ジャッジ日時 2022-04-29 14:35:29
合計ジャッジ時間 180,778 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,580 ms
5,196 KB
testcase_01 AC 1,579 ms
5,124 KB
testcase_02 AC 1,580 ms
5,208 KB
testcase_03 AC 1,579 ms
5,160 KB
testcase_04 AC 1,579 ms
5,160 KB
testcase_05 AC 1,580 ms
5,112 KB
testcase_06 AC 1,580 ms
5,160 KB
testcase_07 AC 1,579 ms
5,224 KB
testcase_08 AC 1,580 ms
6,952 KB
testcase_09 AC 1,579 ms
5,208 KB
testcase_10 AC 1,580 ms
5,220 KB
testcase_11 AC 1,580 ms
5,120 KB
testcase_12 AC 1,580 ms
6,948 KB
testcase_13 AC 1,568 ms
6,952 KB
testcase_14 AC 1,579 ms
5,160 KB
testcase_15 AC 1,580 ms
6,948 KB
testcase_16 AC 1,580 ms
5,160 KB
testcase_17 AC 1,578 ms
5,184 KB
testcase_18 AC 1,580 ms
5,144 KB
testcase_19 AC 1,580 ms
5,188 KB
testcase_20 AC 1,580 ms
6,948 KB
testcase_21 AC 1,579 ms
6,952 KB
testcase_22 AC 1,580 ms
6,948 KB
testcase_23 AC 1,579 ms
5,168 KB
testcase_24 AC 1,580 ms
5,172 KB
testcase_25 AC 1,578 ms
6,948 KB
testcase_26 AC 1,579 ms
6,952 KB
testcase_27 AC 1,579 ms
6,952 KB
testcase_28 AC 1,580 ms
5,156 KB
testcase_29 AC 1,579 ms
5,208 KB
testcase_30 AC 1,579 ms
5,216 KB
testcase_31 AC 1,579 ms
5,112 KB
testcase_32 AC 1,579 ms
5,136 KB
testcase_33 AC 1,579 ms
5,232 KB
testcase_34 AC 1,580 ms
5,236 KB
testcase_35 AC 1,579 ms
5,204 KB
testcase_36 AC 1,580 ms
6,948 KB
testcase_37 AC 1,579 ms
5,120 KB
testcase_38 AC 1,580 ms
6,952 KB
testcase_39 AC 1,578 ms
5,200 KB
testcase_40 AC 1,579 ms
5,180 KB
testcase_41 AC 1,579 ms
5,136 KB
testcase_42 AC 1,579 ms
5,216 KB
testcase_43 AC 1,579 ms
5,136 KB
testcase_44 AC 1,580 ms
5,204 KB
testcase_45 AC 1,580 ms
6,948 KB
testcase_46 AC 1,572 ms
5,232 KB
testcase_47 AC 1,579 ms
5,120 KB
testcase_48 AC 1,579 ms
6,948 KB
testcase_49 AC 1,579 ms
5,112 KB
testcase_50 AC 1,579 ms
5,128 KB
testcase_51 AC 1,579 ms
6,948 KB
testcase_52 AC 1,580 ms
5,248 KB
testcase_53 AC 1,578 ms
5,284 KB
testcase_54 AC 1,579 ms
6,956 KB
testcase_55 AC 1,580 ms
5,116 KB
testcase_56 AC 1,578 ms
5,132 KB
testcase_57 AC 1,578 ms
5,112 KB
testcase_58 AC 1,580 ms
5,156 KB
testcase_59 AC 1,579 ms
6,948 KB
testcase_60 AC 1,578 ms
5,196 KB
testcase_61 AC 1,579 ms
6,948 KB
testcase_62 AC 1,579 ms
5,140 KB
testcase_63 AC 1,579 ms
5,296 KB
testcase_64 AC 1,579 ms
5,292 KB
testcase_65 AC 1,579 ms
6,952 KB
testcase_66 AC 1,579 ms
5,248 KB
testcase_67 AC 1,579 ms
6,948 KB
testcase_68 AC 1,578 ms
6,948 KB
testcase_69 AC 1,580 ms
5,228 KB
testcase_70 AC 1,579 ms
5,264 KB
testcase_71 AC 1,579 ms
5,092 KB
testcase_72 AC 1,579 ms
5,156 KB
testcase_73 AC 1,579 ms
5,160 KB
testcase_74 AC 1,579 ms
5,152 KB
testcase_75 AC 1,578 ms
5,244 KB
testcase_76 AC 1,579 ms
5,124 KB
testcase_77 AC 1,579 ms
6,948 KB
testcase_78 AC 1,579 ms
5,100 KB
testcase_79 AC 1,579 ms
5,128 KB
testcase_80 AC 1,579 ms
5,144 KB
testcase_81 AC 1,579 ms
6,952 KB
testcase_82 AC 1,578 ms
5,100 KB
testcase_83 AC 1,579 ms
5,112 KB
testcase_84 AC 1,579 ms
6,948 KB
testcase_85 AC 1,578 ms
6,952 KB
testcase_86 AC 1,580 ms
5,136 KB
testcase_87 AC 1,579 ms
5,152 KB
testcase_88 AC 1,579 ms
5,152 KB
testcase_89 AC 1,579 ms
5,232 KB
testcase_90 AC 1,579 ms
5,088 KB
testcase_91 AC 1,571 ms
6,948 KB
testcase_92 AC 1,580 ms
5,120 KB
testcase_93 AC 1,580 ms
5,256 KB
testcase_94 AC 1,580 ms
5,140 KB
testcase_95 AC 1,579 ms
5,172 KB
testcase_96 AC 1,578 ms
5,188 KB
testcase_97 AC 1,580 ms
5,100 KB
testcase_98 AC 1,579 ms
6,952 KB
testcase_99 AC 1,579 ms
5,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

START_TIME = Time.utc.to_unix_ms
TL         = 1580
N          = 2048
M          =  256
INF        = 1 << 29
RND        = Random.new(2)

macro debug(msg)
  {% if flag?(:trace) %}
    STDERR.puts({{msg}})
  {% end %}
end

macro debugf(format_string, *args)
  {% if flag?(:trace) %}
    STDERR.printf({{format_string}}, {{*args}})
  {% end %}
end

class Result
  property :bits, :score

  def initialize(@bits : Array(Int32), @score : Int32)
  end

  def to_s(io)
    io << @bits.map { |v| v == -1 ? 0 : v }.reverse.join
  end
end

struct Cond
  getter :var, :val

  def initialize(@var : Int32, @val : Int32)
  end
end

alias Term = Tuple(Cond, Cond, Cond)

class Solver
  def initialize
    @terms = Array(Term).new(N) do
      a, b, c, p, q, r = read_line.split.map(&.to_i)
      {Cond.new(a, p), Cond.new(b, q), Cond.new(c, r)}
    end
    @var_pos = Array(Array(Tuple(Int32, Int32))).new(M) { [] of Tuple(Int32, Int32) }
    N.times do |i|
      3.times do |j|
        term = @terms[i][j]
        @var_pos[term.var] << {i, term.val}
      end
    end
    @best_result = Result.new(Array.new(M, -1), 0)
    @ans = Array(Int32).new(N, -1)
    @search_order = [0, 1, 2]
    @dfs_count = 0
  end

  def solve(timelimit)
    start_time = Time.utc.to_unix_ms
    before_time = start_time
    solve_single()
    after_time = Time.utc.to_unix_ms
    worst_time = after_time - before_time
    turn = 0
    while true
      before_time = after_time
      if before_time + worst_time > timelimit
        debug("turn:#{turn} worst_time:#{worst_time}")
        break
      end
      solve_single()
      # if res.score > best_res.score
      #   best_res = res
      #   best_loops = loops
      #   debug("score:#{best_res.score} at turn #{turn}")
      # end
      after_time = Time.utc.to_unix_ms
      worst_time = {worst_time, after_time - before_time}.max
      turn += 1
    end
    return @best_result
  end

  def solve_single
    @ans.fill(-1)
    @dfs_count = 0
    dfs(0)
  end

  def dfs(pos)
    if pos > @best_result.score
      @best_result.score = pos
      @best_result.bits[0, N] = @ans
      debug("best_score:#{pos}")
    end
    if @dfs_count > 1000
      return
    end
    if @terms[pos].any? { |t| @ans[t.var] == t.val }
      dfs(pos + 1)
    else
      @dfs_count += 1
      shuffle(@search_order)
      3.times do |i|
        t = @terms[pos][@search_order[i]]
        if @ans[t.var] == -1
          ok = true
          @ans[t.var] = t.val
          @var_pos[t.var].each do |ps|
            p, v = ps
            if p > pos && p <= @best_result.score && v != t.val
              if @terms[p].all? { |nt| @ans[nt.var] == 1 - nt.val }
                ok = false
                break
              end
            end
          end
          if ok
            dfs(pos + 1)
          end
          @ans[t.var] = -1
        end
      end
    end
  end

  def shuffle(a)
    (a.size - 1).times do |i|
      pos = RND.rand(a.size - i) + i
      a[i], a[pos] = a[pos], a[i]
    end
  end
end

def main
  solver = Solver.new
  res = solver.solve(START_TIME + TL)
  puts res
  {% if flag?(:local) %}
    STDERR.puts("score:#{res.score}")
  {% end %}
end

main
0