結果
| 問題 |
No.838 Noelちゃんと星々3
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2020-12-10 22:26:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 84 ms / 2,000 ms |
| コード長 | 2,290 bytes |
| コンパイル時間 | 2,391 ms |
| コンパイル使用メモリ | 209,324 KB |
| 最終ジャッジ日時 | 2025-01-16 21:36:59 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:112:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
112 | 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 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;
}
沙耶花