結果

問題 No.1838 Modulo Straight
ユーザー 👑 kmjpkmjp
提出日時 2022-02-12 11:12:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 1,692 bytes
コンパイル時間 2,751 ms
コンパイル使用メモリ 208,080 KB
実行使用メモリ 26,760 KB
最終ジャッジ日時 2023-09-10 22:12:43
合計ジャッジ時間 12,362 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
17,872 KB
testcase_01 AC 10 ms
17,712 KB
testcase_02 AC 10 ms
17,816 KB
testcase_03 AC 11 ms
17,848 KB
testcase_04 AC 11 ms
17,744 KB
testcase_05 AC 11 ms
17,840 KB
testcase_06 AC 11 ms
17,968 KB
testcase_07 AC 11 ms
17,736 KB
testcase_08 AC 11 ms
17,764 KB
testcase_09 AC 11 ms
17,852 KB
testcase_10 AC 12 ms
17,940 KB
testcase_11 AC 174 ms
26,224 KB
testcase_12 AC 176 ms
26,760 KB
testcase_13 AC 177 ms
26,148 KB
testcase_14 AC 178 ms
26,200 KB
testcase_15 AC 175 ms
25,972 KB
testcase_16 AC 160 ms
25,412 KB
testcase_17 AC 163 ms
25,488 KB
testcase_18 AC 160 ms
25,620 KB
testcase_19 AC 153 ms
24,736 KB
testcase_20 AC 154 ms
24,760 KB
testcase_21 AC 155 ms
24,628 KB
testcase_22 AC 125 ms
23,988 KB
testcase_23 AC 123 ms
21,888 KB
testcase_24 AC 111 ms
20,128 KB
testcase_25 AC 114 ms
20,688 KB
testcase_26 AC 117 ms
20,796 KB
testcase_27 AC 125 ms
20,036 KB
testcase_28 AC 126 ms
19,332 KB
testcase_29 AC 128 ms
19,584 KB
testcase_30 AC 129 ms
19,520 KB
testcase_31 AC 133 ms
19,792 KB
testcase_32 AC 135 ms
20,104 KB
testcase_33 AC 155 ms
25,556 KB
testcase_34 AC 156 ms
25,424 KB
testcase_35 AC 146 ms
23,168 KB
testcase_36 AC 139 ms
19,856 KB
testcase_37 AC 131 ms
19,588 KB
testcase_38 AC 127 ms
19,380 KB
testcase_39 AC 41 ms
21,368 KB
testcase_40 AC 10 ms
17,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int M,K;
int num[404040];

template<class V, int ME> class BIT {
public:
	V bit[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { e++; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
};
BIT<int,20> bt;

ll pat[404040];
vector<int> V[404040];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>M>>K;
	ll A=0;
	FOR(i,M*K) {
		cin>>x;
		y=num[x]++;
		V[y].push_back(x);
		A+=bt(1<<19)-bt(y*M+x);
		bt.add(y*M+x,1);
	}
	ZERO(bt.bit);
	
	FOR(i,K) {
		vector<pair<int,int>> P;
		FOR(j,V[i].size()) P.push_back({V[i][j],j});
		sort(ALL(P));
		FORR2(x,j,P) {
			int Lle=bt(j);
			int Lmo=j-bt(j);
			int Rle=bt(M-1)-bt(j);
			int Rmo=(M-1-j)-Rle;
			pat[x+1]+=-Lmo+Rmo-Lle+Rle;
			bt.add(j,1);
		}
		FOR(j,M) bt.add(j,-1);
	}
	
	ll mi=1LL<<60;
	FOR(i,M) {
		if(i) pat[i]+=pat[i-1];
		mi=min(mi,pat[i]);
	}
	
	cout<<(A+mi)<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0