結果

問題 No.738 平らな農地
ユーザー ei1333333ei1333333
提出日時 2018-09-28 22:23:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,345 bytes
コンパイル時間 2,712 ms
コンパイル使用メモリ 212,436 KB
実行使用メモリ 16,640 KB
最終ジャッジ日時 2024-04-20 11:09:39
合計ジャッジ時間 15,020 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 6 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 5 ms
6,944 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 178 ms
8,832 KB
testcase_16 AC 193 ms
8,960 KB
testcase_17 AC 162 ms
6,940 KB
testcase_18 AC 163 ms
6,944 KB
testcase_19 AC 107 ms
6,944 KB
testcase_20 AC 193 ms
8,832 KB
testcase_21 AC 167 ms
6,940 KB
testcase_22 AC 177 ms
8,448 KB
testcase_23 AC 151 ms
6,940 KB
testcase_24 AC 129 ms
6,940 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 3 ms
6,944 KB
testcase_30 AC 3 ms
6,944 KB
testcase_31 AC 4 ms
6,944 KB
testcase_32 AC 3 ms
6,944 KB
testcase_33 AC 4 ms
6,940 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 3 ms
6,940 KB
testcase_36 AC 3 ms
6,940 KB
testcase_37 AC 4 ms
6,944 KB
testcase_38 AC 4 ms
6,944 KB
testcase_39 AC 4 ms
6,944 KB
testcase_40 AC 4 ms
6,944 KB
testcase_41 AC 3 ms
6,940 KB
testcase_42 AC 4 ms
6,940 KB
testcase_43 AC 4 ms
6,940 KB
testcase_44 AC 3 ms
6,940 KB
testcase_45 AC 212 ms
11,008 KB
testcase_46 AC 187 ms
9,216 KB
testcase_47 AC 202 ms
10,624 KB
testcase_48 AC 197 ms
11,136 KB
testcase_49 AC 195 ms
11,136 KB
testcase_50 AC 205 ms
12,032 KB
testcase_51 AC 202 ms
9,856 KB
testcase_52 AC 193 ms
10,240 KB
testcase_53 AC 206 ms
10,240 KB
testcase_54 AC 206 ms
9,984 KB
testcase_55 AC 204 ms
9,728 KB
testcase_56 AC 198 ms
9,856 KB
testcase_57 AC 184 ms
9,856 KB
testcase_58 AC 198 ms
10,624 KB
testcase_59 AC 216 ms
11,392 KB
testcase_60 AC 217 ms
11,648 KB
testcase_61 AC 217 ms
11,392 KB
testcase_62 AC 194 ms
10,368 KB
testcase_63 AC 211 ms
9,984 KB
testcase_64 AC 207 ms
10,496 KB
testcase_65 AC 38 ms
6,940 KB
testcase_66 AC 40 ms
6,940 KB
testcase_67 AC 183 ms
9,856 KB
testcase_68 AC 192 ms
10,496 KB
testcase_69 AC 135 ms
6,940 KB
testcase_70 AC 165 ms
9,216 KB
testcase_71 AC 138 ms
6,944 KB
testcase_72 AC 185 ms
6,948 KB
testcase_73 AC 193 ms
9,216 KB
testcase_74 AC 171 ms
6,944 KB
testcase_75 AC 152 ms
6,940 KB
testcase_76 AC 175 ms
9,344 KB
testcase_77 AC 102 ms
6,944 KB
testcase_78 AC 112 ms
6,940 KB
testcase_79 AC 157 ms
6,944 KB
testcase_80 AC 183 ms
8,960 KB
testcase_81 AC 153 ms
6,940 KB
testcase_82 AC 101 ms
6,944 KB
testcase_83 AC 192 ms
7,168 KB
testcase_84 AC 155 ms
7,424 KB
testcase_85 AC 61 ms
6,940 KB
testcase_86 AC 62 ms
6,940 KB
testcase_87 AC 195 ms
16,640 KB
testcase_88 AC 184 ms
16,000 KB
testcase_89 AC 3 ms
6,944 KB
testcase_90 AC 3 ms
6,944 KB
testcase_91 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

template< typename T >
struct PrioritySumStructure {
  static const bool INCREASE = false;
  static const bool DECREASE = true;

  bool order;
  int k, sz;
  T sum;
  set< pair< T, int > > add, pend;
  map< int, T > adding, pending;

  PrioritySumStructure(int k, bool order = INCREASE) : k(k), sum(0), sz(0), order(order) {}

  void Sweep() {
    while(sz < k && !pend.empty()) {
      auto p = order ? --pend.end() : pend.begin();
      sum += p->first;
      ++sz;
      add.emplace(*p);
      adding.emplace(p->second, p->first);
      pending.erase(p->second);
      pend.erase(p);
    }
    while(sz > k) {
      auto p = order ? add.begin() : --add.end();
      sum -= p->first;
      --sz;
      pend.emplace(*p);
      pending.emplace(p->second, p->first);
      adding.erase(p->second);
      add.erase(p);
    }
  }

  T getSum() {
    if(sz < k) throw ("get Sum Exception");
    return (sum);
  }

  void addElement(int k, T x) {
    if(adding.count(k) || pending.count(k)) {
      throw ("Add Element Exception");
    }
    ++sz;
    add.emplace(x, k);
    adding[k] = x;
    sum += x;
    Sweep();
  }

  void deleteElement(int k) {
    if(pending.count(k)) {
      pend.erase({pending[k], k});
      pending.erase(k);
    } else if(adding.count(k)) {
      --sz;
      sum -= adding[k];
      add.erase({adding[k], k});
      adding.erase(k);
      Sweep();
    } else {
      throw ("delete Element Exception");
    }
  }

  void incrementSize() {
    ++k;
    Sweep();
  }

  void decrementSize() {
    if(k == 0) throw ("decrement Size Exception");
    --k;
    Sweep();
  }
};

using int64 = long long;
const int64 INF = 1LL << 60;

int main() {
  int N, K, A[100000];
  cin >> N >> K;
  for(int i = 0; i < N; i++) cin >> A[i];
  int64 all[100001] = {};
  for(int i = 1; i <= N; i++) all[i] = A[i - 1] + all[i - 1];

  int sz = (K + 1) / 2;
  PrioritySumStructure< int64 > que(sz, false);
  int64 ret = 1LL << 60;
  for(int i = 0; i < N; i++) {
    que.addElement(i, A[i]);
    if(i - K + 1 >= 0) {
      int64 sum = all[i + 1] - all[i - K + 1];
      auto cand = prev(que.add.end())->first;
      ret = min(ret, cand * sz - que.getSum() + (sum - que.getSum()) - (K - sz) * cand);
      que.deleteElement(i - K + 1);
    }
  }
  cout << ret << endl;
}
0