結果

問題 No.5002 stick xor
ユーザー kimiyukikimiyuki
提出日時 2018-05-26 01:31:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 955 ms / 1,000 ms
コード長 5,499 bytes
コンパイル時間 35,102 ms
実行使用メモリ 1,576 KB
スコア 41,355
最終ジャッジ日時 2018-05-26 01:32:30
ジャッジサーバーID
(参考情報)
judge10 /
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 954 ms
1,576 KB
testcase_01 AC 955 ms
1,572 KB
testcase_02 AC 954 ms
1,568 KB
testcase_03 AC 954 ms
1,568 KB
testcase_04 AC 955 ms
1,568 KB
testcase_05 AC 954 ms
1,572 KB
testcase_06 AC 955 ms
1,572 KB
testcase_07 AC 955 ms
1,572 KB
testcase_08 AC 955 ms
1,572 KB
testcase_09 AC 954 ms
1,568 KB
testcase_10 AC 954 ms
1,568 KB
testcase_11 AC 955 ms
1,564 KB
testcase_12 AC 954 ms
1,576 KB
testcase_13 AC 955 ms
1,568 KB
testcase_14 AC 955 ms
1,568 KB
testcase_15 AC 954 ms
1,568 KB
testcase_16 AC 954 ms
1,568 KB
testcase_17 AC 954 ms
1,572 KB
testcase_18 AC 954 ms
1,568 KB
testcase_19 AC 954 ms
1,572 KB
testcase_20 AC 954 ms
1,564 KB
testcase_21 AC 955 ms
1,560 KB
testcase_22 AC 954 ms
1,568 KB
testcase_23 AC 954 ms
1,572 KB
testcase_24 AC 954 ms
1,572 KB
testcase_25 AC 954 ms
1,568 KB
testcase_26 AC 955 ms
1,568 KB
testcase_27 AC 955 ms
1,568 KB
testcase_28 AC 955 ms
1,572 KB
testcase_29 AC 955 ms
1,572 KB
testcase_30 AC 955 ms
1,568 KB
testcase_31 AC 954 ms
1,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
        bool is_vertical1 = bernoulli_distribution(0.5)(gen);
        int y1 = uniform_int_distribution<int>(0, (N - 1) - (is_vertical1 ? l[i] : 0))(gen);
        int x1 = uniform_int_distribution<int>(0, (N - 1) - (is_vertical1 ? 0 : l[i]))(gen);
        int y, x; bool is_vertical; tie(y, x, is_vertical) = rects[i];
        apply_rect(y, x, is_vertical, l[i], a);
        int black = count_black(y, x, is_vertical, l[i], a);
        int black1 = count_black(y1, x1, is_vertical1, l[i], a);
        int delta = black1 - black;
        if (black <= black1 or bernoulli_distribution(0.01 * exp(delta) / temperature)(gen)) {
            rects[i] = make_tuple(y1, x1, is_vertical1);
            score -= l[i] - 2 * black;
            apply_rect(y1, x1, is_vertical1, l[i], a);
            score += l[i] - 2 * black1;
            if (score < highscore) {
                highscore = score;
                result = rects;
                cerr << "black = " << highscore << " / " << N2 << endl;
            }
        } else {
            apply_rect(y, x, is_vertical, 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;
}
0