結果
| 問題 |
No.738 平らな農地
|
| ユーザー |
FF256grhy
|
| 提出日時 | 2018-09-29 03:25:02 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,991 bytes |
| コンパイル時間 | 1,637 ms |
| コンパイル使用メモリ | 171,900 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-12 07:56:49 |
| 合計ジャッジ時間 | 16,820 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 5 |
| other | RE * 87 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for(int i = (l) ; i < (r); i++)
#define incII(i, l, r) for(int i = (l) ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r) ; i >= (l); i--)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define FOR(it, v) for(auto it = v.begin(); it != v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)
template<typename T> bool setmin(T & a, T b) { if(b < a) { a = b; return true; } else { return false; } }
template<typename T> bool setmax(T & a, T b) { if(b > a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
// ---- ----
template<typename T> struct Node { T s; int l, r; };
template<typename T> class Tree {
private:
int d;
vector<Node<T>> v;
public:
Tree(int dd) { // インデックスを d ビットの非負整数とする
d = dd;
v.PB({ 0, 0, 0 });
}
T insert(T x) { return add(x, 1); }
T erase(T x) { return add(x, -1); }
T rank(T x) { return add(x, 0); }
T add(T x, T y) { // a[x] += y して sum [0, x) を返す
int p = 0;
T sum = 0;
dec(i, d) {
bool is_l = ! ((x >> i) & 1);
if(is_l) { v[p].s += y; } else { sum += v[p].s; }
int & ref (is_l ? v[p].l : v[p].r);
if(ref == 0) {
if(y == 0) { return sum; }
ref = v.size();
v.PB({ 0, 0, 0 });
}
p = ref;
}
return sum;
}
T find(T s) { // sum[0, x) <= s となる最大の x を返す
int p = 0;
T sum = 0, x = 0;
dec(i, d) {
if(sum + v[p].s > s) { p = v[p].l; }
else { p = v[p].r; sum += v[p].s; x += 1LL << i; }
}
return x;
}
};
// ----
LL n, k, a[100000];
int main() {
cin >> n >> k;
inc(i, n) { cin >> a[i]; }
Tree<LL> tc(30);
Tree<LL> ts(30);
LL ans = 1e15;
inc(i, n) {
LL x = a[i];
tc.add(x, 1);
ts.add(x, x);
if(i >= k - 1) {
LL M = tc.find(k / 2);
LL H = 1e9 + 1;
LL lc = tc.rank(M);
LL ls = ts.rank(M);
LL hc = tc.rank(H) - lc;
LL hs = ts.rank(H) - ls;
setmin(ans, (M * lc - ls) + (hs - M * hc));
LL y = a[i - k + 1];
tc.add(y, -1);
ts.add(y, -y);
}
}
cout << ans << endl;
return 0;
}
FF256grhy