結果
問題 | No.738 平らな農地 |
ユーザー | kcvlex |
提出日時 | 2020-05-15 19:18:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 11,051 bytes |
コンパイル時間 | 1,922 ms |
コンパイル使用メモリ | 160,688 KB |
実行使用メモリ | 37,504 KB |
最終ジャッジ日時 | 2024-09-19 05:50:43 |
合計ジャッジ時間 | 32,964 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | WA | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
testcase_44 | RE | - |
testcase_45 | RE | - |
testcase_46 | RE | - |
testcase_47 | RE | - |
testcase_48 | RE | - |
testcase_49 | RE | - |
testcase_50 | RE | - |
testcase_51 | RE | - |
testcase_52 | RE | - |
testcase_53 | RE | - |
testcase_54 | RE | - |
testcase_55 | RE | - |
testcase_56 | RE | - |
testcase_57 | RE | - |
testcase_58 | RE | - |
testcase_59 | RE | - |
testcase_60 | RE | - |
testcase_61 | RE | - |
testcase_62 | RE | - |
testcase_63 | RE | - |
testcase_64 | RE | - |
testcase_65 | RE | - |
testcase_66 | RE | - |
testcase_67 | RE | - |
testcase_68 | RE | - |
testcase_69 | RE | - |
testcase_70 | RE | - |
testcase_71 | RE | - |
testcase_72 | RE | - |
testcase_73 | RE | - |
testcase_74 | RE | - |
testcase_75 | RE | - |
testcase_76 | RE | - |
testcase_77 | RE | - |
testcase_78 | RE | - |
testcase_79 | RE | - |
testcase_80 | RE | - |
testcase_81 | RE | - |
testcase_82 | RE | - |
testcase_83 | RE | - |
testcase_84 | RE | - |
testcase_85 | RE | - |
testcase_86 | RE | - |
testcase_87 | RE | - |
testcase_88 | RE | - |
testcase_89 | AC | 1 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 <std::size_t MaxSize> struct FullyIndexableDictionary { using size_type = ssize_t; private: constexpr static size_type sz_log2 = 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> dat; vec<size_type> chunk; vvec<size_type> block; public: FullyIndexableDictionary() { } template <typename F> FullyIndexableDictionary(F f, size_type sz) : sz(sz), cnt1(0), dat(utility::ceil_div(sz, block_sz), 0), chunk(utility::ceil_div(sz, chunk_sz)), block(make_v<size_type>(chunk.size(), block_per_chunk)) { for (size_type i = 0; i < chunk.size(); i++) { chunk[i] = cnt1; size_type tmp = 0; for (size_type j = 0; j < block[i].size(); j++) { block[i][j] = tmp; for (size_type k = 0; k < block_sz; k++) { size_type idx = i * chunk_sz + j * block_sz; auto v = f(idx + k); size_type mask = (1ll << k) * v; dat[i * block_per_chunk + j] |= mask; tmp += v; } } cnt1 += tmp; } } size_type rank(bool b, size_type pos) const { if (!b) return pos - rank(!b, pos); size_type ret = 0, idx = 0; size_type i = pos / chunk_sz; pos %= chunk_sz; idx += i * chunk_sz * block_per_chunk; ret += chunk[i]; size_type j = pos / block_sz; pos %= block_sz; idx += j * block_sz; ret += block[i][j]; auto mask = (1ll << pos) - 1; return ret + utility::popcount(dat[idx] & 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 { 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 { 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); 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; 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; }