結果

問題 No.5002 stick xor
ユーザー merom686merom686
提出日時 2018-05-26 03:51:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 1,000 ms
コード長 2,479 bytes
コンパイル時間 2,495 ms
実行使用メモリ 1,544 KB
スコア 39,729
最終ジャッジ日時 2018-05-26 03:51:16
ジャッジサーバーID
(参考情報)
judge6 /
純コード判定しない問題か言語
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <array>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct BB {
    BB(int n) : n(n) {
        for (int d = 0; d < 2; d++) {
            for (int i = 0; i < n; i++) {
                b[d][i] = 0;
            }
        }
    }
    int search(int l, int& i0, int& j0, int& d0) {
        int m = -1;
        for (int d = 0; d < 2; d++) {
            for (int i = 0; i < n; i++) {
                ll a = (1LL << l) - 1;
                for (int j = 0; j <= n - l; j++) {
                    int c = __builtin_popcountll(b[d][i] & a);
                    if (c > m) {
                        m = c;
                        i0 = i;
                        j0 = j;
                        d0 = d;
                    }
                    a <<= 1;
                }
            }
        }
        for (int h = 0; h < l; h++) {
            xor1(i0, j0 + h, d0);
        }
        if (d0) swap(i0, j0);
        return m;
    }
    void xor1(int i, int j, int d = 0) {
        b[d    ][i] ^= 1LL << j;
        b[d ^ 1][j] ^= 1LL << i;
    }
    int count() {
        int s = 0;
        for (int i = 0; i < n; i++) {
            s += __builtin_popcountll(b[0][i]);
        }
        return s;
    }
    int n;
    ll b[2][60];
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;

    vector<pair<int, int>> l(k);
    for (int h = 0; h < k; h++) {
        cin >> l[h].first;
        l[h].second = h;
    }

    BB bb(n);
    for (int i = 0; i < n; i++) {
        char a[61];
        cin >> a;
        for (int j = 0; j < n; j++) {
            if (a[j] == '1') bb.xor1(i, j);
        }
    }

    int s0 = bb.count();

    sort(l.begin(), l.end());
    reverse(l.begin(), l.end());

    vector<array<int, 4>> r(k);

    int s = 0;
    for (int h = 0; h < k; h++) {
        int i, j, d;
        int l0 = l[h].first;
        int m = bb.search(l0, i, j, d);
        auto& a = r[l[h].second];
        a = { i, j, i, j };
        a[0]++;
        a[1]++;
        a[2 + d] += 1;
        a[2 + (d ^ 1)] += l0;
        s += m - (l0 - m);
    }

    int s1 = bb.count();

    if (s != s0 - s1) {
        cout << s << ' ' << s0 - s1 << endl;
        throw;
    }

    for (int h = 0; h < k; h++) {
        for (int i = 0; i < 4; i++) {
            cout << r[h][i] << " \n"[i == 3];
        }
    }

    return 0;
}
0