結果

問題 No.1838 Modulo Straight
ユーザー SSRSSSRS
提出日時 2022-02-11 22:34:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,549 bytes
コンパイル時間 2,211 ms
コンパイル使用メモリ 205,868 KB
実行使用メモリ 17,616 KB
最終ジャッジ日時 2023-09-10 04:25:47
合計ジャッジ時間 8,075 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 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 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 146 ms
15,692 KB
testcase_12 AC 147 ms
15,768 KB
testcase_13 AC 147 ms
15,740 KB
testcase_14 AC 147 ms
15,616 KB
testcase_15 AC 146 ms
15,500 KB
testcase_16 AC 140 ms
11,596 KB
testcase_17 AC 141 ms
11,712 KB
testcase_18 AC 142 ms
11,652 KB
testcase_19 AC 137 ms
10,252 KB
testcase_20 AC 136 ms
10,452 KB
testcase_21 AC 137 ms
10,448 KB
testcase_22 AC 110 ms
17,616 KB
testcase_23 AC 107 ms
13,780 KB
testcase_24 AC 93 ms
9,572 KB
testcase_25 AC 95 ms
9,328 KB
testcase_26 AC 100 ms
9,376 KB
testcase_27 AC 108 ms
8,860 KB
testcase_28 AC 110 ms
8,056 KB
testcase_29 AC 115 ms
8,392 KB
testcase_30 AC 117 ms
8,216 KB
testcase_31 AC 125 ms
8,564 KB
testcase_32 AC 122 ms
8,808 KB
testcase_33 AC 139 ms
11,668 KB
testcase_34 AC 140 ms
11,752 KB
testcase_35 AC 132 ms
9,892 KB
testcase_36 AC 129 ms
8,884 KB
testcase_37 AC 120 ms
8,256 KB
testcase_38 AC 114 ms
8,016 KB
testcase_39 AC 41 ms
7,004 KB
testcase_40 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
template <typename T>
struct binary_indexed_tree{
  int N;
  vector<T> BIT;
  binary_indexed_tree(){
  }
  binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
  }
  void add(int i, T x){
    i++;
    while (i <= N){
      BIT[i] += x;
      i += i & -i;
    }
  }
  T sum(int i){
    T ans = 0;
    while (i > 0){
      ans += BIT[i];
      i -= i & -i;
    }
    return ans;
  }
  T sum(int L, int R){
    return sum(R) - sum(L);
  }
};
int main(){
  int M, K;
  cin >> M >> K;
  vector<int> A(M * K);
  for (int i = 0; i < M * K; i++){
    cin >> A[i];
  }
  vector<int> cnt(M, 0);
  vector<int> p(M * K);
  vector<vector<int>> B(K);
  for (int i = 0; i < M * K; i++){
    p[i] = cnt[A[i]];
    B[cnt[A[i]]].push_back(A[i]);
    cnt[A[i]]++;
  }
  long long ans1 = 0;
  binary_indexed_tree<int> BIT1(K);
  for (int i = 0; i < M * K; i++){
    ans1 += BIT1.sum(p[i] + 1, K);
    BIT1.add(p[i], 1);
  }
  vector<long long> op(M, 0);
  for (int i = 0; i < K; i++){
    vector<int> pos(M);
    for (int j = 0; j < M; j++){
      pos[B[i][j]] = j;
    }
    long long C = 0;
    binary_indexed_tree<int> BIT2(M); 
    for (int j = 0; j < M; j++){
      C += BIT2.sum(B[i][j] + 1, M);
      BIT2.add(B[i][j], 1);
    }
    op[0] += C;
    for (int j = 0; j < M - 1; j++){
      C -= pos[j];
      C += M - 1 - pos[j];
      op[j + 1] += C;
    }
  }
  long long ans2 = INF;
  for (int i = 0; i < M; i++){
    ans2 = min(ans2, op[i]);
  }
  cout << ans1 + ans2 << endl;
}
0