結果

問題 No.2304 Distinct Elements
ユーザー 👑 kmjpkmjp
提出日時 2023-05-12 23:36:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 287 ms / 3,000 ms
コード長 1,775 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 209,396 KB
実行使用メモリ 46,532 KB
最終ジャッジ日時 2023-08-19 06:24:08
合計ジャッジ時間 11,846 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
22,292 KB
testcase_01 AC 8 ms
22,288 KB
testcase_02 AC 7 ms
22,320 KB
testcase_03 AC 6 ms
22,468 KB
testcase_04 AC 204 ms
41,844 KB
testcase_05 AC 211 ms
41,920 KB
testcase_06 AC 199 ms
41,864 KB
testcase_07 AC 198 ms
41,852 KB
testcase_08 AC 197 ms
41,984 KB
testcase_09 AC 79 ms
33,004 KB
testcase_10 AC 78 ms
32,904 KB
testcase_11 AC 80 ms
33,052 KB
testcase_12 AC 77 ms
32,900 KB
testcase_13 AC 77 ms
32,952 KB
testcase_14 AC 8 ms
22,404 KB
testcase_15 AC 8 ms
22,576 KB
testcase_16 AC 8 ms
22,452 KB
testcase_17 AC 8 ms
22,332 KB
testcase_18 AC 8 ms
22,304 KB
testcase_19 AC 7 ms
22,384 KB
testcase_20 AC 6 ms
22,280 KB
testcase_21 AC 9 ms
22,532 KB
testcase_22 AC 9 ms
22,408 KB
testcase_23 AC 9 ms
22,292 KB
testcase_24 AC 9 ms
22,304 KB
testcase_25 AC 9 ms
22,296 KB
testcase_26 AC 9 ms
22,332 KB
testcase_27 AC 9 ms
22,328 KB
testcase_28 AC 9 ms
22,480 KB
testcase_29 AC 151 ms
36,884 KB
testcase_30 AC 17 ms
23,492 KB
testcase_31 AC 119 ms
34,392 KB
testcase_32 AC 62 ms
30,664 KB
testcase_33 AC 11 ms
23,168 KB
testcase_34 AC 63 ms
30,828 KB
testcase_35 AC 72 ms
31,780 KB
testcase_36 AC 11 ms
23,064 KB
testcase_37 AC 56 ms
28,104 KB
testcase_38 AC 195 ms
41,508 KB
testcase_39 AC 204 ms
41,792 KB
testcase_40 AC 130 ms
35,572 KB
testcase_41 AC 63 ms
29,100 KB
testcase_42 AC 196 ms
41,848 KB
testcase_43 AC 211 ms
41,920 KB
testcase_44 AC 58 ms
29,776 KB
testcase_45 AC 55 ms
29,540 KB
testcase_46 AC 47 ms
27,584 KB
testcase_47 AC 10 ms
22,456 KB
testcase_48 AC 10 ms
22,360 KB
testcase_49 AC 235 ms
41,920 KB
testcase_50 AC 232 ms
42,000 KB
testcase_51 AC 227 ms
42,064 KB
testcase_52 AC 287 ms
46,532 KB
testcase_53 AC 205 ms
42,004 KB
testcase_54 AC 241 ms
43,692 KB
testcase_55 AC 209 ms
42,816 KB
testcase_56 AC 207 ms
42,280 KB
testcase_57 AC 174 ms
41,976 KB
testcase_58 AC 176 ms
41,976 KB
testcase_59 AC 188 ms
41,848 KB
testcase_60 AC 9 ms
22,316 KB
testcase_61 AC 7 ms
22,580 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 N;
int A[202020];
multiset<int> L[202020],R[202020];

int getML(int a) {
	return *L[a].rbegin();
}
int getMR(int a) {
	if(L[a].size()==R[a].size()) return *R[a].begin();
	return *L[a].rbegin();
}

void add(int t,int a) {
	L[t].insert(a);
	int x=*L[t].rbegin();
	L[t].erase(L[t].find(x));
	R[t].insert(x);
	if(R[t].size()>L[t].size()) {
		x=*R[t].begin();
		L[t].insert(x);
		R[t].erase(R[t].find(x));
	}
}

void solve() {
	int i,j,k,l,r,x,y;
	
	cin>>N;
	FOR(i,N) cin>>A[i];
	sort(A,A+N);
	vector<int> s;
	FOR(i,N) {
		add(i,A[i]-i);
		s.push_back(i);
		while(s.size()>=2) {
			x=s.back();
			s.pop_back();
			y=s.back();
			s.pop_back();
			if(getML(y)<getMR(x)) {
				s.push_back(y);
				s.push_back(x);
				break;
			}
			if(L[x].size()<L[y].size()) swap(x,y);
			FORR(a,L[y]) add(x,a);
			FORR(a,R[y]) add(x,a);
			s.push_back(x);
		}
	}
	ll ret=0;
	FORR(a,s) {
		ll b=getML(a);
		
		FORR(v,L[a]) ret+=abs(b-v);
		FORR(v,R[a]) ret+=abs(b-v);
	}
	cout<<ret<<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