結果

問題 No.1838 Modulo Straight
ユーザー milanis48663220milanis48663220
提出日時 2022-02-11 23:14:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 745 ms / 2,000 ms
コード長 3,552 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 133,956 KB
実行使用メモリ 60,220 KB
最終ジャッジ日時 2023-09-10 06:03:22
合計ジャッジ時間 14,836 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 745 ms
60,152 KB
testcase_12 AC 718 ms
60,164 KB
testcase_13 AC 742 ms
60,100 KB
testcase_14 AC 717 ms
60,220 KB
testcase_15 AC 740 ms
60,100 KB
testcase_16 AC 550 ms
37,616 KB
testcase_17 AC 568 ms
37,668 KB
testcase_18 AC 533 ms
37,540 KB
testcase_19 AC 460 ms
33,116 KB
testcase_20 AC 477 ms
33,060 KB
testcase_21 AC 469 ms
33,048 KB
testcase_22 AC 126 ms
33,624 KB
testcase_23 AC 120 ms
28,052 KB
testcase_24 AC 96 ms
18,812 KB
testcase_25 AC 143 ms
19,832 KB
testcase_26 AC 181 ms
20,080 KB
testcase_27 AC 202 ms
19,012 KB
testcase_28 AC 205 ms
15,888 KB
testcase_29 AC 235 ms
18,756 KB
testcase_30 AC 255 ms
19,644 KB
testcase_31 AC 288 ms
21,164 KB
testcase_32 AC 355 ms
21,904 KB
testcase_33 AC 544 ms
37,668 KB
testcase_34 AC 546 ms
37,540 KB
testcase_35 AC 397 ms
26,088 KB
testcase_36 AC 348 ms
23,044 KB
testcase_37 AC 259 ms
17,776 KB
testcase_38 AC 225 ms
16,112 KB
testcase_39 AC 80 ms
20,168 KB
testcase_40 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

template <typename T>
struct bit{
    int n;
    vector<T> data;

    bit(int n_){
        n = 1;
        while(n < n_) n *= 2;
        data = vector<T>(n+1);
        for(int i = 0; i <= n; i++) data[i] = 0;
    }
    
    T sum(int i){
        T ret = 0;
        while(i > 0){
            ret += data[i];
            i -= i&-i;
        }
        return ret;
    }
    T all(){
        return sum(n);
    }

    void add(int i, T x){
        while(i <= n){
            data[i] += x;
            i += i&-i;
        }
    }
};

template<typename T>
class Compress{
    public:
    vector<T> data;
    int offset;
    Compress(vector<T> data_, int offset=0): offset(offset){
        set<T> st;
        for(T x: data_) st.insert(x);
        for(T x: st) data.push_back(x);
    };
    int operator[](T x) {
        auto p = lower_bound(data.begin(), data.end(), x);
        assert(x == *p);
        return offset+(p-data.begin());
	}
    T inv(int x){
        return data[x-offset];
    }
    int size(){
        return data.size();
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int m, k; cin >> m >> k;
    vector<int> a(m*k);
    vector<vector<int>> idx(m);

    for(int i = 0; i < m*k; i++) {
        cin >> a[i];
        idx[a[i]].push_back(i+1);
    }
    ll ans = 0;
    bit<ll> bt(k*m);
    for(int i = 0; i < k; i++){
        for(int j = 0; j < m; j++){
            ans += bt.all() - bt.sum(idx[j][i]);
            bt.add(idx[j][i], 1); 
        }
    }
    // debug_value(ans);
    ll tmp = ans;
    vector<bit<ll>> bts(k, bit<ll>(m));
    vector<vector<int>> idx_cp(k);
    for(int i = 0; i < k; i++){
        vector<int> v(m);
        for(int j = 0; j < m; j++){
            v[j] = idx[j][i];
        }
        auto cp = Compress<int>(v, 1);
        for(int j = 0; j < m; j++){
            idx_cp[i].push_back(cp[v[j]]);
        }
        for(int j = 0; j < m; j++){
            bts[i].add(idx_cp[i][j], 1);
        }
    }
    for(int j = 0; j < m; j++){
        for(int i = 0; i < k; i++){
            // jを一番うしろに持ってくる
            bts[i].add(idx_cp[i][j], -1);
            tmp -= bts[i].sum(idx_cp[i][j]);
            tmp += bts[i].all() - bts[i].sum(idx_cp[i][j]);
            bts[i].add(idx_cp[i][j], 1);
        }
        chmin(ans, tmp);
    }
    cout << ans << endl;
}
0