結果
| 問題 |
No.2304 Distinct Elements
|
| コンテスト | |
| ユーザー |
Taiki0715
|
| 提出日時 | 2023-11-14 18:21:07 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 68 ms / 3,000 ms |
| コード長 | 4,188 bytes |
| コンパイル時間 | 7,120 ms |
| コンパイル使用メモリ | 180,480 KB |
| 実行使用メモリ | 7,036 KB |
| 最終ジャッジ日時 | 2024-09-26 03:37:47 |
| 合計ジャッジ時間 | 10,430 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 58 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}
istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}
#endif
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define reps(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) reps(i,0,n)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcount(x)
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
else return init;
}
void SOLVE();
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
clock_t start=clock();
#endif
int testcase=1;
//cin>>testcase;
for(int i=0;i<testcase;i++){
SOLVE();
}
#ifdef LOCAL
cerr<<"time:";
cerr<<(clock()-start)/1000;
cerr<<"ms\n";
#endif
}
template<typename T>
struct slope_trick{
private:
priority_queue<T>left;
minque<T>right;
T mn;
T left_shift,right_shift;
void left_push(T a){left.push(a-left_shift);}
T left_top()const{return left.top()+left_shift;}
void right_push(T a){right.push(a-right_shift);}
T right_top()const{return right.top()+right_shift;}
//f(x)+=max(0,x-a)
void add_right(T a){
if(left.empty()||left_top()<a)right_push(a);
else{
mn+=max<T>(0,left_top()-a);
left_push(a);
right_push(left_top());
left.pop();
}
}
//f(x)+=max(0,a-x)
void add_left(T a){
if(right.empty()||right_top()>a)left_push(a);
else{
mn+=max<T>(0,a-right_top());
right_push(a);
left_push(right_top());
right.pop();
}
}
public:
//f(x)=0
slope_trick():mn(0),left_shift(0),right_shift(0),left(),right(){}
//f(x)+=|x-a|
void add(T a){
add_left(a);
add_right(a);
}
T min()const{return mn;}
//f(x)=min[x-b<=y<=x-a](f(y))
void xshift(T a,T b){
assert(a<=b);
left_shift+=a;
right_shift+=b;
}
//f(x)=f(x-a)
void xshift(T a){
left_shift+=a;
right_shift+=a;
}
//f(x)+=a
void yshift(T a){mn+=a;}
pair<T,T>amin()const{
pair<T,T>ret{numeric_limits<T>::min(),numeric_limits<T>::max()};
if(!left.empty())ret.first=left_top();
if(!right.empty())ret.second=right_top();
return ret;
}
//f(x)=min[y<=x](f(y))
void ruisekimin(){while(!right.empty())right.pop();}
void merge(slope_trick &a){
if(a.left.size()+a.right.size()>left.size()+right.size()){
swap(a.left,left);
swap(a.right,right);
swap(a.mn,mn);
swap(a.left_shift,left_shift);
swap(a.right_shift,right_shift);
}
while(!a.left.empty()){
add_left(a.left_top());
a.left.pop();
}
while(!a.right.empty()){
add_right(a.right_top());
a.right.pop();
}
mn+=a.mn;
}
};
void SOLVE(){
int n;
cin>>n;
vector<ll>a(n);
cin>>a;
sort(all(a));
slope_trick<ll>dp;
rep(i,n){
dp.xshift(1);
dp.ruisekimin();
dp.add(a[i]);
}
cout<<dp.min()<<endl;
}
Taiki0715