結果

問題 No.5005 3-SAT
ユーザー simansiman
提出日時 2022-05-06 14:56:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,883 ms / 2,000 ms
コード長 5,429 bytes
コンパイル時間 1,242 ms
実行使用メモリ 5,224 KB
スコア 98,263
最終ジャッジ日時 2022-05-06 15:00:12
合計ジャッジ時間 197,558 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,883 ms
5,020 KB
testcase_01 AC 1,881 ms
4,968 KB
testcase_02 AC 1,882 ms
5,152 KB
testcase_03 AC 1,882 ms
5,048 KB
testcase_04 AC 1,882 ms
5,224 KB
testcase_05 AC 1,882 ms
4,952 KB
testcase_06 AC 1,882 ms
5,140 KB
testcase_07 AC 1,881 ms
4,936 KB
testcase_08 AC 1,882 ms
5,028 KB
testcase_09 AC 1,882 ms
4,988 KB
testcase_10 AC 1,882 ms
5,132 KB
testcase_11 AC 1,882 ms
5,000 KB
testcase_12 AC 1,882 ms
5,020 KB
testcase_13 AC 1,881 ms
5,160 KB
testcase_14 AC 1,883 ms
5,040 KB
testcase_15 AC 1,883 ms
5,124 KB
testcase_16 AC 1,882 ms
4,996 KB
testcase_17 AC 1,882 ms
4,980 KB
testcase_18 AC 1,882 ms
5,012 KB
testcase_19 AC 1,882 ms
4,952 KB
testcase_20 AC 1,882 ms
5,088 KB
testcase_21 AC 1,883 ms
4,992 KB
testcase_22 AC 1,882 ms
5,048 KB
testcase_23 AC 1,882 ms
5,224 KB
testcase_24 AC 1,883 ms
5,008 KB
testcase_25 AC 1,882 ms
5,016 KB
testcase_26 AC 1,882 ms
5,192 KB
testcase_27 AC 1,882 ms
5,084 KB
testcase_28 AC 1,882 ms
4,948 KB
testcase_29 AC 1,883 ms
5,116 KB
testcase_30 AC 1,882 ms
4,952 KB
testcase_31 AC 1,882 ms
4,964 KB
testcase_32 AC 1,882 ms
4,948 KB
testcase_33 AC 1,882 ms
4,968 KB
testcase_34 AC 1,883 ms
4,960 KB
testcase_35 AC 1,882 ms
5,132 KB
testcase_36 AC 1,882 ms
4,960 KB
testcase_37 AC 1,883 ms
5,104 KB
testcase_38 AC 1,881 ms
4,996 KB
testcase_39 AC 1,881 ms
5,028 KB
testcase_40 AC 1,882 ms
4,988 KB
testcase_41 AC 1,882 ms
4,948 KB
testcase_42 AC 1,883 ms
4,992 KB
testcase_43 AC 1,883 ms
4,972 KB
testcase_44 AC 1,882 ms
5,136 KB
testcase_45 AC 1,882 ms
5,160 KB
testcase_46 AC 1,882 ms
4,956 KB
testcase_47 AC 1,883 ms
5,088 KB
testcase_48 AC 1,883 ms
5,124 KB
testcase_49 AC 1,882 ms
4,996 KB
testcase_50 AC 1,881 ms
4,992 KB
testcase_51 AC 1,882 ms
5,084 KB
testcase_52 AC 1,883 ms
4,940 KB
testcase_53 AC 1,883 ms
4,972 KB
testcase_54 AC 1,883 ms
5,012 KB
testcase_55 AC 1,882 ms
5,172 KB
testcase_56 AC 1,881 ms
4,932 KB
testcase_57 AC 1,881 ms
5,128 KB
testcase_58 AC 1,882 ms
5,020 KB
testcase_59 AC 1,883 ms
5,044 KB
testcase_60 AC 1,882 ms
5,004 KB
testcase_61 AC 1,882 ms
5,160 KB
testcase_62 AC 1,882 ms
5,144 KB
testcase_63 AC 1,882 ms
5,088 KB
testcase_64 AC 1,882 ms
4,976 KB
testcase_65 AC 1,883 ms
5,164 KB
testcase_66 AC 1,882 ms
4,952 KB
testcase_67 AC 1,882 ms
5,136 KB
testcase_68 AC 1,883 ms
4,940 KB
testcase_69 AC 1,882 ms
4,992 KB
testcase_70 AC 1,883 ms
4,952 KB
testcase_71 AC 1,883 ms
4,996 KB
testcase_72 AC 1,882 ms
5,148 KB
testcase_73 AC 1,881 ms
4,984 KB
testcase_74 AC 1,882 ms
4,968 KB
testcase_75 AC 1,882 ms
4,980 KB
testcase_76 AC 1,883 ms
5,120 KB
testcase_77 AC 1,883 ms
5,080 KB
testcase_78 AC 1,883 ms
4,948 KB
testcase_79 AC 1,881 ms
4,972 KB
testcase_80 AC 1,882 ms
5,224 KB
testcase_81 AC 1,882 ms
5,128 KB
testcase_82 AC 1,882 ms
5,196 KB
testcase_83 AC 1,882 ms
5,080 KB
testcase_84 AC 1,882 ms
4,948 KB
testcase_85 AC 1,882 ms
5,000 KB
testcase_86 AC 1,883 ms
4,984 KB
testcase_87 AC 1,882 ms
4,996 KB
testcase_88 AC 1,881 ms
4,960 KB
testcase_89 AC 1,882 ms
5,208 KB
testcase_90 AC 1,881 ms
5,092 KB
testcase_91 AC 1,883 ms
5,004 KB
testcase_92 AC 1,882 ms
5,036 KB
testcase_93 AC 1,882 ms
4,996 KB
testcase_94 AC 1,881 ms
5,124 KB
testcase_95 AC 1,882 ms
5,220 KB
testcase_96 AC 1,881 ms
4,968 KB
testcase_97 AC 1,882 ms
5,020 KB
testcase_98 AC 1,882 ms
5,008 KB
testcase_99 AC 1,882 ms
5,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

ll g_start_cycle;
const ll CYCLE_PER_SEC = 2400000000;
double TIME_LIMIT = 1.8;

unsigned long long int get_cycle() {
  unsigned int low, high;
  __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
  return ((unsigned long long int) low) | ((unsigned long long int) high << 32);
}

double get_time(unsigned long long int begin_cycle) {
  return (double) (get_cycle() - begin_cycle) / CYCLE_PER_SEC;
}

unsigned long long xor128() {
  static unsigned long long rx = 123456789, ry = 362436069, rz = 521288629, rw = 88675123;
  unsigned long long rt = (rx ^ (rx << 11));
  rx = ry;
  ry = rz;
  rz = rw;
  return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}

const int QUERY_NUM = 2048;
const int L = 256;
const int BIT_PTN[8][3] = {
  {0, 0, 0},
  {0, 0, 1},
  {0, 1, 0},
  {0, 1, 1},
  {1, 0, 0},
  {1, 0, 1},
  {1, 1, 0},
  {1, 1, 1},
};

struct Query {
  int a;
  int b;
  int c;
  int p;
  int q;
  int r;

  Query(int a = -1, int b = -1, int c = -1, int p = -1, int q = -1, int r = -1) {
    this->a = a;
    this->b = b;
    this->c = c;
    this->p = p;
    this->q = q;
    this->r = r;
  }
};

vector <Query> g_query_list;
vector<int> g_ids[QUERY_NUM];
int g_bit_ptn[L];

class SAT {
public:
  void init() {
    g_start_cycle = get_cycle();

    setup();
  }

  void setup() {
    vector<int> G[L];

    for (int i = 0; i < QUERY_NUM; ++i) {
      Query &query = g_query_list[i];

      G[query.a].push_back(i);
      G[query.b].push_back(i);
      G[query.c].push_back(i);
    }

    for (int i = 0; i < QUERY_NUM; ++i) {
      Query &query = g_query_list[i];
      set<int> ids;

      for (int id : G[query.a]) {
        ids.insert(id);
      }

      for (int id : G[query.b]) {
        ids.insert(id);
      }

      for (int id : G[query.c]) {
        ids.insert(id);
      }

      for (int id : ids) {
        g_ids[i].push_back(id);
      }
    }
  }

  string run() {
    string res = "";

    init();
    search_best_bit_ptn();

    return res;
  }

  vector<int> search_best_bit_ptn() {
    memset(g_bit_ptn, 0, sizeof(g_bit_ptn));

    int best_bit_ptn[L];
    memcpy(best_bit_ptn, g_bit_ptn, sizeof(g_bit_ptn));

    vector<int> res(L, 0);
    int cur_score = calc_score_full();
    int best_score = cur_score;

    double cur_time = get_time(g_start_cycle);
    int try_count = 0;
    double total_diff = 0.0;
    double t = 3.0;
    int R = 500000;

    while (cur_time < TIME_LIMIT) {
      cur_time = get_time(g_start_cycle);
      double remain_time = (TIME_LIMIT - cur_time) / TIME_LIMIT;

      int qid = cur_score;
      Query &q = g_query_list[qid];
      int idx = xor128() % 3;

      flip(qid, idx);
      int score = calc_score_full();
      double diff_score = score - cur_score;
      total_diff += fabs(diff_score);

      if (diff_score > 0 || (xor128() % R < R * exp(diff_score / (t * sqrt(remain_time))))) {
        cur_score = score;

        if (best_score < score) {
          best_score = score;
          memcpy(best_bit_ptn, g_bit_ptn, sizeof(g_bit_ptn));
        }
      } else {
        flip(qid, idx);
      }

      ++try_count;
      double average_diff = total_diff / try_count;
      t = 0.5 * remain_time * average_diff;
    }

    memcpy(g_bit_ptn, best_bit_ptn, sizeof(best_bit_ptn));
    int score = calc_score_full();
    fprintf(stderr, "try_count: %d, best_score: %d\n", try_count, score);

    return res;
  }

  void flip(int qid, int idx) {
    Query &q = g_query_list[qid];

    switch (idx) {
      case 0:
        g_bit_ptn[q.a] ^= 1;
        break;
      case 1:
        g_bit_ptn[q.b] ^= 1;
        break;
      case 2:
        g_bit_ptn[q.c] ^= 1;
        break;
      default:
        assert(false);
    }
  }

  int calc_score_full() {
    int score = 0;

    for (int qid = 0; qid < QUERY_NUM; ++qid) {
      Query &query = g_query_list[qid];

      if (g_bit_ptn[query.a] == query.p || g_bit_ptn[query.b] == query.q || g_bit_ptn[query.c] == query.r) {
        ++score;
      } else {
        break;
      }
    }

    return score;
  }

  void search_best_ptn(int qid) {
    Query &q = g_query_list[qid];
    int best_score = 0;
    int best_ptn_id = 0;

    for (int pid = 0; pid < 8; ++pid) {
      g_bit_ptn[q.a] = BIT_PTN[pid][0];
      g_bit_ptn[q.b] = BIT_PTN[pid][1];
      g_bit_ptn[q.c] = BIT_PTN[pid][2];

      int score = 0;
      for (int id : g_ids[qid]) {
        Query &query = g_query_list[id];

        if (g_bit_ptn[query.a] == query.p || g_bit_ptn[query.b] == query.q || g_bit_ptn[query.c] == query.r) {
          score++;
        } else {
          break;
        }
      }

      if (best_score < score) {
        best_score = score;
        best_ptn_id = pid;
      }
    }

    g_bit_ptn[q.a] = BIT_PTN[best_ptn_id][0];
    g_bit_ptn[q.b] = BIT_PTN[best_ptn_id][1];
    g_bit_ptn[q.c] = BIT_PTN[best_ptn_id][2];
  }
};

int main() {
  int a, b, c, p, q, r;
  for (int i = 0; i < QUERY_NUM; ++i) {
    cin >> a >> b >> c >> p >> q >> r;
    g_query_list.push_back(Query(a, b, c, p, q, r));
  }

  SAT sat;
  sat.run();

  string res = "";
  for (int i = 0; i < L; ++i) {
    res += to_string(g_bit_ptn[i]);
  }

  reverse(res.begin(), res.end());
  cout << res << endl;

  return 0;
}
0