結果
問題 | No.738 平らな農地 |
ユーザー | FF256grhy |
提出日時 | 2018-09-29 05:45:36 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 438 ms / 2,000 ms |
コード長 | 3,962 bytes |
コンパイル時間 | 2,110 ms |
コンパイル使用メモリ | 183,244 KB |
実行使用メモリ | 15,284 KB |
最終ジャッジ日時 | 2024-10-12 08:06:16 |
合計ジャッジ時間 | 19,735 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 7 ms
6,820 KB |
testcase_06 | AC | 8 ms
6,820 KB |
testcase_07 | AC | 6 ms
6,816 KB |
testcase_08 | AC | 4 ms
6,816 KB |
testcase_09 | AC | 3 ms
6,820 KB |
testcase_10 | AC | 2 ms
6,816 KB |
testcase_11 | AC | 3 ms
6,816 KB |
testcase_12 | AC | 2 ms
6,816 KB |
testcase_13 | AC | 6 ms
6,816 KB |
testcase_14 | AC | 3 ms
6,820 KB |
testcase_15 | AC | 257 ms
12,536 KB |
testcase_16 | AC | 270 ms
12,708 KB |
testcase_17 | AC | 324 ms
12,580 KB |
testcase_18 | AC | 316 ms
12,476 KB |
testcase_19 | AC | 382 ms
12,872 KB |
testcase_20 | AC | 272 ms
12,636 KB |
testcase_21 | AC | 383 ms
12,904 KB |
testcase_22 | AC | 292 ms
12,712 KB |
testcase_23 | AC | 357 ms
12,724 KB |
testcase_24 | AC | 350 ms
12,632 KB |
testcase_25 | AC | 3 ms
6,820 KB |
testcase_26 | AC | 3 ms
6,820 KB |
testcase_27 | AC | 3 ms
6,820 KB |
testcase_28 | AC | 3 ms
6,816 KB |
testcase_29 | AC | 3 ms
6,816 KB |
testcase_30 | AC | 3 ms
6,816 KB |
testcase_31 | AC | 3 ms
6,816 KB |
testcase_32 | AC | 3 ms
6,816 KB |
testcase_33 | AC | 3 ms
6,816 KB |
testcase_34 | AC | 3 ms
6,816 KB |
testcase_35 | AC | 3 ms
6,820 KB |
testcase_36 | AC | 3 ms
6,816 KB |
testcase_37 | AC | 3 ms
6,820 KB |
testcase_38 | AC | 3 ms
6,816 KB |
testcase_39 | AC | 3 ms
6,816 KB |
testcase_40 | AC | 3 ms
6,816 KB |
testcase_41 | AC | 3 ms
6,820 KB |
testcase_42 | AC | 3 ms
6,820 KB |
testcase_43 | AC | 3 ms
6,816 KB |
testcase_44 | AC | 3 ms
6,816 KB |
testcase_45 | AC | 259 ms
13,448 KB |
testcase_46 | AC | 260 ms
12,924 KB |
testcase_47 | AC | 262 ms
13,224 KB |
testcase_48 | AC | 236 ms
13,060 KB |
testcase_49 | AC | 222 ms
13,024 KB |
testcase_50 | AC | 225 ms
13,280 KB |
testcase_51 | AC | 285 ms
13,328 KB |
testcase_52 | AC | 249 ms
13,008 KB |
testcase_53 | AC | 257 ms
13,200 KB |
testcase_54 | AC | 276 ms
13,324 KB |
testcase_55 | AC | 290 ms
13,268 KB |
testcase_56 | AC | 285 ms
13,368 KB |
testcase_57 | AC | 241 ms
12,892 KB |
testcase_58 | AC | 254 ms
13,184 KB |
testcase_59 | AC | 253 ms
13,484 KB |
testcase_60 | AC | 241 ms
13,432 KB |
testcase_61 | AC | 257 ms
13,388 KB |
testcase_62 | AC | 241 ms
12,944 KB |
testcase_63 | AC | 278 ms
13,524 KB |
testcase_64 | AC | 271 ms
13,400 KB |
testcase_65 | AC | 370 ms
12,692 KB |
testcase_66 | AC | 388 ms
12,796 KB |
testcase_67 | AC | 219 ms
14,648 KB |
testcase_68 | AC | 215 ms
14,856 KB |
testcase_69 | AC | 313 ms
14,980 KB |
testcase_70 | AC | 259 ms
15,284 KB |
testcase_71 | AC | 263 ms
9,132 KB |
testcase_72 | AC | 219 ms
8,804 KB |
testcase_73 | AC | 197 ms
9,000 KB |
testcase_74 | AC | 268 ms
9,032 KB |
testcase_75 | AC | 298 ms
14,704 KB |
testcase_76 | AC | 255 ms
14,888 KB |
testcase_77 | AC | 322 ms
14,584 KB |
testcase_78 | AC | 344 ms
15,224 KB |
testcase_79 | AC | 343 ms
15,144 KB |
testcase_80 | AC | 263 ms
14,960 KB |
testcase_81 | AC | 297 ms
14,724 KB |
testcase_82 | AC | 337 ms
14,936 KB |
testcase_83 | AC | 237 ms
9,076 KB |
testcase_84 | AC | 225 ms
9,104 KB |
testcase_85 | AC | 438 ms
15,220 KB |
testcase_86 | AC | 386 ms
14,524 KB |
testcase_87 | AC | 137 ms
15,000 KB |
testcase_88 | AC | 125 ms
14,472 KB |
testcase_89 | AC | 2 ms
6,820 KB |
testcase_90 | AC | 2 ms
6,816 KB |
testcase_91 | AC | 1 ms
6,820 KB |
ソースコード
#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> class SegmentTree { private: T * a = NULL; int N = -1, S; function<T(T, T)> F; T I; bool is_available = false; public: SegmentTree() { } SegmentTree(int n, function<T(T, T)> func, T id) { init(n, func, id); } void init(int size, function<T(T, T)> func, T id) { assert(size > 0); N = size; F = func; I = id; S = 1; while(S < size) { S *= 2; } delete[] a; a = new T[S * 2]; inc(i, S * 2) { a[i] = I; } is_available = true; } T operator[](int p) { assert(inID(p, 0, N)); p += S; return a[p]; } T & ref(int p) { is_available = false; assert(inID(p, 0, N)); p += S; return a[p]; } void calc() { decID(i, 1, S) { a[i] = F(a[i * 2], a[i * 2 + 1]); } is_available = true; } void apply(int p, function<void(T &)> op) { assert(inID(p, 0, N)); p += S; op(a[p]); while(p != 1) { p /= 2; a[p] = F(a[p * 2], a[p * 2 + 1]); } } T fold_ID(int l, int r, bool loop = false) { assert(is_available); assert(inII(l, 0, N)); assert(inII(r, 0, N)); if(loop && l >= r) { return F(fold_ID(l, N), fold_ID(0, r)); } assert(l <= r); l += S; r += S; T v = I, w = I; while(l < r) { if(l + 1 == r) { v = F(v, a[l]); break; } if(l % 2 == 1) { v = F(v, a[l]); } if(r % 2 == 1) { w = F(a[r - 1], w); } l = (l + 1) / 2; r = r / 2; } return F(v, w); } T fold_II(int l, int r, bool loop = false) { return fold_ID(l , r + 1, loop); } T fold_CD(int l, int r, bool loop = false) { return fold_ID(l + 1, r , loop); } T fold_CI(int l, int r, bool loop = false) { return fold_ID(l + 1, r + 1, loop); } }; #define OP(op) [&](auto A, auto B) { return op; } #define AP(op) [&](auto & A) { op; } // ---- ---- LL n, k, a[100000], b[100000]; map<LL, LL> mp; int main() { cin >> n >> k; inc(i, n) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); inc(i, n) { mp[b[i]] = i; } SegmentTree<pair<LL, LL>> st(n, OP(MP(A.FI + B.FI, A.SE + B.SE)), MP(0, 0)); LL ans = 1e15; inc(i, n) { st.apply(mp[a[i]], AP(A.FI++; A.SE += a[i])); if(i >= k - 1) { int L = -1, H = n; while(H - L > 1) { int M = (H + L) / 2; (st.fold_II(0, M).FI <= k / 2 ? L : H) = M; } int m = H; auto ll = st.fold_ID(0, m); auto hh = st.fold_ID(m, n); setmin(ans, (b[m] * ll.FI - ll.SE) - (b[m] * hh.FI - hh.SE)); st.apply(mp[a[i - k + 1]], AP(A.FI--; A.SE -= a[i - k + 1])); } } cout << ans << endl; return 0; }