結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー 沙耶花沙耶花
提出日時 2020-10-09 21:26:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,993 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 209,768 KB
実行使用メモリ 9,772 KB
最終ジャッジ日時 2023-09-27 14:17:56
合計ジャッジ時間 16,035 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 137 ms
9,128 KB
testcase_19 AC 137 ms
9,192 KB
testcase_20 AC 78 ms
6,516 KB
testcase_21 AC 13 ms
4,376 KB
testcase_22 AC 81 ms
6,796 KB
testcase_23 AC 10 ms
4,376 KB
testcase_24 AC 109 ms
8,684 KB
testcase_25 AC 82 ms
6,516 KB
testcase_26 AC 30 ms
4,648 KB
testcase_27 AC 10 ms
4,376 KB
testcase_28 AC 143 ms
9,440 KB
testcase_29 AC 147 ms
9,564 KB
testcase_30 AC 146 ms
9,520 KB
testcase_31 AC 144 ms
9,568 KB
testcase_32 AC 145 ms
9,556 KB
testcase_33 AC 143 ms
9,680 KB
testcase_34 AC 151 ms
9,724 KB
testcase_35 AC 141 ms
9,772 KB
testcase_36 AC 148 ms
9,620 KB
testcase_37 AC 145 ms
9,720 KB
testcase_38 AC 146 ms
9,432 KB
testcase_39 AC 147 ms
9,508 KB
testcase_40 AC 149 ms
9,684 KB
testcase_41 AC 144 ms
9,560 KB
testcase_42 AC 148 ms
9,584 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,376 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 2000000001

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;
	
	vector<long long> A(N),B(N);
	rep(i,N)cin>>A[i];
	rep(i,N)cin>>B[i];
		
	absolute_minima X;
	rep(i,N){
		X.add(A[i],B[i]);
	}
	
	cout<<X.median()<<' '<<X.distance_sum()<<endl;

    return 0;
}
0