結果
問題 | No.738 平らな農地 |
ユーザー | FF256grhy |
提出日時 | 2018-09-29 05:29:35 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 435 ms / 2,000 ms |
コード長 | 4,022 bytes |
コンパイル時間 | 1,878 ms |
コンパイル使用メモリ | 182,684 KB |
実行使用メモリ | 15,232 KB |
最終ジャッジ日時 | 2024-10-12 08:05:56 |
合計ジャッジ時間 | 19,311 ms |
ジャッジサーバーID (参考情報) |
judge4 / 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 | 7 ms
5,248 KB |
testcase_06 | AC | 8 ms
5,248 KB |
testcase_07 | AC | 7 ms
5,248 KB |
testcase_08 | AC | 4 ms
5,248 KB |
testcase_09 | AC | 3 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 4 ms
5,248 KB |
testcase_12 | AC | 3 ms
5,248 KB |
testcase_13 | AC | 6 ms
5,248 KB |
testcase_14 | AC | 3 ms
5,248 KB |
testcase_15 | AC | 260 ms
12,416 KB |
testcase_16 | AC | 268 ms
12,544 KB |
testcase_17 | AC | 323 ms
12,416 KB |
testcase_18 | AC | 323 ms
12,416 KB |
testcase_19 | AC | 386 ms
12,672 KB |
testcase_20 | AC | 280 ms
12,672 KB |
testcase_21 | AC | 383 ms
12,800 KB |
testcase_22 | AC | 286 ms
12,672 KB |
testcase_23 | AC | 354 ms
12,672 KB |
testcase_24 | AC | 354 ms
12,544 KB |
testcase_25 | AC | 3 ms
5,248 KB |
testcase_26 | AC | 3 ms
5,248 KB |
testcase_27 | AC | 3 ms
5,248 KB |
testcase_28 | AC | 3 ms
5,248 KB |
testcase_29 | AC | 3 ms
5,248 KB |
testcase_30 | AC | 3 ms
5,248 KB |
testcase_31 | AC | 3 ms
5,248 KB |
testcase_32 | AC | 4 ms
5,248 KB |
testcase_33 | AC | 3 ms
5,248 KB |
testcase_34 | AC | 3 ms
5,248 KB |
testcase_35 | AC | 3 ms
5,248 KB |
testcase_36 | AC | 3 ms
5,248 KB |
testcase_37 | AC | 3 ms
5,248 KB |
testcase_38 | AC | 3 ms
5,248 KB |
testcase_39 | AC | 3 ms
5,248 KB |
testcase_40 | AC | 3 ms
5,248 KB |
testcase_41 | AC | 3 ms
5,248 KB |
testcase_42 | AC | 3 ms
5,248 KB |
testcase_43 | AC | 3 ms
5,248 KB |
testcase_44 | AC | 3 ms
5,248 KB |
testcase_45 | AC | 259 ms
13,312 KB |
testcase_46 | AC | 263 ms
12,800 KB |
testcase_47 | AC | 260 ms
13,184 KB |
testcase_48 | AC | 233 ms
12,928 KB |
testcase_49 | AC | 222 ms
12,928 KB |
testcase_50 | AC | 237 ms
13,184 KB |
testcase_51 | AC | 289 ms
13,184 KB |
testcase_52 | AC | 246 ms
12,928 KB |
testcase_53 | AC | 261 ms
13,056 KB |
testcase_54 | AC | 283 ms
13,184 KB |
testcase_55 | AC | 293 ms
13,184 KB |
testcase_56 | AC | 281 ms
13,056 KB |
testcase_57 | AC | 244 ms
12,800 KB |
testcase_58 | AC | 259 ms
13,056 KB |
testcase_59 | AC | 251 ms
13,312 KB |
testcase_60 | AC | 241 ms
13,280 KB |
testcase_61 | AC | 253 ms
13,056 KB |
testcase_62 | AC | 239 ms
12,800 KB |
testcase_63 | AC | 283 ms
13,312 KB |
testcase_64 | AC | 277 ms
13,312 KB |
testcase_65 | AC | 370 ms
12,544 KB |
testcase_66 | AC | 391 ms
12,672 KB |
testcase_67 | AC | 224 ms
14,692 KB |
testcase_68 | AC | 217 ms
14,900 KB |
testcase_69 | AC | 318 ms
14,976 KB |
testcase_70 | AC | 263 ms
15,124 KB |
testcase_71 | AC | 261 ms
9,088 KB |
testcase_72 | AC | 221 ms
8,704 KB |
testcase_73 | AC | 197 ms
8,852 KB |
testcase_74 | AC | 268 ms
8,960 KB |
testcase_75 | AC | 298 ms
14,592 KB |
testcase_76 | AC | 256 ms
14,888 KB |
testcase_77 | AC | 316 ms
14,592 KB |
testcase_78 | AC | 347 ms
15,104 KB |
testcase_79 | AC | 344 ms
15,232 KB |
testcase_80 | AC | 266 ms
14,720 KB |
testcase_81 | AC | 300 ms
14,720 KB |
testcase_82 | AC | 334 ms
14,976 KB |
testcase_83 | AC | 238 ms
8,960 KB |
testcase_84 | AC | 225 ms
8,956 KB |
testcase_85 | AC | 435 ms
14,976 KB |
testcase_86 | AC | 395 ms
14,464 KB |
testcase_87 | AC | 144 ms
14,720 KB |
testcase_88 | AC | 128 ms
14,464 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> 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); LL lc = ll.FI; LL ls = ll.SE; LL hc = hh.FI; LL hs = hh.SE; setmin(ans, (b[m] * lc - ls) + (hs - b[m] * hc)); st.apply(mp[a[i - k + 1]], AP(A.FI--; A.SE -= a[i - k + 1])); } } cout << ans << endl; return 0; }