結果

問題 No.838 Noelちゃんと星々3
ユーザー 沙耶花沙耶花
提出日時 2020-12-10 22:26:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,290 bytes
コンパイル時間 2,480 ms
コンパイル使用メモリ 219,424 KB
実行使用メモリ 11,920 KB
最終ジャッジ日時 2023-10-20 00:56:37
合計ジャッジ時間 3,897 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
11,916 KB
testcase_01 AC 95 ms
11,920 KB
testcase_02 AC 93 ms
11,920 KB
testcase_03 AC 96 ms
11,916 KB
testcase_04 AC 93 ms
11,920 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000000

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 main(){
	
	int N;
	cin>>N;
	
	map<long long,long long> mp;
	
	rep(i,N){
		int x;
		scanf("%d",&x);
		mp[x]++;
	}
	
	vector<pair<long long,long long>> A;
	
	for(auto a:mp){
		A.push_back(a);
	}
	
	N = A.size();
	
	vector<long long> dp(N+1,Inf);
	
	dp[0] = 0;
	
	rep(i,N){
		absolute_minima X;
		rep(j,3){
			if(i+j>=N)break;
			X.add(A[i+j].first,A[i+j].second);
			if(j==0&&A[i+j].second==1)continue;
			dp[i+j+1] = min(dp[i+j+1],dp[i] + X.distance_sum());
		}
	}
	
	cout<<dp.back()<<endl;
	
	
    return 0;
}
0