結果
問題 | No.5002 stick xor |
ユーザー | kimiyuki |
提出日時 | 2018-05-26 01:41:27 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 967 ms / 1,000 ms |
コード長 | 5,723 bytes |
コンパイル時間 | 35,144 ms |
実行使用メモリ | 1,548 KB |
スコア | 39,587 |
最終ジャッジ日時 | 2018-05-26 01:42:04 |
ジャッジサーバーID (参考情報) |
judge9 / |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 965 ms
1,548 KB |
testcase_01 | AC | 963 ms
1,544 KB |
testcase_02 | AC | 958 ms
1,540 KB |
testcase_03 | AC | 956 ms
1,540 KB |
testcase_04 | AC | 963 ms
1,544 KB |
testcase_05 | AC | 957 ms
1,536 KB |
testcase_06 | AC | 959 ms
1,544 KB |
testcase_07 | AC | 961 ms
1,544 KB |
testcase_08 | AC | 955 ms
1,544 KB |
testcase_09 | AC | 964 ms
1,540 KB |
testcase_10 | AC | 966 ms
1,532 KB |
testcase_11 | AC | 962 ms
1,544 KB |
testcase_12 | AC | 959 ms
1,544 KB |
testcase_13 | AC | 955 ms
1,540 KB |
testcase_14 | AC | 959 ms
1,544 KB |
testcase_15 | AC | 958 ms
1,548 KB |
testcase_16 | AC | 957 ms
1,544 KB |
testcase_17 | AC | 964 ms
1,544 KB |
testcase_18 | AC | 967 ms
1,540 KB |
testcase_19 | AC | 963 ms
1,548 KB |
testcase_20 | AC | 956 ms
1,536 KB |
testcase_21 | AC | 962 ms
1,540 KB |
testcase_22 | AC | 963 ms
1,540 KB |
testcase_23 | AC | 963 ms
1,544 KB |
testcase_24 | AC | 957 ms
1,544 KB |
testcase_25 | AC | 956 ms
1,544 KB |
testcase_26 | AC | 958 ms
1,536 KB |
testcase_27 | AC | 963 ms
1,540 KB |
testcase_28 | AC | 955 ms
1,544 KB |
testcase_29 | AC | 960 ms
1,540 KB |
testcase_30 | AC | 956 ms
1,540 KB |
testcase_31 | AC | 957 ms
1,540 KB |
ソースコード
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < int(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i)) #define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = int(n) - 1; (i) >= int(m); -- (i)) #define ALL(x) begin(x), end(x) #define dump(x) cerr << #x " = " << x << endl #define unittest_name_helper(counter) unittest_ ## counter #define unittest_name(counter) unittest_name_helper(counter) #define unittest __attribute__((constructor)) void unittest_name(__COUNTER__) () using ll = long long; using namespace std; template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >; template <class T> inline void chmax(T & a, T const & b) { a = max(a, b); } template <class T> inline void chmin(T & a, T const & b) { a = min(a, b); } template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); } template <typename T> ostream & operator << (ostream & out, vector<T> const & xs) { REP (i, int(xs.size()) - 1) out << xs[i] << ' '; if (not xs.empty()) out << xs.back(); return out; } constexpr int N = 60; constexpr int K = 500; constexpr int MAX_L = 25; constexpr int N2 = N * N; int count_black(int y, int x, bool is_vertical, int len, bitset<N2> const & a) { int cnt = 0; REP (i, len) { int ny = y + (is_vertical ? i : 0); int nx = x + (is_vertical ? 0 : i); cnt += a[ny * N + nx]; } return cnt; } void apply_rect(int y, int x, bool is_vertical, int len, bitset<N2> & a) { REP (i, len) { int ny = y + (is_vertical ? i : 0); int nx = x + (is_vertical ? 0 : i); auto && p = a[ny * N + nx]; p = not p; } } array<tuple<int, int, bool>, K> solve_greedy(array<int, K> const & l, bitset<N2> & a) { array<vector<int>, N + 1> lookup; REP (i, K) lookup[l[i]].push_back(i); array<tuple<int, int, bool>, K> result; REP_R (len, N + 1) { for (int i : lookup[len]) { int highscore = -1; REP (y, N) REP (x, N) REP (is_vertical, 2) { if ( is_vertical and y + len > N) continue; if (not is_vertical and x + len > N) continue; int score = count_black(y, x, is_vertical, l[i], a); if (highscore < score) { highscore = score; result[i] = make_tuple(y, x, is_vertical); } } assert (highscore != -1); int y, x; bool is_vertical; tie(y, x, is_vertical) = result[i]; apply_rect(y, x, is_vertical, l[i], a); } } return result; } array<tuple<int, int, bool>, K> solve(array<int, K> const & l, bitset<N2> a) { const auto TLE = chrono::microseconds(1000 * 1000 - 50 * 1000); chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now() + TLE; default_random_engine gen; array<tuple<int, int, bool>, K> result = solve_greedy(l, a); array<tuple<int, int, bool>, K> rects = result; int highscore = a.count(); int score = highscore; int iteration = 0; double temperature = 1.0; for (; ; iteration ++) { if (iteration % 100 == 0) { temperature = (clock_end - chrono::high_resolution_clock::now()).count() / (double)TLE.count(); if (temperature < 0) break; } int i = uniform_int_distribution<int>(0, K - 1)(gen); int y1, x1; bool is_vertical1; tie(y1, x1, is_vertical1) = rects[i]; apply_rect(y1, x1, is_vertical1, l[i], a); int black1 = count_black(y1, x1, is_vertical1, l[i], a); int y2, x2; bool is_vertical2; int black2 = -1; REP (y, N) REP (x, N) REP (is_vertical, 2) { if ( is_vertical and y + l[i] > N) continue; if (not is_vertical and x + l[i] > N) continue; int black = count_black(y, x, is_vertical, l[i], a); if (black2 < black) { black2 = black; y2 = y; x2 = x; is_vertical2 = is_vertical; } } int delta = black2 - black1; if (black1 <= black2 or bernoulli_distribution(exp(delta) / temperature)(gen)) { rects[i] = make_tuple(y1, x1, is_vertical1); score -= l[i] - 2 * black1; apply_rect(y2, x2, is_vertical2, l[i], a); score += l[i] - 2 * black2; assert (score == a.count()); if (score < highscore) { highscore = score; result = rects; cerr << "black = " << highscore << " / " << N2 << endl; } } else { apply_rect(y1, x1, is_vertical1, l[i], a); } } cerr << "iteration = " << iteration << endl; cerr << "black = " << highscore << " / " << N2 << endl; return result; } int main() { // input { int n; cin >> n; assert (n == N); } { int k; cin >> k; assert (k == K); } array<int, K> l; REP (i, K) cin >> l[i]; bitset<N2> a; REP (y, N) REP (x, N) { char c; cin >> c; a[y * N + x] = c - '0'; } // solve array<tuple<int, int, bool>, K> result = solve(l, a); // output REP (i, K) { int y, x; bool is_vertical; tie(y, x, is_vertical) = result[i]; int ny = y + (is_vertical ? l[i] - 1 : 0); int nx = x + (is_vertical ? 0 : l[i] - 1); cout << y + 1 << ' ' << x + 1 << ' ' << ny + 1 << ' ' << nx + 1 << endl; } return 0; }