結果

問題 No.1838 Modulo Straight
ユーザー 👑 hitonanodehitonanode
提出日時 2021-12-19 17:24:53
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,442 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 106,464 KB
実行使用メモリ 35,768 KB
最終ジャッジ日時 2023-10-13 18:54:55
合計ジャッジ時間 5,521 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,372 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 3 ms
4,356 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
4,352 KB
testcase_08 WA -
testcase_09 AC 5 ms
4,352 KB
testcase_10 WA -
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// 嘘三分探索
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#include <atcoder/fenwicktree>

int main() {
    int M, K;
    cin >> M >> K;
    const int N = M * K;
    vector<int> A(N);
    vector<int> ord(N);
    vector<int> freq(M);
    vector<vector<int>> v2i(M);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
        ord[i] = A[i] + freq[A[i]] * M;
        v2i[A[i]].push_back(i);
        freq[A[i]]++;
    }

    auto eval = [&](int head_value) -> long long {
        atcoder::fenwick_tree<int> bit(N);
        long long ret = 0;
        for (int k = 0; k < K; ++k) {
            for (int i = 0; i < M; ++i) {
                int j = head_value + i;
                if (j >= M) j -= M;
                int x = v2i[j][k];
                ret += bit.sum(x, N);
                bit.add(x, 1);
            }
        }
        return ret;
    };
    int l = 0, r = M - 1;
    long long vl = eval(l), vr = eval(r);

    long long ret = min(vl, vr);
    while (l + 5 < r) {
        int cl = (l * 2 + r) / 3, cr = (l + r * 2) / 3;
        auto ecl = eval(cl), ecr = eval(cr);
        ret = min({ret, ecl, ecr});
        if (ecl < ecr) {
            r = cr;
            vr = ecr;
        } else {
            l = cl;
            vl = ecl;
        }
    }
    for (int i = l; i <= r; ++i) ret = min(ret, eval(i));

    cout << ret << '\n';
}
0