結果

問題 No.1838 Modulo Straight
ユーザー SSRSSSRS
提出日時 2022-02-11 22:31:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,456 bytes
コンパイル時間 3,108 ms
コンパイル使用メモリ 206,612 KB
実行使用メモリ 18,080 KB
最終ジャッジ日時 2023-09-10 04:19:25
合計ジャッジ時間 8,758 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 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 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 108 ms
18,080 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 41 ms
6,516 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++){
    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 += -B[i][j] + (M - 1 - B[i][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