結果
問題 | No.738 平らな農地 |
ユーザー | はまやんはまやん |
提出日時 | 2018-09-30 11:06:01 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 525 ms / 2,000 ms |
コード長 | 5,894 bytes |
コンパイル時間 | 3,224 ms |
コンパイル使用メモリ | 206,872 KB |
実行使用メモリ | 70,384 KB |
最終ジャッジ日時 | 2024-10-12 09:00:04 |
合計ジャッジ時間 | 27,765 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 11 ms
5,376 KB |
testcase_06 | AC | 11 ms
5,504 KB |
testcase_07 | AC | 11 ms
5,504 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 | 6 ms
5,248 KB |
testcase_12 | AC | 4 ms
5,248 KB |
testcase_13 | AC | 10 ms
5,248 KB |
testcase_14 | AC | 4 ms
5,248 KB |
testcase_15 | AC | 447 ms
65,068 KB |
testcase_16 | AC | 457 ms
65,492 KB |
testcase_17 | AC | 491 ms
65,280 KB |
testcase_18 | AC | 479 ms
64,672 KB |
testcase_19 | AC | 448 ms
67,620 KB |
testcase_20 | AC | 459 ms
65,512 KB |
testcase_21 | AC | 525 ms
67,692 KB |
testcase_22 | AC | 472 ms
65,888 KB |
testcase_23 | AC | 502 ms
66,656 KB |
testcase_24 | AC | 485 ms
66,008 KB |
testcase_25 | AC | 5 ms
5,248 KB |
testcase_26 | AC | 5 ms
5,248 KB |
testcase_27 | AC | 4 ms
5,248 KB |
testcase_28 | AC | 4 ms
5,248 KB |
testcase_29 | AC | 5 ms
5,248 KB |
testcase_30 | AC | 4 ms
5,248 KB |
testcase_31 | AC | 5 ms
5,248 KB |
testcase_32 | AC | 5 ms
5,248 KB |
testcase_33 | AC | 5 ms
5,248 KB |
testcase_34 | AC | 5 ms
5,248 KB |
testcase_35 | AC | 5 ms
5,248 KB |
testcase_36 | AC | 4 ms
5,248 KB |
testcase_37 | AC | 5 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 | 5 ms
5,248 KB |
testcase_42 | AC | 4 ms
5,248 KB |
testcase_43 | AC | 4 ms
5,248 KB |
testcase_44 | AC | 4 ms
5,248 KB |
testcase_45 | AC | 472 ms
69,620 KB |
testcase_46 | AC | 456 ms
65,100 KB |
testcase_47 | AC | 459 ms
68,324 KB |
testcase_48 | AC | 424 ms
66,136 KB |
testcase_49 | AC | 423 ms
65,748 KB |
testcase_50 | AC | 430 ms
68,324 KB |
testcase_51 | AC | 484 ms
68,604 KB |
testcase_52 | AC | 445 ms
65,748 KB |
testcase_53 | AC | 457 ms
67,416 KB |
testcase_54 | AC | 479 ms
68,712 KB |
testcase_55 | AC | 484 ms
68,584 KB |
testcase_56 | AC | 476 ms
68,064 KB |
testcase_57 | AC | 443 ms
64,968 KB |
testcase_58 | AC | 452 ms
67,976 KB |
testcase_59 | AC | 463 ms
69,488 KB |
testcase_60 | AC | 450 ms
69,168 KB |
testcase_61 | AC | 453 ms
68,840 KB |
testcase_62 | AC | 435 ms
65,232 KB |
testcase_63 | AC | 495 ms
69,616 KB |
testcase_64 | AC | 474 ms
68,836 KB |
testcase_65 | AC | 337 ms
65,496 KB |
testcase_66 | AC | 346 ms
67,020 KB |
testcase_67 | AC | 370 ms
66,752 KB |
testcase_68 | AC | 369 ms
67,892 KB |
testcase_69 | AC | 442 ms
68,448 KB |
testcase_70 | AC | 412 ms
70,252 KB |
testcase_71 | AC | 158 ms
38,476 KB |
testcase_72 | AC | 147 ms
37,612 KB |
testcase_73 | AC | 131 ms
37,900 KB |
testcase_74 | AC | 161 ms
38,112 KB |
testcase_75 | AC | 424 ms
66,508 KB |
testcase_76 | AC | 397 ms
67,928 KB |
testcase_77 | AC | 423 ms
66,516 KB |
testcase_78 | AC | 456 ms
70,072 KB |
testcase_79 | AC | 462 ms
70,384 KB |
testcase_80 | AC | 413 ms
68,316 KB |
testcase_81 | AC | 412 ms
67,148 KB |
testcase_82 | AC | 411 ms
68,576 KB |
testcase_83 | AC | 159 ms
38,764 KB |
testcase_84 | AC | 170 ms
39,400 KB |
testcase_85 | AC | 423 ms
70,128 KB |
testcase_86 | AC | 393 ms
66,492 KB |
testcase_87 | AC | 324 ms
68,316 KB |
testcase_88 | AC | 315 ms
66,508 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 |
ソースコード
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; 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 (b<a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- template<class V> struct BIT { BIT() {} // [L, R) int NV; vector<V> bit; BIT(int n) { init(n); } void init(int n) { NV = 1; while (NV<n)NV *= 2; bit.resize(NV); } V operator[](int e) { V s = 0; e++; while (e) s += bit[e - 1], e -= e & -e; return s; } void add(int e, V v) { e++; while (e <= NV) bit[e - 1] += v, e += e & -e; } int lower_bound(V val) { V tv = 0; int i, ent = 0; for (i = NV - 1; i >= 0; i--) if (tv + bit[ent + (1 << i) - 1] <= val)tv += bit[ent + (1 << i) - 1], ent += (1 << i); return ent; } V get(int L, int R) { assert(0 <= L); assert(R <= NV); assert(L <= R); V res = 0; if (R) res += operator[](R - 1); if (L) res -= operator[](L - 1); return res; } void clear() { bit.clear(); } }; // StaticHealthy2DSegtree #define def 0 using V = ll; V comp(V& l, V& r) { return l + r; }; struct SegTree { //[l,r) int NV; vector<V> val; void init(int n) { NV = 1; while (NV < n) NV *= 2; val = vector<V>(NV * 2, def); } V get(int x, int y, int l, int r, int k) { if (r <= x || y <= l) return def; if (x <= l && r <= y)return val[k]; auto a = get(x, y, l, (l + r) / 2, k * 2); auto b = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(a, b); } V get(int x, int y) { return get(x, y, 0, NV, 1); } void update(int i, V v) { i += NV; val[i] = v; while (i>1)i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); } void compupdate(int i, V v) { update(i, comp(v, val[i + NV])); } void add(int i, V v) { update(i, val[i + NV] + v); } V operator[](int x) { return get(x, x + 1); } }; struct StaticHealthy2DSegTree { int NV; vector<SegTree> st; vector<vector<int>> index; void init(vector<vector<pair<int, V>>> &v) { int n = v.size(); NV = 1; while (NV < n) NV *= 2; index.resize(2 * NV); rep(i, 0, n) fore(p, v[i]) index[i + NV].push_back(p.first); rrep(i, NV * 2 - 1, 1) { sort(index[i].begin(), index[i].end()); index[i].erase(unique(index[i].begin(), index[i].end()), index[i].end()); fore(j, index[i]) index[i / 2].push_back(j); } st.resize(2 * NV); rep(i, 1, NV * 2) st[i].init(index[i].size()); rep(i, 0, n) fore(p, v[i]) { int j = lower_bound(index[i + NV].begin(), index[i + NV].end(), p.first) - index[i + NV].begin(); st[i + NV].update(j, p.second); } rrep(i, NV * 2 - 1, 2) { rep(j, 0, index[i].size()) { V v = st[i][j]; int k = lower_bound(index[i / 2].begin(), index[i / 2].end(), index[i][j]) - index[i / 2].begin(); st[i / 2].compupdate(k, v); } } } V get(int sx, int tx, int sy, int ty, int k, int l, int r) { assert(k < NV * 2); assert(l < r); if (r <= sx or tx <= l) return def; if (sx <= l and r <= tx) { int syy = lower_bound(index[k].begin(), index[k].end(), sy) - index[k].begin(); int tyy = lower_bound(index[k].begin(), index[k].end(), ty) - index[k].begin(); return st[k].get(syy, tyy); } int md = (l + r) / 2; V le = get(sx, tx, sy, ty, k * 2, l, md); V ri = get(sx, tx, sy, ty, k * 2 + 1, md, r); return comp(le, ri); } V get(int sx, int tx, int sy, int ty) { return get(sx, tx, sy, ty, 1, 0, NV); } }; /*--------------------------------------------------------------------------------------------------- ∧_∧ ∧_∧ (´<_` ) Welcome to My Coding Space! ( ´_ゝ`) / ⌒i / \ | | / / ̄ ̄ ̄ ̄/ | __(__ニつ/ _/ .| .|____ \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, K, A[101010], M; BIT<int> bit(101010); StaticHealthy2DSegTree st; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> K; rep(i, 0, N) cin >> A[i]; vector<int> dic; rep(i, 0, N) dic.push_back(A[i]); sort(all(dic)); dic.erase(unique(all(dic)), dic.end()); M = dic.size(); vector<vector<pair<int, ll>>> v(N); rep(i, 0, N) { int y = lower_bound(all(dic), A[i]) - dic.begin(); v[i].push_back({y, A[i]}); A[i] = y; } st.init(v); ll ans = infl; rep(i, 0, K - 1) bit.add(A[i], 1); rep(L, 0, N - K + 1) { int R = L + K; bit.add(A[R - 1], 1); int ng = 0, ok = M; while (ng + 1 != ok) { int md = (ng + ok) / 2; if (K / 2 < bit.get(0, md)) ok = md; else ng = md; } ok--; ll cent = dic[ok]; ll low = st.get(L, R, 0, ok); ll high = st.get(L, R, ok + 1, inf); ll cst = bit.get(0, ok) * cent - low + high - bit.get(ok + 1, M) * cent; chmin(ans, cst); bit.add(A[L], -1); } cout << ans << endl; }