結果

問題 No.2304 Distinct Elements
ユーザー 沙耶花沙耶花
提出日時 2023-05-12 22:23:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,118 bytes
コンパイル時間 5,544 ms
コンパイル使用メモリ 280,028 KB
実行使用メモリ 33,000 KB
最終ジャッジ日時 2024-05-06 12:26:13
合計ジャッジ時間 11,377 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 141 ms
10,216 KB
testcase_05 AC 108 ms
10,344 KB
testcase_06 AC 106 ms
10,220 KB
testcase_07 AC 104 ms
10,348 KB
testcase_08 AC 104 ms
10,216 KB
testcase_09 AC 159 ms
32,744 KB
testcase_10 AC 158 ms
32,868 KB
testcase_11 AC 159 ms
32,740 KB
testcase_12 AC 158 ms
32,876 KB
testcase_13 AC 165 ms
32,868 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
5,376 KB
testcase_18 WA -
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 97 ms
9,252 KB
testcase_30 AC 9 ms
5,376 KB
testcase_31 AC 79 ms
7,232 KB
testcase_32 AC 129 ms
32,576 KB
testcase_33 AC 12 ms
5,376 KB
testcase_34 AC 132 ms
32,584 KB
testcase_35 AC 145 ms
32,668 KB
testcase_36 AC 10 ms
5,376 KB
testcase_37 AC 37 ms
5,376 KB
testcase_38 AC 116 ms
10,332 KB
testcase_39 AC 118 ms
10,344 KB
testcase_40 AC 63 ms
8,924 KB
testcase_41 AC 33 ms
6,248 KB
testcase_42 AC 79 ms
10,344 KB
testcase_43 AC 137 ms
10,472 KB
testcase_44 AC 120 ms
32,520 KB
testcase_45 AC 83 ms
18,184 KB
testcase_46 WA -
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 115 ms
10,344 KB
testcase_50 AC 125 ms
10,216 KB
testcase_51 AC 122 ms
10,220 KB
testcase_52 AC 94 ms
9,116 KB
testcase_53 AC 126 ms
33,000 KB
testcase_54 AC 84 ms
10,724 KB
testcase_55 AC 82 ms
10,728 KB
testcase_56 AC 80 ms
10,344 KB
testcase_57 AC 131 ms
16,868 KB
testcase_58 AC 122 ms
16,868 KB
testcase_59 AC 91 ms
10,216 KB
testcase_60 AC 2 ms
5,376 KB
testcase_61 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

struct absolute_minima{
	using ll = long long;
	
	priority_queue<pair<ll,ll>> pQ;
	priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> sQ;
	ll pSum=0,sSum=0,pCnt=0,sCnt=0;
	
	void add(long long x,long long w){
		sQ.emplace(x,w);
		sSum += x*w;
		sCnt += w;
		while(true){
			if(pQ.size()>0&&sQ.size()>0){
				if(pQ.top().first>sQ.top().first){
					move(pCnt > sCnt);
					continue;
				}
				else if(pCnt > sCnt){
					move(true);
					continue;
				}
				else if(pCnt + sQ.top().second <= sCnt - sQ.top().second){
					move(false);
					continue;
				}
			}
			else{
				if(sQ.size()==0){
					move(true);
					continue;
				}
				else{
					if(sQ.top().second <= sCnt - sQ.top().second){
						move(false);
						continue;
					}
				}
			}
			break;
		}
	}
	
	void add(ll x){
		add(x,1LL);
	}
	
	ll median(){
		return sQ.top().first;
	}
	
	vector<ll> medians(){
		vector<ll> ret;
		if((pCnt+sCnt)%2==0){
			if(pCnt==sCnt)ret.push_back(pQ.top().first);
			else ret.push_back(sQ.top().first);
		}
		ret.push_back(median());
		return ret;
	}
	
	ll distance_sum(){
		ll ret = 0LL;
		ret -= pSum;
		ret += sSum;
		ret += pCnt * median();
		ret -= sCnt * median();
		return ret;
	}
	
	void move(bool f){
		if(f){
			if(pQ.size()>0){
				pair<ll,ll> p = pQ.top();
				pQ.pop();
				pSum -= p.first*p.second;
				pCnt -= p.second;
				sQ.push(p);
				sSum += p.first*p.second;
				sCnt += p.second;
			}
		}
		else{
			if(sQ.size()>0){
				pair<ll,ll> p = sQ.top();
				sQ.pop();
				sSum -= p.first*p.second;
				sCnt -= p.second;
				pQ.push(p);
				pSum += p.first*p.second;
				pCnt += p.second;
			}
		}
	}
	int size(){
		return pQ.size() + sQ.size();
	}
};

int main(){
	int n;
	cin>>n;
	vector<int> a(n);
	rep(i,n)cin>>a[i];
	
	sort(a.begin(),a.end());
	
	vector<absolute_minima> A;
	vector<bool> f;
	rep(i,n){
		absolute_minima t;
		t.add(a[i],1);
		A.push_back(t);
		f.push_back(false);
		while(A.size()>=2){
			int x = A.size()-2;
			int y = A.size()-1;
			int xr = A[x].median() + A[x].sQ.size() - 1;
			int yl = A[y].median() - A[y].pQ.size();
			if(f[x])xr++;
			if(xr<yl)break;
			if(A[y].size()%2==0 && xr<yl+1){
				f.back() = true;
				break;
			}
			if(A[x].size()<A[y].size())swap(A[x],A[y]);
			t = A.back();
			A.pop_back();
			f.pop_back();
			while(t.pQ.size()>0){
				A.back().add(t.pQ.top().first,1);
				t.pQ.pop();
			}
			while(t.sQ.size()>0){
				A.back().add(t.sQ.top().first,1);
				t.sQ.pop();
			}
		}
	}
	
	long long ans = 0;
	rep(i,A.size()){
		int l = A[i].median() - A[i].pQ.size();
		if(f[i])l++;
		vector<long long> vs;
		while(A[i].pQ.size()>0){
			vs.push_back(A[i].pQ.top().first);
			A[i].pQ.pop();
		}
		while(A[i].sQ.size()>0){
			vs.push_back(A[i].sQ.top().first);
			A[i].sQ.pop();
		}
		sort(vs.begin(),vs.end());
		rep(j,vs.size()){
			ans += abs(vs[j] - (l+j));
		}
		
	}
	cout<<ans<<endl;
	
	return 0;
}
0