結果

問題 No.738 平らな農地
ユーザー msm1993msm1993
提出日時 2020-02-18 18:49:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,921 bytes
コンパイル時間 1,098 ms
コンパイル使用メモリ 117,496 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-04-16 04:12:36
合計ジャッジ時間 8,410 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 84 ms
6,940 KB
testcase_16 AC 77 ms
6,940 KB
testcase_17 AC 71 ms
6,944 KB
testcase_18 AC 80 ms
6,940 KB
testcase_19 AC 56 ms
6,940 KB
testcase_20 AC 87 ms
6,944 KB
testcase_21 AC 84 ms
6,940 KB
testcase_22 AC 86 ms
6,940 KB
testcase_23 AC 71 ms
6,944 KB
testcase_24 AC 70 ms
6,948 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 3 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 3 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 95 ms
6,940 KB
testcase_46 AC 79 ms
6,940 KB
testcase_47 AC 93 ms
6,940 KB
testcase_48 AC 79 ms
6,944 KB
testcase_49 AC 79 ms
6,940 KB
testcase_50 AC 83 ms
6,944 KB
testcase_51 AC 93 ms
6,940 KB
testcase_52 AC 89 ms
6,940 KB
testcase_53 AC 88 ms
6,944 KB
testcase_54 AC 92 ms
6,940 KB
testcase_55 AC 92 ms
6,940 KB
testcase_56 AC 82 ms
6,940 KB
testcase_57 AC 77 ms
6,944 KB
testcase_58 AC 90 ms
6,940 KB
testcase_59 AC 87 ms
6,940 KB
testcase_60 AC 87 ms
6,948 KB
testcase_61 AC 84 ms
6,940 KB
testcase_62 AC 85 ms
6,940 KB
testcase_63 AC 95 ms
6,944 KB
testcase_64 AC 92 ms
6,940 KB
testcase_65 AC 36 ms
6,944 KB
testcase_66 AC 37 ms
6,944 KB
testcase_67 AC 89 ms
6,940 KB
testcase_68 AC 80 ms
6,940 KB
testcase_69 AC 69 ms
6,944 KB
testcase_70 AC 82 ms
6,944 KB
testcase_71 AC 53 ms
6,944 KB
testcase_72 AC 62 ms
6,944 KB
testcase_73 AC 65 ms
6,940 KB
testcase_74 AC 59 ms
6,940 KB
testcase_75 AC 67 ms
6,940 KB
testcase_76 AC 75 ms
6,940 KB
testcase_77 AC 59 ms
6,944 KB
testcase_78 AC 71 ms
6,940 KB
testcase_79 AC 83 ms
6,940 KB
testcase_80 AC 97 ms
6,940 KB
testcase_81 AC 69 ms
6,944 KB
testcase_82 AC 55 ms
6,944 KB
testcase_83 AC 78 ms
6,940 KB
testcase_84 AC 81 ms
6,944 KB
testcase_85 AC 43 ms
6,944 KB
testcase_86 AC 45 ms
6,940 KB
testcase_87 AC 79 ms
8,448 KB
testcase_88 AC 73 ms
8,192 KB
testcase_89 AC 2 ms
6,940 KB
testcase_90 AC 1 ms
6,944 KB
testcase_91 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}
0