結果
| 問題 |
No.837 Noelちゃんと星々2
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2020-12-10 22:11:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 82 ms / 2,000 ms |
| コード長 | 2,388 bytes |
| コンパイル時間 | 2,006 ms |
| コンパイル使用メモリ | 209,824 KB |
| 最終ジャッジ日時 | 2025-01-16 21:36:39 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 29 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:113:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
113 | scanf("%d",&x);
| ~~~~~^~~~~~~~~
ソースコード
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 10000000
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;
set<int> S;
vector<long long> A(N);
rep(i,N){
int x;
scanf("%d",&x);
A[i] = x;
S.insert(x);
}
if(S.size()==1)cout<<1<<endl;
else if(S.size()==2)cout<<0<<endl;
else{
sort(A.begin(),A.end());
vector<long long> S1(N,0),S2(N,0);
{
absolute_minima X;
rep(i,N){
X.add(A[i]);
S1[i] = X.distance_sum();
}
}
{
absolute_minima X;
for(int i=N-1;i>=0;i--){
X.add(A[i]);
S2[i] = X.distance_sum();
}
}
long long ans = 1e18;
rep(i,N-1){
ans = min(ans,S1[i] + S2[i+1]);
}
cout<<ans<<endl;
}
return 0;
}
沙耶花