結果

問題 No.738 平らな農地
ユーザー fumiphysfumiphys
提出日時 2019-03-27 02:38:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 742 ms / 2,000 ms
コード長 7,355 bytes
コンパイル時間 1,775 ms
コンパイル使用メモリ 134,328 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-10-11 01:40:28
合計ジャッジ時間 30,007 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 14 ms
5,248 KB
testcase_06 AC 15 ms
5,248 KB
testcase_07 AC 14 ms
5,248 KB
testcase_08 AC 6 ms
5,248 KB
testcase_09 AC 4 ms
5,248 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 8 ms
5,248 KB
testcase_12 AC 4 ms
5,248 KB
testcase_13 AC 13 ms
5,248 KB
testcase_14 AC 5 ms
5,248 KB
testcase_15 AC 452 ms
9,728 KB
testcase_16 AC 663 ms
9,856 KB
testcase_17 AC 703 ms
9,984 KB
testcase_18 AC 477 ms
9,728 KB
testcase_19 AC 584 ms
10,368 KB
testcase_20 AC 471 ms
9,856 KB
testcase_21 AC 544 ms
10,240 KB
testcase_22 AC 475 ms
9,856 KB
testcase_23 AC 742 ms
9,984 KB
testcase_24 AC 486 ms
9,856 KB
testcase_25 AC 6 ms
5,248 KB
testcase_26 AC 5 ms
5,248 KB
testcase_27 AC 5 ms
5,248 KB
testcase_28 AC 5 ms
5,248 KB
testcase_29 AC 6 ms
5,248 KB
testcase_30 AC 5 ms
5,248 KB
testcase_31 AC 4 ms
5,248 KB
testcase_32 AC 5 ms
5,248 KB
testcase_33 AC 6 ms
5,248 KB
testcase_34 AC 5 ms
5,248 KB
testcase_35 AC 4 ms
5,248 KB
testcase_36 AC 4 ms
5,248 KB
testcase_37 AC 4 ms
5,248 KB
testcase_38 AC 5 ms
5,248 KB
testcase_39 AC 5 ms
5,248 KB
testcase_40 AC 5 ms
5,248 KB
testcase_41 AC 6 ms
5,248 KB
testcase_42 AC 4 ms
5,248 KB
testcase_43 AC 5 ms
5,248 KB
testcase_44 AC 5 ms
5,248 KB
testcase_45 AC 442 ms
10,496 KB
testcase_46 AC 631 ms
9,856 KB
testcase_47 AC 449 ms
10,112 KB
testcase_48 AC 522 ms
9,856 KB
testcase_49 AC 551 ms
9,856 KB
testcase_50 AC 556 ms
9,984 KB
testcase_51 AC 495 ms
10,368 KB
testcase_52 AC 441 ms
9,984 KB
testcase_53 AC 435 ms
9,984 KB
testcase_54 AC 454 ms
10,368 KB
testcase_55 AC 487 ms
10,240 KB
testcase_56 AC 674 ms
10,240 KB
testcase_57 AC 542 ms
9,856 KB
testcase_58 AC 455 ms
9,984 KB
testcase_59 AC 611 ms
10,368 KB
testcase_60 AC 570 ms
10,240 KB
testcase_61 AC 604 ms
10,240 KB
testcase_62 AC 419 ms
9,856 KB
testcase_63 AC 490 ms
10,368 KB
testcase_64 AC 452 ms
10,240 KB
testcase_65 AC 13 ms
5,248 KB
testcase_66 AC 14 ms
5,248 KB
testcase_67 AC 267 ms
9,728 KB
testcase_68 AC 419 ms
9,984 KB
testcase_69 AC 623 ms
10,240 KB
testcase_70 AC 526 ms
10,496 KB
testcase_71 AC 361 ms
10,240 KB
testcase_72 AC 539 ms
9,728 KB
testcase_73 AC 541 ms
9,856 KB
testcase_74 AC 607 ms
10,112 KB
testcase_75 AC 580 ms
9,728 KB
testcase_76 AC 530 ms
9,984 KB
testcase_77 AC 541 ms
9,728 KB
testcase_78 AC 365 ms
10,240 KB
testcase_79 AC 444 ms
10,368 KB
testcase_80 AC 383 ms
10,112 KB
testcase_81 AC 401 ms
9,984 KB
testcase_82 AC 618 ms
10,240 KB
testcase_83 AC 440 ms
10,496 KB
testcase_84 AC 436 ms
10,240 KB
testcase_85 AC 117 ms
10,240 KB
testcase_86 AC 95 ms
9,856 KB
testcase_87 AC 209 ms
10,112 KB
testcase_88 AC 191 ms
9,600 KB
testcase_89 AC 2 ms
5,248 KB
testcase_90 AC 2 ms
5,248 KB
testcase_91 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <climits>
#include <bitset>
#include <list>
#include <random>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}

template <typename T, typename E>
struct ImplicitTreap{
  random_device rnd;
  T def = 0;
  E l_def = 0;
  function<T(T, T)> f;
  function<E(E, E)> g;
  function<T(T, E, int)> p;

  struct Node{
    T val, acc;
    E lazy;
    int cnt, pri;
    bool rev;
    Node *l, *r;
    Node(T val, int pri, T def, E l_def): val(val), acc(def), lazy(l_def), cnt(1), pri(pri), rev(false), l(nullptr), r(nullptr){}
  };
  using Tree = Node *;
  Tree root = nullptr;
  ImplicitTreap(T def, E l_def, function<T(T, T)> f, function<E(E, E)> g, function<T(T, E, int)> p): def(def), l_def(l_def), f(f), g(g), p(p){}
  int cnt(Tree t){
    if(!t)return 0;
    return t->cnt;
  }
  T acc(Tree t){
    if(!t)return def;
    return t->acc;
  }
  void update_cnt(Tree t){
    if(t){
      //cerr << t->val << endl;
      t->cnt = 1 + cnt(t->l) + cnt(t->r);
    }
  }
  void update_acc(Tree t){
    if(t){
      t->acc = f(t->val, f(acc(t->l), acc(t->r)));
    }
  }
  void pushup(Tree t){
    update_cnt(t);
    update_acc(t);
  }
  void pushdown(Tree t){
    if(t){
      if(t->rev){
        t->rev = false;
        swap(t->l, t->r);
        if(t->l)t->l->rev ^= 1;
        if(t->r)t->r->rev ^= 1;
      }
      if(t->lazy != l_def){
        if(t->l){
          t->l->lazy = g(t->l->lazy, t->lazy);
          t->l->acc = p(t->l->acc, t->lazy, cnt(t->l));
        }
        if(t->r){
          t->r->lazy = g(t->r->lazy, t->lazy);
          t->r->acc = p(t->r->acc, t->lazy, cnt(t->r));
        }
        t->val = p(t->val, t->lazy, 1);
        t->lazy = l_def;
      }
    }
    pushup(t);
  }
  void split(Tree t, int key, Tree &l, Tree &r){
    if(!t){
      l = r = nullptr;
      return;
    }
    pushdown(t);
    int ikey = cnt(t->l) + 1;
    if(key < ikey)split(t->l, key, l, t->l), r = t;
    else split(t->r, key - ikey, t->r, r), l = t;
    pushup(t);
  }
  void merge(Tree &t, Tree l, Tree r){
    pushdown(l);
    pushdown(r);
    if(!l || !r){
      if(l)t = l;
      else t = r;
      return;
    }
    if(l->pri > r->pri){
      merge(l->r, l->r, r), t = l;
    }else{
      merge(r->l, l, r->l), t = r;
    }
    pushup(t);
  }
  void insert(Tree &t, int key, Tree item){
    Tree t1, t2;
    split(t, key, t1, t2);
    merge(t1, t1, item);
    merge(t, t1, t2);
  }
  void erase(Tree &t, int key){
    Tree t1, t2, t3;
    split(t, key + 1, t1, t2);
    split(t1, key, t1, t3);
    merge(t, t1, t2);
  }
  void update(Tree t, int l, int r, E x){
    Tree t1, t2, t3;
    split(t, l, t1, t2);
    split(t2, r - l, t2, t3);
    t2->lazy = g(t2->lazy, x);
    t2->acc = p(t2->acc, x, cnt(t2));
    merge(t2, t2, t3);
    merge(t, t1, t2);
  }
  T query(Tree t, int l, int r){
    Tree t1, t2, t3;
    split(t, l, t1, t2);
    split(t2, r - l, t2, t3);
    T res = t2->acc;
    merge(t2, t2, t3);
    merge(t, t1, t2);
    return res;
  }
  int find_(Tree t, T x, int of, bool left=true){
    if(f(t->acc, x) == x)return -1;
    if(left){
      if(t->l && f(t->l->acc, x) != x)return find_(t->l, x, of, left);
      if(f(t->acc, x) != x)return of + cnt(t->l);
      return find_(t->r, x, of + cnt(t->l) + 1, left);
    }else{
      if(t->r && f(t->r->acc, x) != x)return find_(t->r, x, of + cnt(t->l) + 1, left);
      if(f(t->acc, x) != x)return of + cnt(t->l);
      return find_(t->l, x, of, left);
    }
  }
  void reverse(Tree t, int l, int r){
    if(l > r)return;
    Tree t1, t2, t3;
    split(t, l, t1, t2);
    split(t2, r - l, t2, t3);
    t2->rev ^= 1;
    merge(t2, t2, t3);
    merge(t, t1, t2);
  }
  // m is top
  void rotate(Tree t, int l, int m, int r){
    reverse(t, l, r);
    reverse(t, l, l + r - m);
    reverse(t, l + r - m, r);
  }
  int size(){
    return cnt(root);
  }
  void insert(int pos, T x){
    insert(root, pos, new Node(x, rnd(), def, l_def));
  }
  void update(int l, int r, T x){
    update(root, l, r, x);
  }
  T query(int l, int r){
    return query(root, l, r);
  }
  int find(int l, int r, T x, bool left=true){
    Tree t1, t2, t3;
    split(root, l, t1, t2);
    split(t2, r - l, t2, t3);
    int res = find_(t2, x, l, left);
    merge(t2, t2, t3);
    merge(root, t1, t2);
    return res;
  }
  void erase(int pos){
    erase(root, pos);
  }
  void reverse(int l, int r){
    reverse(root, l, r);
  }
  void rotate(int l, int m, int r){
    rotate(root, l, m, r);
  }
  T operator[](int pos){
    Tree t1, t2, t3;
    split(root, pos + 1, t1, t2);
    split(t1, pos, t1, t3);
    T res = t3->acc;
    merge(t1, t1, t3);
    merge(root, t1, t2);
    return res;
  }
  int at(Tree t, T x){
    if(!t)return 0;
    if(t->val == x)return cnt(t->l);
    else if(t->val > x)return at(t->l, x);
    else return cnt(t->l) + 1 + at(t->r, x);
  }
};

int at(ImplicitTreap<ll, ll> & itr, ll x){
  if(itr.size() == 0)return 0;
  return itr.at(itr.root, x);
}

int main(int argc, char const* argv[])
{
  //ios_base::sync_with_stdio(false);
  //cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  //rep(i, n)cin >> a[i];
  rep(i, n)scanf("%lld", &a[i]);
  if(k == 1){
    cout << 0 << endl;
    return 0;
  }
  ImplicitTreap<ll, ll> itr(0, 0, [](ll a, ll b){return a + b;},
      [](ll a, ll b){return a + b;}, [](ll a, ll b, int c){return a + b * c;});

  for(int i = 0; i < k; i++){
    itr.insert(at(itr, a[i]), a[i]);
  }
  ll res = linf;
  for(int i = k - 1; i < n; i++){
    if(i != k - 1){
      //for(int j = 0; j < k; j++)cerr << "o " << itr[j] << "\n "[j + 1 != k];
      //cerr << a[i-k] << " " << at(itr, a[i-k]) << endl;
      itr.erase(at(itr, a[i-k]));
      itr.insert(at(itr, a[i]), a[i]);
    }
    if(k % 2 == 1){
      ll med = itr[k / 2];
      ll tmp = itr.query(k / 2, k) - (k - k / 2) * med;
      tmp += (k / 2) * med - itr.query(0, k / 2);
      res = min(res, tmp);
    }else{
      ll med = (itr[k / 2 - 1] + itr[k / 2]) / 2;
      ll tmp = itr.query(k / 2, k) - (k - k / 2) * med;
      //cerr << itr[0] << " " << itr[1] << endl;
      tmp += (k / 2) * med - itr.query(0, k / 2);
      if(tmp >= 0)res = min(res, tmp);
      med++;
      tmp = itr.query(k / 2, k) - (k - k / 2) * med;
      tmp += (k / 2) * med - itr.query(0, k / 2);
      if(tmp >= 0)res = min(res, tmp);
    }
  }
  cout << res << endl;
	return 0;
}
0