結果

問題 No.2304 Distinct Elements
ユーザー 👑 emthrm
提出日時 2023-05-13 01:56:00
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 49 ms / 3,000 ms
コード長 3,181 bytes
コンパイル時間 3,041 ms
コンパイル使用メモリ 261,708 KB
実行使用メモリ 6,264 KB
最終ジャッジ日時 2024-11-28 22:04:48
合計ジャッジ時間 6,045 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 58
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
template <typename T>
struct SlopeTrick {
const T inf;
explicit SlopeTrick(
const T min_f = 0, const T inf = std::numeric_limits<T>::max())
: inf(inf), added_l(0), added_r(0), min_f(min_f) {}
T min() const { return min_f; }
std::pair<T, T> argmin() const { return {top_l(), top_r()}; }
template <typename U>
U f(const U x) {
U f_x = min_f;
std::vector<T> tmp;
for (; top_l() > x; l.pop()) {
const T t = top_l();
f_x += t - x;
tmp.emplace_back(t);
}
for (; !tmp.empty(); tmp.pop_back()) {
emplace_l(tmp.back());
}
for (; top_r() < x; r.pop()) {
const T t = top_r();
f_x += x - t;
tmp.emplace_back(t);
}
for (; !tmp.empty(); tmp.pop_back()) {
emplace_r(tmp.back());
}
return f_x;
}
void constant_function(const T c) { min_f += c; }
void x_minus_a(const T a) {
const T t = top_l();
if (t <= a) {
emplace_r(a);
} else {
min_f += t - a;
emplace_l(a);
l.pop();
emplace_r(t);
}
}
void a_minus_x(const T a) {
const T t = top_r();
if (a <= t) {
emplace_l(a);
} else {
min_f += a - t;
emplace_r(a);
r.pop();
emplace_l(t);
}
}
void abs_x_minus_a(const T a) {
x_minus_a(a);
a_minus_x(a);
}
void cumulative_min() {
while (!r.empty()) r.pop();
added_r = 0;
}
void rcumulative_min() {
while (!l.empty()) l.pop();
added_l = 0;
}
void translate(const T a) { sliding_window_minimum(a, a); }
void sliding_window_minimum(const T a, const T b) {
assert(a <= b);
added_l += a;
added_r += b;
}
private:
T added_l, added_r, min_f;
std::priority_queue<T> l;
std::priority_queue<T, std::vector<T>, std::greater<T>> r;
void emplace_l(const T a) { l.emplace(a - added_l); }
void emplace_r(const T a) { r.emplace(a - added_r); }
T top_l() const { return l.empty() ? -inf : l.top() + added_l; }
T top_r() const { return r.empty() ? inf : r.top() + added_r; }
};
int main() {
int n; cin >> n;
vector<int> a(n); REP(i, n) cin >> a[i];
ranges::sort(a);
SlopeTrick<ll> slope_trick(0);
REP(i, n) {
slope_trick.cumulative_min();
slope_trick.abs_x_minus_a(a[i] - i);
}
cout << slope_trick.min() << '\n';
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0