結果

問題 No.5002 stick xor
ユーザー kimiyukikimiyuki
提出日時 2018-05-26 00:51:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 957 ms / 1,000 ms
コード長 4,708 bytes
コンパイル時間 33,757 ms
実行使用メモリ 1,572 KB
スコア 39,537
最終ジャッジ日時 2018-05-26 00:52:29
ジャッジサーバーID
(参考情報)
judge7 /
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 907 ms
1,572 KB
testcase_01 AC 931 ms
1,568 KB
testcase_02 AC 926 ms
1,568 KB
testcase_03 AC 937 ms
1,560 KB
testcase_04 AC 931 ms
1,560 KB
testcase_05 AC 924 ms
1,560 KB
testcase_06 AC 917 ms
1,564 KB
testcase_07 AC 943 ms
1,564 KB
testcase_08 AC 948 ms
1,564 KB
testcase_09 AC 924 ms
1,568 KB
testcase_10 AC 920 ms
1,564 KB
testcase_11 AC 936 ms
1,564 KB
testcase_12 AC 933 ms
1,572 KB
testcase_13 AC 912 ms
1,568 KB
testcase_14 AC 908 ms
1,568 KB
testcase_15 AC 929 ms
1,568 KB
testcase_16 AC 913 ms
1,564 KB
testcase_17 AC 943 ms
1,568 KB
testcase_18 AC 904 ms
1,568 KB
testcase_19 AC 934 ms
1,568 KB
testcase_20 AC 950 ms
1,564 KB
testcase_21 AC 957 ms
1,560 KB
testcase_22 AC 948 ms
1,568 KB
testcase_23 AC 906 ms
1,572 KB
testcase_24 AC 916 ms
1,572 KB
testcase_25 AC 925 ms
1,568 KB
testcase_26 AC 939 ms
1,564 KB
testcase_27 AC 909 ms
1,564 KB
testcase_28 AC 929 ms
1,568 KB
testcase_29 AC 932 ms
1,564 KB
testcase_30 AC 948 ms
1,564 KB
testcase_31 AC 935 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;
    }
}

template <class RandomEngine>
array<tuple<int, int, bool>, K> solve1(array<int, K> const & l, bitset<N2> & a, RandomEngine & gen) {
    array<vector<int>, N + 1> lookup;
    REP (i, K) lookup[l[i]].push_back(i);
    array<uint64_t, N2> priority_table;
    REP (z, N2) priority_table[z] = gen();
    array<tuple<int, int, bool>, K> result;
    REP_R (len, N + 1) {
        for (int i : lookup[len]) {
            int highscore = -1, priority = 0;
            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 or (highscore == score and priority < priority_table[y * N + x])) {
                    highscore = score;
                    priority = priority_table[y * N + x];
                    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> const & a) {
    chrono::high_resolution_clock::time_point clock_begin = chrono::high_resolution_clock::now();
    default_random_engine gen;
    array<tuple<int, int, bool>, K> result;
    int highscore = -1;
    int iteration = 0;
    for (; ; iteration ++) {
        chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now();
        if (chrono::duration_cast<chrono::milliseconds>(clock_end - clock_begin).count() >= 900) break;
        auto na = a;
        auto it = solve1(l, na, gen);
        int score = na.size() - na.count();
        if (highscore < score) {
            highscore = score;
            result = it;
        }
cerr << "score = " << score << " / " << N2 << endl;  // it seems to have failed
    }
    cerr << "iteration = " << iteration << endl;
    cerr << "score = " << 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