結果

問題 No.919 You Are A Project Manager
ユーザー kimiyukikimiyuki
提出日時 2019-11-08 07:44:44
言語 C++17(1z)
(gcc 10.2.0 + boost 1.73.0)
結果
AC  
実行時間 746 ms / 3,000 ms
コード長 3,657 Byte
コンパイル時間 3,596 ms
使用メモリ 14,204 KB
最終ジャッジ日時 2021-04-11 23:12:30
合計ジャッジ時間 21,136 ms
ジャッジサーバーID
(参考情報)
judge6 / judge8
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 697 ms
11,168 KB
testcase_01 AC 1 ms
3,332 KB
testcase_02 AC 2 ms
3,376 KB
testcase_03 AC 1 ms
3,300 KB
testcase_04 AC 65 ms
4,796 KB
testcase_05 AC 74 ms
5,188 KB
testcase_06 AC 5 ms
3,528 KB
testcase_07 AC 197 ms
6,424 KB
testcase_08 AC 29 ms
4,000 KB
testcase_09 AC 20 ms
3,748 KB
testcase_10 AC 42 ms
3,956 KB
testcase_11 AC 38 ms
3,944 KB
testcase_12 AC 2 ms
3,284 KB
testcase_13 AC 52 ms
4,400 KB
testcase_14 AC 3 ms
3,424 KB
testcase_15 AC 12 ms
3,592 KB
testcase_16 AC 84 ms
4,820 KB
testcase_17 AC 582 ms
14,068 KB
testcase_18 AC 560 ms
11,528 KB
testcase_19 AC 699 ms
11,148 KB
testcase_20 AC 656 ms
10,952 KB
testcase_21 AC 701 ms
11,264 KB
testcase_22 AC 219 ms
6,656 KB
testcase_23 AC 211 ms
6,464 KB
testcase_24 AC 234 ms
7,016 KB
testcase_25 AC 218 ms
6,664 KB
testcase_26 AC 634 ms
10,408 KB
testcase_27 AC 531 ms
9,884 KB
testcase_28 AC 719 ms
11,000 KB
testcase_29 AC 576 ms
14,168 KB
testcase_30 AC 555 ms
11,548 KB
testcase_31 AC 566 ms
14,160 KB
testcase_32 AC 554 ms
14,068 KB
testcase_33 AC 247 ms
6,764 KB
testcase_34 AC 248 ms
6,760 KB
testcase_35 AC 745 ms
11,132 KB
testcase_36 AC 746 ms
11,260 KB
testcase_37 AC 740 ms
11,284 KB
testcase_38 AC 741 ms
11,252 KB
testcase_39 AC 2 ms
3,380 KB
testcase_40 AC 12 ms
3,632 KB
testcase_41 AC 4 ms
3,500 KB
testcase_42 AC 4 ms
3,456 KB
testcase_43 AC 6 ms
3,596 KB
testcase_44 AC 17 ms
3,724 KB
testcase_45 AC 5 ms
3,444 KB
testcase_46 AC 14 ms
3,684 KB
testcase_47 AC 218 ms
6,776 KB
testcase_48 AC 213 ms
6,776 KB
testcase_49 AC 234 ms
6,768 KB
testcase_50 AC 218 ms
6,764 KB
testcase_51 AC 194 ms
6,396 KB
testcase_52 AC 137 ms
5,864 KB
testcase_53 AC 192 ms
6,504 KB
testcase_54 AC 12 ms
3,724 KB
testcase_55 AC 1 ms
3,380 KB
testcase_56 AC 1 ms
3,380 KB
testcase_57 AC 1 ms
3,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); }
template <class Key> using rb_tree_set = __gnu_pbds::tree<Key, __gnu_pbds::null_type, std::less<Key>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

int64_t solve(int n, const vector<int> & a) {
    // list queries for Mo's algorithm
    vector<pair<int, int> > queries;
    REP3 (k, 1, n + 1) {
        REP (i, n / k) {
            int l = k * i;
            int r = k * (i + 1);
            queries.emplace_back(l, r);
        }
        REP (i, n / k) {
            int l = n - k * (i + 1);
            int r = n - k * i;
            queries.emplace_back(l, r);
        }
    }

    // prepare the structure for Mo's algorithm
    rb_tree_set<pair<int, int> > used;
    int l = 0;
    int r = 0;
    auto add = [&](int i) {
        assert (i == l - 1 or i == r);
        used.insert(make_pair(a[i], i));
        if (i == l - 1) -- l;
        if (i == r) ++ r;
    };
    auto remove = [&](int i) {
        assert (i == l or i == r - 1);
        used.erase(make_pair(a[i], i));
        if (i == l) ++ l;
        if (i == r - 1) -- r;
    };
    auto get = [&]() {
        auto it = used.find_by_order((r - l - 1) / 2);
        assert (it != used.end());
        return it->first;
    };

    // run Mo's algorithm
    unordered_map<uint64_t, int> median;
    sort(ALL(queries));
    int sqrt_n = sqrt(n) + 3;
    for (int bl = 0, ql = 0; bl < n; ) {
        int br = min(n, bl + sqrt_n);
        int qr = ql;
        while (qr < queries.size() and queries[qr].first < br) {
            ++ qr;
        }
        sort(queries.begin() + ql, queries.begin() + qr, [&](pair<int, int> a, pair<int, int> b) {
            return a.second < b.second;
        });
        REP3 (i, ql, qr) {
            int nl, nr; tie(nl, nr) = queries[i];
            while (r < nr) add(r);
            while (nl < l) add(l - 1);
            while (nr < r) remove(r - 1);
            while (l < nl) remove(l);
            median[((uint64_t)nl << 32) | nr] = get();
        }
        bl = br;
        ql = qr;
    }

    // compute the answer
    int64_t answer = INT64_MIN;
    vector<int64_t> left, right;
    REP3 (k, 1, n + 1) {
        int cnt = n / k;

        // list left groups
        int64_t sum_left = 0;
        left.assign(cnt + 1, 0);
        REP (i, cnt) {
            int l = k * i;
            int r = k * (i + 1);
            sum_left += median.at(((uint64_t)l << 32) | r);
            left[i + 1] = max(left[i], sum_left);
        }

        // list right groups
        int64_t sum_right = 0;
        right.assign(cnt + 1, 0);
        REP (i, cnt) {
            int l = n - k * (i + 1);
            int r = n - k * i;
            sum_right += median.at(((uint64_t)l << 32) | r);
            right[i + 1] = max(right[i], sum_right);
        }

        // convolution
        REP (i, left.size()) {
            int j = right.size() - i - 1;
            chmax(answer, k * (left[i] + right[j]));
        }
    }
    return answer;
}

int main() {
    int n; cin >> n;
    vector<int> a(n);
    REP (i, n) {
        cin >> a[i];
    }
    cout << solve(n, a) << endl;
    return 0;
}
0