結果
問題 | No.1838 Modulo Straight |
ユーザー |
![]() |
提出日時 | 2022-02-11 22:31:59 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,456 bytes |
コンパイル時間 | 2,338 ms |
コンパイル使用メモリ | 200,684 KB |
最終ジャッジ日時 | 2025-01-27 21:50:29 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 4 WA * 34 |
ソースコード
#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; }