結果

問題 No.738 平らな農地
ユーザー beetbeet
提出日時 2020-02-21 16:45:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 8,138 bytes
コンパイル時間 3,133 ms
コンパイル使用メモリ 223,172 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-04-17 06:46:09
合計ジャッジ時間 12,839 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 6 ms
6,944 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 4 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 136 ms
9,600 KB
testcase_16 AC 134 ms
9,728 KB
testcase_17 AC 157 ms
9,856 KB
testcase_18 AC 151 ms
9,600 KB
testcase_19 AC 175 ms
10,112 KB
testcase_20 AC 137 ms
9,728 KB
testcase_21 AC 168 ms
10,240 KB
testcase_22 AC 144 ms
9,856 KB
testcase_23 AC 165 ms
9,984 KB
testcase_24 AC 170 ms
9,856 KB
testcase_25 AC 2 ms
6,948 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 3 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 3 ms
6,944 KB
testcase_33 AC 3 ms
6,940 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 3 ms
6,944 KB
testcase_36 AC 3 ms
6,940 KB
testcase_37 AC 3 ms
6,940 KB
testcase_38 AC 3 ms
6,944 KB
testcase_39 AC 3 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 3 ms
6,940 KB
testcase_42 AC 3 ms
6,944 KB
testcase_43 AC 3 ms
6,944 KB
testcase_44 AC 3 ms
6,940 KB
testcase_45 AC 139 ms
10,752 KB
testcase_46 AC 136 ms
10,112 KB
testcase_47 AC 141 ms
10,368 KB
testcase_48 AC 130 ms
10,240 KB
testcase_49 AC 125 ms
9,984 KB
testcase_50 AC 131 ms
10,368 KB
testcase_51 AC 142 ms
10,368 KB
testcase_52 AC 137 ms
9,984 KB
testcase_53 AC 142 ms
10,368 KB
testcase_54 AC 144 ms
10,624 KB
testcase_55 AC 153 ms
10,368 KB
testcase_56 AC 145 ms
10,368 KB
testcase_57 AC 132 ms
9,984 KB
testcase_58 AC 136 ms
10,240 KB
testcase_59 AC 138 ms
10,624 KB
testcase_60 AC 141 ms
10,624 KB
testcase_61 AC 137 ms
10,624 KB
testcase_62 AC 131 ms
10,112 KB
testcase_63 AC 158 ms
10,624 KB
testcase_64 AC 140 ms
10,496 KB
testcase_65 AC 183 ms
9,728 KB
testcase_66 AC 199 ms
9,984 KB
testcase_67 AC 120 ms
11,392 KB
testcase_68 AC 121 ms
11,520 KB
testcase_69 AC 167 ms
11,648 KB
testcase_70 AC 151 ms
12,160 KB
testcase_71 AC 67 ms
7,424 KB
testcase_72 AC 68 ms
6,940 KB
testcase_73 AC 61 ms
7,040 KB
testcase_74 AC 72 ms
7,296 KB
testcase_75 AC 168 ms
11,392 KB
testcase_76 AC 143 ms
11,648 KB
testcase_77 AC 173 ms
11,264 KB
testcase_78 AC 194 ms
12,032 KB
testcase_79 AC 187 ms
12,032 KB
testcase_80 AC 149 ms
11,776 KB
testcase_81 AC 161 ms
11,392 KB
testcase_82 AC 172 ms
11,776 KB
testcase_83 AC 70 ms
7,552 KB
testcase_84 AC 67 ms
7,296 KB
testcase_85 AC 248 ms
11,904 KB
testcase_86 AC 226 ms
11,136 KB
testcase_87 AC 107 ms
11,648 KB
testcase_88 AC 104 ms
11,136 KB
testcase_89 AC 2 ms
6,944 KB
testcase_90 AC 2 ms
6,944 KB
testcase_91 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/1732"

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename V>
V compress(V v){
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  return v;
}
template<typename T>
map<T, int> dict(const vector<T> &v){
  map<T, int> res;
  for(int i=0;i<(int)v.size();i++)
    res[v[i]]=i;
  return res;
}
map<char, int> dict(const string &v){
  return dict(vector<char>(v.begin(),v.end()));
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
struct FullyIndexableDictionary{
  int len,blk;
  vector<unsigned> bit;
  vector<int> sum;

  FullyIndexableDictionary(){}
  FullyIndexableDictionary(int len)
    :len(len),blk((len+31)>>5),bit(blk,0),sum(blk,0){}

  void set(int k){
    bit[k>>5]|=1u<<(k&31);
  }

  void build(){
    sum[0]=0;
    for(int i=1;i<blk;i++)
      sum[i]=sum[i-1]+__builtin_popcount(bit[i-1]);
  }

  bool operator[](int k) const{
    return bool((bit[k>>5]>>(k&31))&1);
  }

  int rank(int k){
    return sum[k>>5]+__builtin_popcount(bit[k>>5]&((1u<<(k&31))-1));
  }

  int rank(bool v,int k){
    return (v?rank(k):k-rank(k));
  }

  int select(bool v,int k){
    if(k<0||rank(v,len)<=k) return -1;
    int l=0,r=len;
    while(l+1<r){
      int m=(l+r)>>1;
      if(rank(v,m)>=k+1) r=m;
      else l=m;
    }
    return r-1;
  }

  int select(bool v,int i,int l){
    return select(v,i+rank(v,l));
  }
};

template<class T,int MAXLOG>
struct WaveletMatrix{
  int len;
  FullyIndexableDictionary mat[MAXLOG];
  int zs[MAXLOG],buff1[MAXLOG],buff2[MAXLOG];
  static const T npos=-1;

  int freq_dfs(int d,int l,int r,T val,T a,T b){
    if(l==r) return 0;
    if(d==MAXLOG) return (a<=val&&val<b)?r-l:0;
    T nv=T(1)<<(MAXLOG-d-1)|val;
    T nnv=((T(1)<<(MAXLOG-d-1))-1)|nv;
    if(nnv<a||b<=val) return 0;
    if(a<=val&&nnv<b) return r-l;
    int lc=mat[d].rank(1,l),rc=mat[d].rank(1,r);
    return freq_dfs(d+1,l-lc,r-rc,val,a,b)
      +freq_dfs(d+1,lc+zs[d],rc+zs[d],nv,a,b);
  }

  WaveletMatrix(vector<T> data){
    len=data.size();
    vector<T> ls(len),rs(len);
    for(int dep=0;dep<MAXLOG;dep++){
      mat[dep]=FullyIndexableDictionary(len+1);
      int p=0,q=0;
      for(int i=0;i<len;i++){
        bool k=(data[i]>>(MAXLOG-(dep+1)))&1;
        if(k) rs[q++]=data[i],mat[dep].set(i);
        else  ls[p++]=data[i];
      }
      zs[dep]=p;
      mat[dep].build();
      swap(ls,data);
      for(int i=0;i<q;i++) data[p+i]=rs[i];
    }
  }

  T access(int k){
    T res=0;
    for(int dep=0;dep<MAXLOG;dep++){
      bool bit=mat[dep][k];
      res=(res<<1)|bit;
      k=mat[dep].rank(bit,k)+zs[dep]*dep;
    }
    return res;
  }

  // return the number of v in [0,k)
  int rank(T v,int k){
    int l=0,r=k;
    for(int dep=0;dep<MAXLOG;dep++){
      buff1[dep]=l;buff2[dep]=r;
      bool bit=(v>>(MAXLOG-(dep+1)))&1;
      l=mat[dep].rank(bit,l)+zs[dep]*bit;
      r=mat[dep].rank(bit,r)+zs[dep]*bit;
    }
    return r-l;
  }

  // return the position of k-th v
  int select(T v,int k){
    rank(v,len);
    for(int dep=MAXLOG-1;dep>=0;dep--){
      bool bit=(v>>(MAXLOG-(dep+1)))&1;
      k=mat[dep].select(bit,k,buff1[dep]);
      if(k>=buff2[dep]||k<0) return -1;
      k-=buff1[dep];
    }
    return k;
  }

  int select(T v,int k,int l){
    return select(v,k+rank(v,l));
  }

  // return k-th largest value in [l,r)
  T quantile(int l,int r,int k){
    if(r-l<=k||k<0) return -1;
    T res=0;
    for(int dep=0;dep<MAXLOG;dep++){
      int p=mat[dep].rank(1,l);
      int q=mat[dep].rank(1,r);
      if(q-p>k){
        l=p+zs[dep];
        r=q+zs[dep];
        res|=T(1)<<(MAXLOG-(dep+1));
      }else{
        k-=(q-p);
        l-=p;
        r-=q;
      }
    }
    return res;
  }

  T rquantile(int l,int r,int k){
    return quantile(l,r,r-l-k-1);
  }

  // return number of points in [left, right) * [lower, upper)
  int rangefreq(int left,int right,T lower,T upper){
    return freq_dfs(0,left,right,0,lower,upper);
  }

  pair<int, int> ll(int l,int r,T v){
    int res=0;
    for(int dep=0;dep<MAXLOG;dep++){
      buff1[dep]=l;buff2[dep]=r;
      bool bit=(v>>(MAXLOG-(dep+1)))&1;
      if(bit) res+=r-l+mat[dep].rank(bit,l)-mat[dep].rank(bit,r);
      l=mat[dep].rank(bit,l)+zs[dep]*bit;
      r=mat[dep].rank(bit,r)+zs[dep]*bit;
    }
    return make_pair(res,r-l);
  }

  int lt(int l,int r,T v){
    auto p=ll(l,r,v);
    return p.first;
  }

  int le(int l,int r,T v){
    auto p=ll(l,r,v);
    return p.first+p.second;
  }

  T succ(int l,int r,T v){
    int k=le(l,r,v);
    return k==r-l?npos:rquantile(l,r,k);
  }

  T pred(int l,int r,T v){
    int k=lt(l,r,v);
    return k?rquantile(l,r,k-1):npos;
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE

// test rquantile
signed SPOJ_MKTHNUM(){
  int n,q;
  scanf("%d %d",&n,&q);
  vector<int> vs(n);
  for(int i=0;i<n;i++) scanf("%d",&vs[i]);
  const int OFS = 1e9+1;
  for(int i=0;i<n;i++) vs[i]+=OFS;
  WaveletMatrix<int,32> wm(vs);
  for(int i=0;i<q;i++){
    int l,r,k;
    scanf("%d %d %d",&l,&r,&k);
    l--;k--;
    printf("%d\n",wm.rquantile(l,r,k)-OFS);
  }
  return 0;
}
/*
  verified on 2019/10/29
  https://www.spoj.com/problems/MKTHNUM/
*/

signed main(){
  //SPOJ_MKTHNUM();
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T>
struct BIT{
  int n;
  vector<T> bit;
  //1-indexed
  BIT(int n_):n(n_+1),bit(n+1,0){}

  T sum(int i){
    T s(0);
    for(int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }

  void add(int i,T a){
    if(i==0) return;
    for(int x=i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }

  int lower_bound(int w){
    if(w<=0) return 0;
    int x=0,r=1;
    while(r<n) r<<=1;
    for(int k=r;k>0;k>>=1){
      if(x+k<=n&&bit[x+k]<w){
        w-=bit[x+k];
        x+=k;
      }
    }
    return x+1;
  }

  // [l, r)
  T query(int l,int r){
    return sum(r-1)-sum(l-1);
  }

  T sum0(int i){
    return sum(i+1);
  }

  void add0(int i,T a){
    add(i+1,a);
  }

  T query0(int l,int r){
    return sum(r)-sum(l);
  }
};
//END CUT HERE
#ifndef call_from_test

#define call_from_test
#include "../tools/fastio.cpp"
#undef call_from_test

// lower_bound
signed ARC033_C(){
  int q;
  cin>>q;
  BIT<int> bit(2e5);
  while(q--){
    int t,x;
    cin>>t>>x;
    if(t==1) bit.add(x,1);
    if(t==2){
      int k=bit.lower_bound(x);
      bit.add(k,-1);
      cout<<k<<endl;
    }
  }
  return 0;
}
/*
  verified on 2019/11/11
  https://atcoder.jp/contests/arc033/tasks/arc033_3
*/

signed main(){
  ARC033_C();
  return 0;
}
#endif

#undef call_from_test

signed main(){
  using ll = long long;
  int n,k;
  cin>>n>>k;
  vector<int> as(n);
  for(int i=0;i<n;i++) cin>>as[i];

  auto vx=compress(as);
  auto mx=dict(vx);

  BIT<ll> bit1(n+10),bit2(n+10);

  ll ans=1e18,sum=0;
  for(int i=0;i<k;i++){
    bit1.add0(mx[as[i]],1);
    bit2.add0(mx[as[i]],as[i]);
    sum+=as[i];
  }

  WaveletMatrix<int, 30> wm(as);
  for(int i=0;i+k<=n;i++){
    int j=i+k;
    int x=wm.quantile(i,j,k>>1);

    ll p=bit1.sum0(mx[x]),q=k-p;
    ll s=bit2.sum0(mx[x]);
    chmin(ans,p*x-s+(sum-s)-q*x);

    bit1.add0(mx[as[i]],-1);
    bit2.add0(mx[as[i]],-as[i]);
    sum-=as[i];

    if(j==n) continue;
    bit1.add0(mx[as[j]],1);
    bit2.add0(mx[as[j]],as[j]);
    sum+=as[j];
  }

  cout<<ans<<endl;
  return 0;
}
0