結果

問題 No.1838 Modulo Straight
ユーザー 👑 NachiaNachia
提出日時 2022-02-11 21:52:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,556 bytes
コンパイル時間 957 ms
コンパイル使用メモリ 89,592 KB
実行使用メモリ 43,092 KB
最終ジャッジ日時 2023-09-10 02:06:37
合計ジャッジ時間 7,867 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 276 ms
42,364 KB
testcase_12 AC 279 ms
43,052 KB
testcase_13 AC 278 ms
41,844 KB
testcase_14 AC 281 ms
43,092 KB
testcase_15 AC 276 ms
41,712 KB
testcase_16 AC 220 ms
23,848 KB
testcase_17 AC 218 ms
23,844 KB
testcase_18 AC 222 ms
23,780 KB
testcase_19 AC 187 ms
18,644 KB
testcase_20 AC 201 ms
18,720 KB
testcase_21 AC 198 ms
18,696 KB
testcase_22 AC 90 ms
10,928 KB
testcase_23 AC 80 ms
11,084 KB
testcase_24 AC 64 ms
11,220 KB
testcase_25 AC 75 ms
11,472 KB
testcase_26 AC 82 ms
11,692 KB
testcase_27 AC 95 ms
12,264 KB
testcase_28 AC 98 ms
11,924 KB
testcase_29 AC 108 ms
12,364 KB
testcase_30 AC 118 ms
12,636 KB
testcase_31 AC 134 ms
13,136 KB
testcase_32 AC 158 ms
13,868 KB
testcase_33 AC 204 ms
23,780 KB
testcase_34 AC 205 ms
23,784 KB
testcase_35 AC 178 ms
15,008 KB
testcase_36 AC 150 ms
13,852 KB
testcase_37 AC 120 ms
11,768 KB
testcase_38 AC 103 ms
11,536 KB
testcase_39 AC 36 ms
14,664 KB
testcase_40 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/fenwicktree>
using namespace std;


using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned int;
#define rep(i,n) for(int i=0; i<(int)(n); i++)


i64 inversion(vector<int> P){
    int n = P.size();
    atcoder::fenwick_tree<int> G(n);
    i64 ans = 0;
    for(auto p : P){
        ans += G.sum(p+1, n);
        G.add(p, 1);
    }
    return ans;
}

vector<i64> rot_inversion(vector<int> A){
    int n = A.size();
    vector<int> X(n); rep(i,n) X[i] = i;
    sort(X.begin(), X.end(), [&](int l, int r)->bool{ return A[l] < A[r]; });
    rep(i,n) A[X[i]] = i;
    vector<i64> res;
    res.push_back(inversion(A));
    rep(i,n) res.push_back(res.back() + (n-1-2*A[i]));
    return res;
}


int main() {
    int N, P; cin >> P >> N;
    vector<int> A(N*P);
    vector<vector<int>> pos(P);
    rep(i,N*P){
        int a; cin >> a;
        A[i] = a;
        pos[a].push_back(i);
    }

    vector<int> tA(N*P);
    rep(i,P) rep(j,N) tA[pos[i][j]] = j;
    i64 off = inversion(tA);

    vector<i64> dInv(P, off);
    rep(i,N){
        vector<int> subA(P);
        rep(j,P) subA[j] = pos[j][i];
        auto tmp = rot_inversion(move(subA));
        rep(j,P) dInv[j] += tmp[j];
    }

    i64 ans = (i64)N*P*N*P;
    rep(i,P) ans = min(ans, dInv[i]);
    cout << ans << "\n";

    return 0;
}




struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0