結果

問題 No.2304 Distinct Elements
ユーザー 👑 kmjpkmjp
提出日時 2023-05-12 23:33:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,773 bytes
コンパイル時間 2,792 ms
コンパイル使用メモリ 207,852 KB
実行使用メモリ 42,120 KB
最終ジャッジ日時 2023-08-19 06:22:08
合計ジャッジ時間 10,472 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
22,628 KB
testcase_01 WA -
testcase_02 AC 7 ms
22,456 KB
testcase_03 WA -
testcase_04 AC 210 ms
41,972 KB
testcase_05 AC 214 ms
41,876 KB
testcase_06 AC 216 ms
41,864 KB
testcase_07 AC 212 ms
41,972 KB
testcase_08 AC 195 ms
41,844 KB
testcase_09 AC 76 ms
33,092 KB
testcase_10 AC 75 ms
33,024 KB
testcase_11 AC 77 ms
32,992 KB
testcase_12 AC 77 ms
33,016 KB
testcase_13 AC 78 ms
33,020 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 8 ms
22,388 KB
testcase_22 WA -
testcase_23 AC 8 ms
22,384 KB
testcase_24 AC 7 ms
22,328 KB
testcase_25 AC 7 ms
22,532 KB
testcase_26 AC 7 ms
22,548 KB
testcase_27 AC 7 ms
22,372 KB
testcase_28 AC 7 ms
22,344 KB
testcase_29 AC 150 ms
36,884 KB
testcase_30 AC 16 ms
23,728 KB
testcase_31 AC 138 ms
34,548 KB
testcase_32 AC 71 ms
30,740 KB
testcase_33 AC 14 ms
23,016 KB
testcase_34 AC 73 ms
30,672 KB
testcase_35 AC 80 ms
31,948 KB
testcase_36 AC 14 ms
23,172 KB
testcase_37 AC 68 ms
28,124 KB
testcase_38 AC 225 ms
41,544 KB
testcase_39 AC 231 ms
41,964 KB
testcase_40 AC 146 ms
35,304 KB
testcase_41 AC 75 ms
29,088 KB
testcase_42 AC 217 ms
42,056 KB
testcase_43 AC 230 ms
41,844 KB
testcase_44 AC 59 ms
29,956 KB
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 8 ms
22,376 KB
testcase_48 AC 7 ms
22,356 KB
testcase_49 AC 203 ms
41,928 KB
testcase_50 AC 212 ms
42,120 KB
testcase_51 AC 214 ms
41,844 KB
testcase_52 AC 184 ms
42,056 KB
testcase_53 AC 84 ms
34,540 KB
testcase_54 AC 185 ms
41,848 KB
testcase_55 AC 203 ms
41,844 KB
testcase_56 AC 204 ms
41,972 KB
testcase_57 AC 170 ms
41,872 KB
testcase_58 AC 182 ms
41,924 KB
testcase_59 AC 196 ms
41,984 KB
testcase_60 AC 9 ms
22,316 KB
testcase_61 AC 7 ms
22,444 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