結果
問題 | No.738 平らな農地 |
ユーザー | kcvlex |
提出日時 | 2020-05-15 23:28:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 433 ms / 2,000 ms |
コード長 | 11,049 bytes |
コンパイル時間 | 1,954 ms |
コンパイル使用メモリ | 160,052 KB |
実行使用メモリ | 36,224 KB |
最終ジャッジ日時 | 2024-09-19 13:45:04 |
合計ジャッジ時間 | 18,310 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 8 ms
5,376 KB |
testcase_06 | AC | 10 ms
5,376 KB |
testcase_07 | AC | 6 ms
5,376 KB |
testcase_08 | AC | 5 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 4 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 9 ms
5,376 KB |
testcase_14 | AC | 5 ms
5,376 KB |
testcase_15 | AC | 200 ms
33,152 KB |
testcase_16 | AC | 195 ms
33,792 KB |
testcase_17 | AC | 243 ms
33,536 KB |
testcase_18 | AC | 246 ms
32,768 KB |
testcase_19 | AC | 308 ms
35,328 KB |
testcase_20 | AC | 212 ms
33,664 KB |
testcase_21 | AC | 274 ms
35,584 KB |
testcase_22 | AC | 216 ms
33,792 KB |
testcase_23 | AC | 267 ms
34,560 KB |
testcase_24 | AC | 276 ms
34,176 KB |
testcase_25 | AC | 3 ms
5,376 KB |
testcase_26 | AC | 4 ms
5,376 KB |
testcase_27 | AC | 3 ms
5,376 KB |
testcase_28 | AC | 3 ms
5,376 KB |
testcase_29 | AC | 3 ms
5,376 KB |
testcase_30 | AC | 4 ms
5,376 KB |
testcase_31 | AC | 3 ms
5,376 KB |
testcase_32 | AC | 3 ms
5,376 KB |
testcase_33 | AC | 3 ms
5,376 KB |
testcase_34 | AC | 4 ms
5,376 KB |
testcase_35 | AC | 3 ms
5,376 KB |
testcase_36 | AC | 3 ms
5,376 KB |
testcase_37 | AC | 3 ms
5,376 KB |
testcase_38 | AC | 3 ms
5,376 KB |
testcase_39 | AC | 3 ms
5,376 KB |
testcase_40 | AC | 3 ms
5,376 KB |
testcase_41 | AC | 4 ms
5,376 KB |
testcase_42 | AC | 3 ms
5,376 KB |
testcase_43 | AC | 3 ms
5,376 KB |
testcase_44 | AC | 4 ms
5,376 KB |
testcase_45 | AC | 182 ms
35,968 KB |
testcase_46 | AC | 188 ms
33,152 KB |
testcase_47 | AC | 182 ms
34,944 KB |
testcase_48 | AC | 156 ms
33,792 KB |
testcase_49 | AC | 153 ms
33,664 KB |
testcase_50 | AC | 153 ms
34,816 KB |
testcase_51 | AC | 196 ms
35,328 KB |
testcase_52 | AC | 175 ms
33,536 KB |
testcase_53 | AC | 185 ms
34,176 KB |
testcase_54 | AC | 192 ms
35,328 KB |
testcase_55 | AC | 200 ms
35,200 KB |
testcase_56 | AC | 190 ms
34,688 KB |
testcase_57 | AC | 174 ms
33,024 KB |
testcase_58 | AC | 175 ms
34,688 KB |
testcase_59 | AC | 177 ms
36,096 KB |
testcase_60 | AC | 166 ms
35,712 KB |
testcase_61 | AC | 169 ms
35,584 KB |
testcase_62 | AC | 172 ms
33,152 KB |
testcase_63 | AC | 204 ms
35,968 KB |
testcase_64 | AC | 187 ms
35,456 KB |
testcase_65 | AC | 362 ms
33,664 KB |
testcase_66 | AC | 373 ms
34,816 KB |
testcase_67 | AC | 233 ms
33,280 KB |
testcase_68 | AC | 226 ms
34,176 KB |
testcase_69 | AC | 372 ms
34,688 KB |
testcase_70 | AC | 280 ms
35,840 KB |
testcase_71 | AC | 251 ms
35,328 KB |
testcase_72 | AC | 233 ms
33,152 KB |
testcase_73 | AC | 200 ms
33,792 KB |
testcase_74 | AC | 267 ms
34,816 KB |
testcase_75 | AC | 340 ms
33,024 KB |
testcase_76 | AC | 251 ms
34,176 KB |
testcase_77 | AC | 386 ms
33,024 KB |
testcase_78 | AC | 433 ms
35,712 KB |
testcase_79 | AC | 414 ms
35,968 KB |
testcase_80 | AC | 298 ms
34,560 KB |
testcase_81 | AC | 359 ms
33,536 KB |
testcase_82 | AC | 356 ms
34,816 KB |
testcase_83 | AC | 258 ms
36,224 KB |
testcase_84 | AC | 260 ms
34,944 KB |
testcase_85 | AC | 409 ms
35,712 KB |
testcase_86 | AC | 375 ms
33,024 KB |
testcase_87 | AC | 59 ms
34,560 KB |
testcase_88 | AC | 55 ms
33,024 KB |
testcase_89 | AC | 2 ms
5,376 KB |
testcase_90 | AC | 2 ms
5,376 KB |
testcase_91 | AC | 2 ms
5,376 KB |
ソースコード
#include <limits> #include <initializer_list> #include <utility> #include <bitset> #include <tuple> #include <type_traits> #include <functional> #include <string> #include <array> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <iterator> #include <algorithm> #include <complex> #include <random> #include <numeric> #include <iostream> #include <iomanip> #include <sstream> #include <regex> #include <cassert> #include <cstddef> #include <variant> // Yay!! #define endl codeforces // macros for iterator #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) // alias using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair<int, int>; using tii = std::tuple<int, int, int>; using pll = std::pair<ll, ll>; using tll = std::tuple<ll, ll, ll>; template <typename T> using vec = std::vector<T>; template <typename T> using vvec = vec<vec<T>>; // variadic min/max template <typename T> const T& var_min(const T &t) { return t; } template <typename T> const T& var_max(const T &t) { return t; } template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } // variadic chmin/chmax template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } // multi demension array template <typename T, std::size_t Head, std::size_t... Tail> struct multi_dim_array { using type = std::array<typename multi_dim_array<T, Tail...>::type, Head>; }; template <typename T, std::size_t Head> struct multi_dim_array<T, Head> { using type = std::array<T, Head>; }; template <typename T, std::size_t... Args> using mdarray = typename multi_dim_array<T, Args...>::type; // fill container template <typename T, typename F, typename... Args> void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable<F, Args...>::value) { t = f(args...); } else { for (ssize_t i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } } // make multi dimension vector template <typename T> vec<T> make_v(ssize_t sz) { return vec<T>(sz); } template <typename T, typename... Tail> auto make_v(ssize_t hs, Tail&&... ts) { auto v = std::move(make_v<T>(std::forward<Tail>(ts)...)); return vec<decltype(v)>(hs, v); } // init namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } namespace utility { template <typename T> using validate_integer = typename std::enable_if<std::is_integral<T>::value, ll>::type; template <typename T> auto popcount(T n) -> validate_integer<T> { return __builtin_popcount(n); } // 0 indexed template <typename T> auto msb(T n) -> validate_integer<T> { return 64 - __builtin_clzll(n) - 1; } template <typename T> constexpr auto ceil_pow2(T s) -> validate_integer<T> { ll ret = 1; while (ret < s) ret *= 2; return ret; } } namespace utility { constexpr ll ceil_div(ll a, ll b) { return a / b + !!(a % b); } } namespace succinct { template <typename T> constexpr T ceil_log2(T n) { for (int i = 0; i < 63; i++) { T mask = 1ll << i; if (n <= mask) return i; } return -1; } template <typename T> constexpr T ceil_even(T n) { return n + (n & 1); } template <std::size_t MaxSize> struct FullyIndexableDictionary { using size_type = ssize_t; private: constexpr static size_type sz_log2 = ceil_even(ceil_log2<size_type>(MaxSize)); constexpr static size_type chunk_sz = sz_log2 * sz_log2; constexpr static size_type block_sz = sz_log2 / 2; constexpr static size_type block_per_chunk = chunk_sz / block_sz; size_type sz, cnt1; vec<size_type> chunk, block, dat; public: FullyIndexableDictionary() { } template <typename F> FullyIndexableDictionary(F f, size_type sz) : sz(sz), cnt1(0), chunk(utility::ceil_div(sz, chunk_sz)), block(chunk.size() * block_per_chunk), dat(block.size()) { size_type idx = 0; for (size_type i = 0; i < chunk.size(); i++) { chunk[i] = cnt1; size_type tmp = 0; for (size_type j = 0; j < block_per_chunk; j++, idx++) { block[idx] = tmp; for (size_type k = 0; k < block_sz; k++) { auto v = f(idx * block_sz + k); size_type mask = (1ll << k) * v; dat[idx] |= mask; tmp += v; } } cnt1 += tmp; } } size_type rank(bool b, size_type pos) const { if (!b) return pos - rank(!b, pos); if (pos == sz) return sum(b); size_type ret = 0; ret += chunk[pos / chunk_sz]; ret += block[pos / block_sz]; auto mask = (1ll << (pos % block_sz)) - 1; return ret + utility::popcount(dat[pos / block_sz] & mask); } size_type rank(bool b, size_type l, size_type r) const { return rank(b, r) - rank(b, l); } size_type select(bool b, size_type n) const { if (rank(b, sz) < n) return -1; size_type ok = sz, ng = 0; while (1 < std::abs(ok - ng)) { size_type mid = (ok + ng) / 2; (n <= rank(b, mid) ? ok : ng) = mid; } return ok; } bool operator [](size_type i) const { size_type offset = i % block_sz; return (dat[i / block_sz] >> offset) & 1; } size_type sum(bool b) const { return b ? cnt1 : sz - cnt1; } }; } namespace succinct { template <typename T, std::size_t MaxSize, std::size_t MaxValueLog = 32> class WaveletMatrix { using data_type = FullyIndexableDictionary<MaxSize>; public: using size_type = typename data_type::size_type; private: size_type sz; std::array<data_type, MaxValueLog> mat; std::array<vec<size_type>, MaxValueLog> acc_sum; void update_range_aux(bool bt, size_type &l, size_type &r, size_type i) const { const auto &dic = mat[i]; l = dic.rank(bt, l) + bt * dic.sum(0); r = dic.rank(bt, r) + bt * dic.sum(0); } size_type range_freq_aux(size_type l, size_type r, T bound) const { if ((1ll << MaxValueLog) - 1 <= bound) return r - l; size_type ret = 0; for (size_type i = 0; i < MaxValueLog && l < r; i++) { T mask = 1ll << (MaxValueLog - (i + 1)); bool bt = !!(bound & mask); if (bt) ret += mat[i].rank(0, l, r); update_range_aux(bt, l, r, i); } return ret; } size_type range_sum_aux(size_type l, size_type r, T bound) const { if ((1ll << MaxValueLog) - 1 <= bound) return acc_sum[0][r] - acc_sum[0][l]; T ret = 0; for (size_type i = 0; i < MaxValueLog && l < r; i++) { T mask = 1ll << (MaxValueLog - (i + 1)); bool bt = !!(bound & mask); if (bt) ret += acc_sum[i][r] - acc_sum[i][l]; update_range_aux(bt, l, r, i); } return ret; } public: WaveletMatrix(vec<T> dat) : sz(dat.size()) { vec<T> buf(sz); for (size_type i = 0; i < MaxValueLog; i++) { size_type mask = 1ll << (MaxValueLog - i - 1); auto f = [&](size_type idx) -> bool { if (dat.size() <= idx) return false; return !!(dat[idx] & mask); }; mat[i] = std::move(data_type(f, dat.size())); size_type l = 0, r = 0; acc_sum[i].resize(sz + 1, 0); for (size_type j = 0; j < dat.size(); j++) { auto e = dat[j]; if (e & mask) { r++; buf[sz - r] = e; } else { buf[l] = e; acc_sum[i][j + 1] += e; l++; } } for (size_type k = 0; k < dat.size(); k++) acc_sum[i][k + 1] += acc_sum[i][k]; std::reverse(buf.begin() + l, buf.end()); std::swap(buf, dat); } } size_type rank(T t, size_type pos) const { size_type mask = 1ll << (MaxValueLog - 1); size_type l = 0, r = pos; for (size_type i = 0; i < MaxValueLog; i++, mask /= 2) { bool bt = !!(t & mask); update_range_aux(bt, l, r, i); } return r - l; } size_type select(T t, size_type n) const { std::array<size_type, MaxValueLog> larr, rarr; size_type l = 0, r = sz; for (size_type i = 0, mask = (1ll << (MaxValueLog - 1)); i < MaxValueLog; i++, mask /= 2) { larr[i] = l, rarr[i] = r; bool bt = !!(t & mask); update_range_aux(bt, l, r, i); } for (size_type i = 0, mask = 1; i < MaxValueLog; i++, mask *= 2) { size_type idx = MaxValueLog - (i + 1); const auto &dic = mat[idx]; bool bt = !!(t & mask); n += dic.rank(bt, larr[idx]); size_type ra = dic.sel(bt, n); if (ra == -1) return -1; n = ra; } return n; } // k >= 0 T quantile(size_type l, size_type r, size_type k) const { T ret = 0; for (size_type i = 0; i < MaxValueLog && l < r; i++) { T mask = 1ll << (MaxValueLog - (i + 1)); const auto &dic = mat[i]; size_type cl = dic.rank(1, l), cr = dic.rank(1, r); if (k < cr - cl) { l = cl + dic.sum(0); r = cr + dic.sum(0); ret |= mask; } else { k -= cr - cl; l -= cl; r -= cr; } } return ret; } // [l, r), [min, max) size_type range_freq(size_type l, size_type r, T min, T max) const { return range_freq_aux(l, r, max) - range_freq_aux(l, r, min); } size_type range_sum(size_type l, size_type r, T min, T max) const { return range_sum_aux(l, r, max) - range_sum_aux(l, r, min); } }; } const std::size_t SIZE = 1e5 + 10; const ll inf = 5e15; int main() { ll n, k; std::cin >> n >> k; vec<ll> av(n); for (auto &&e : av) std::cin >> e; ll ans = inf; succinct::WaveletMatrix<ll, SIZE> wm(av); for (ll i = 0; i + k <= n; i++) { ll mode = wm.quantile(i, i + k, k / 2); ll cost = 0; cost += mode * wm.range_freq(i, i + k, 0, mode) - wm.range_sum(i, i + k, 0, mode); cost += wm.range_sum(i, i + k, mode, inf) - mode * wm.range_freq(i, i + k, mode, inf); chmin(ans, cost); } std::cout << ans << "\n"; return 0; }