結果

問題 No.5002 stick xor
ユーザー masaktmasakt
提出日時 2018-05-26 15:55:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 21 ms / 1,000 ms
コード長 3,080 bytes
コンパイル時間 4,639 ms
実行使用メモリ 1,624 KB
スコア 40,509
最終ジャッジ日時 2018-05-26 15:55:50
ジャッジサーバーID
(参考情報)
judge9 /
純コード判定しない問題か言語
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

constexpr double TIME_LIMIT = 0.9;
constexpr double CYCLES_PER_SEC_INV = 1.0 / (2.8 * 1e9);
constexpr double XORSHIFT_INV = 1.0 / (1ULL << 32);

double getTime() {
    uint32_t low, high;
    __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
    uint64_t cycle = ((uint64_t)low) | ((uint64_t)high << 32);
    return cycle * CYCLES_PER_SEC_INV;
}

double getRand() {
    static uint32_t x = 123456789, y = 362436069,
        z = 521288629 ^ ((uint64_t)(getTime() / CYCLES_PER_SEC_INV)),
        w = 88675123 ^ ((uint64_t)(getTime() / CYCLES_PER_SEC_INV));
    uint32_t t = x ^ (x << 11); x = y; y = z; z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w * XORSHIFT_INV;
}

constexpr int MAX_N = 60;
int n;
int k;
vector<int> l;
vector<bitset<MAX_N>> bsL;
vector<bitset<MAX_N>> rowA;
vector<bitset<MAX_N>> colA;
vector<array<int, 4>> states;

void input() {
    cin >> n >> k;
    l.resize(k);
    bsL.resize(k);
    for (int i = 0; i < k; i++) {
        cin >> l[i];
        for (int j = 0; j < l[i]; j++) {
            bsL[i][j] = 1;
        }
    }
    rowA.resize(n);
    colA.resize(n);
    for (int r = 0; r < n; r++) {
        string t;
        cin >> t;
        reverse(t.begin(), t.end());
        rowA[r] = bitset<MAX_N>(t);
    }
    for (int r = 0; r < n; r++) {
        for (int c = 0; c < n; c++) {
            colA[c][r] = rowA[r][c];
        }
    }
}

void greedy() {
    double startTime = getTime();
    
    for (int i = 0; i < k; i++) {
        int idx = (int)(getRand() * n);
        int bestDiff = 0;
        array<int, 4> bestState = {0, 0, (l[i] - 1), 0};
        for (int r = 0; r < n; r++) {
            for (int c = 0; c < n - (l[i] - 1); c++) {
                auto diff = rowA[r] & (bsL[i] << c);
                if (bestDiff < diff.count()) {
                    bestDiff = diff.count();
                    bestState = {r, c, r, c + (l[i] - 1)};
                }
                if (l[i] <= diff.count()) {
                    break;
                }
            }
        }
        for (int c = 0; c < n; c++) {
            for (int r = 0; r < n - (l[i] - 1); r++) {
                auto diff = colA[c] & (bsL[i] << r);
                if (bestDiff < diff.count()) {
                    bestDiff = diff.count();
                    bestState = {r, c, r + (l[i] - 1), c};
                }
                if (l[i] <= diff.count()) {
                    break;
                }
            }
        }
        
        states.emplace_back(bestState);
        for (int r = bestState[0]; r <= bestState[2]; r++) {
            for (int c = bestState[1]; c <= bestState[3]; c++) {
                rowA[r].flip(c);
                colA[c].flip(r);
            }
        }
    }
    
    fprintf(stderr, "greedy time:%f\n", getTime() - startTime);
}

void output() {
    for (int i = 0; i < k; i++) {
        for (int j = 0; j < 4; j++) {
            cout << (states[i][j] + 1) << (j < 3 ? ' ' : '\n');
        }
    }
}

int main() {
    input();
    greedy();
    output();
    return 0;
}
0