結果

問題 No.2304 Distinct Elements
ユーザー milanis48663220
提出日時 2023-05-13 18:31:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 63 ms / 3,000 ms
コード長 3,054 bytes
コンパイル時間 1,130 ms
コンパイル使用メモリ 123,688 KB
最終ジャッジ日時 2025-02-13 00:08:29
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 58
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
return vector<vector<T>>(n, vector<T>(m, v));
}
template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}
template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
if(v.empty()) {
cout << endl;
return;
}
for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
cout << v.back() << endl;
}
template<typename T>
class SlopeTrick {
public:
SlopeTrick() {
min_f = 0;
add_l = 0;
add_r = 0;
}
void add_pos(T a){ // add max(x-a, 0)
l.push(a-add_l);
T l0 = l.top()+add_l; l.pop();
r.push(l0-add_r);
min_f += max<T>(l0-a, 0);
}
void add_neg(T a){ // add max(a-x, 0)
r.push(a-add_r);
T r0 = r.top()+add_r; r.pop();
l.push(r0-add_l);
min_f += max<T>(a-r0, 0);
}
void slide_neg(T a){
add_l += a;
}
void slide_pos(T a){
add_r += a;
}
T get_min(){
return min_f;
}
void clear_right(){
while(!r.empty()) r.pop();
}
/**
* be careful that this method takes O(NlonN) time (N := r.size()+l.size()).
*/
T eval(T x) {
T ans = min_f;
vector<T> buf;
while(!l.empty()){
buf.push_back(l.top());
l.pop();
}
for(T a: buf){
l.push(a);
ans += max<T>(a+add_l-x, 0);
}
buf.clear();
while(!r.empty()){
buf.push_back(r.top());
r.pop();
}
for(T a: buf){
r.push(a);
ans += max<T>(x-a-add_r, 0);
}
return ans;
}
private:
priority_queue<T> l;
priority_queue<T, vector<T>, greater<T>> r;
T min_f;
T add_l;
T add_r;
};
using P = pair<ll, ll>;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n; cin >> n;
vector<ll> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
for(int i = 0; i < n; i++) a[i] -= i;
SlopeTrick<ll> st;
for(int i = 0; i < n; i++){
st.clear_right();
st.add_pos(a[i]);
st.add_neg(a[i]);
}
cout << st.get_min() << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0