結果

問題 No.738 平らな農地
ユーザー cotton_fn_
提出日時 2019-07-01 22:01:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,921 bytes
コンパイル時間 965 ms
コンパイル使用メモリ 115,392 KB
最終ジャッジ日時 2025-01-07 05:49:05
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 87
権限があれば一括ダウンロードができます

ソースコード

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cstdint>
using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x)
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0)
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs
i64 n, k;
vector<i64> a;
int main() {
cin >> n >> k;
read_n(a, n);
multiset<i64> lq, hq;
i64 ans = 1ll << 60, ls = 0, hs = 0;
for (i64 i = 0; i < n; ++i) {
if (i - k >= 0) {
i64 y = a[i - k];
auto lit = lq.find(y);
if (lit != lq.end()) {
lq.erase(lq.find(y));
ls -= y;
} else {
hq.erase(hq.find(y));
hs -= y;
}
}
i64 x = a[i];
if (lq.empty() || x <= *(lq.rbegin())) {
lq.insert(x);
ls += x;
} else {
hq.insert(x);
hs += x;
}
while (lq.size() < hq.size()) {
i64 d = *(hq.begin());
lq.insert(d);
hq.erase(hq.begin());
ls += d;
hs -= d;
}
while (lq.size() > hq.size()) {
i64 d = *(lq.rbegin());
hq.insert(d);
lq.erase(--(lq.end()));
hs += d;
ls -= d;
}
if (i + 1 >= k) {
i64 med = *(hq.begin());
i64 scr = lq.size() * med - ls + hs - hq.size() * med;
ans = min(ans, abs(scr));
}
}
cout << ans << '\n';
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0