結果

問題 No.1838 Modulo Straight
ユーザー kwm_tkwm_t
提出日時 2022-02-12 11:38:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 1,902 bytes
コンパイル時間 4,808 ms
コンパイル使用メモリ 263,728 KB
実行使用メモリ 32,324 KB
最終ジャッジ日時 2023-09-10 22:42:43
合計ジャッジ時間 13,529 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 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,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 321 ms
32,172 KB
testcase_12 AC 317 ms
32,280 KB
testcase_13 AC 314 ms
32,324 KB
testcase_14 AC 310 ms
32,208 KB
testcase_15 AC 308 ms
32,244 KB
testcase_16 AC 272 ms
21,140 KB
testcase_17 AC 283 ms
21,136 KB
testcase_18 AC 269 ms
21,128 KB
testcase_19 AC 254 ms
17,544 KB
testcase_20 AC 260 ms
17,516 KB
testcase_21 AC 252 ms
17,516 KB
testcase_22 AC 93 ms
11,884 KB
testcase_23 AC 95 ms
12,000 KB
testcase_24 AC 90 ms
12,236 KB
testcase_25 AC 110 ms
12,496 KB
testcase_26 AC 123 ms
12,776 KB
testcase_27 AC 146 ms
13,264 KB
testcase_28 AC 151 ms
12,504 KB
testcase_29 AC 162 ms
13,292 KB
testcase_30 AC 173 ms
13,552 KB
testcase_31 AC 193 ms
14,052 KB
testcase_32 AC 212 ms
15,136 KB
testcase_33 AC 253 ms
21,132 KB
testcase_34 AC 255 ms
21,144 KB
testcase_35 AC 226 ms
15,704 KB
testcase_36 AC 204 ms
14,832 KB
testcase_37 AC 175 ms
12,728 KB
testcase_38 AC 156 ms
12,460 KB
testcase_39 AC 47 ms
11,772 KB
testcase_40 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
int op(int a, int b) { return a + b; }
int e() { return 0; }
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int m, k; cin >> m >> k;
	vector<int>v(m * k);
	vector<vector<int>>pos(m);
	rep(i, m*k) {
		int a; cin >> a;
		pos[a].push_back(i);
	}
	rep(i, m) {
		rep(j, k) {
			int idx = m * j + i;
			v[idx] = pos[i][j];
		}
	}
	long long memo = 0;
	{
		segtree<int, op, e>seg(m * k);
		rep(i, v.size()) {
			memo += seg.prod(v[i], m * k);
			seg.set(v[i], 1);
		}
	}
	long long ans = memo;
	vector<int>v2(m * k);
	rep(i, k) {
		//set<int>st;
		vector<int>vec;
		rep(j, m) {
			int idx = i * m + j;
			//st.insert(v[idx]);
			vec.push_back(v[idx]);
		}
		sort(all(vec));
		rep(j, m) {
			int idx = i * m + j;
			//v2[idx] = st.lower_bound(v[idx]) - st.begin();
			v2[idx] = lower_bound(all(vec), v[idx]) - vec.begin();
		}
	}
	//rep(i, v2.size())cout << v2[i] << " ";
	//cout << endl;
	rep(i, m - 1) {
		rep(j, k) {
			int idx = m * j + i;
			int x = v2[idx];
			memo -= x;
			memo += (m - 1) - x;
		}
		chmin(ans, memo);
	}
	cout << ans << endl;
	return 0;
}
0