結果

問題 No.919 You Are A Project Manager
ユーザー kimiyukikimiyuki
提出日時 2019-11-08 07:44:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 739 ms / 3,000 ms
コード長 3,657 bytes
コンパイル時間 4,000 ms
コンパイル使用メモリ 255,596 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2023-09-10 03:56:06
合計ジャッジ時間 20,921 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 682 ms
11,216 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 65 ms
5,032 KB
testcase_05 AC 72 ms
5,136 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 193 ms
6,456 KB
testcase_08 AC 29 ms
4,384 KB
testcase_09 AC 19 ms
4,384 KB
testcase_10 AC 40 ms
4,384 KB
testcase_11 AC 37 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 51 ms
4,612 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 12 ms
4,380 KB
testcase_16 AC 82 ms
4,900 KB
testcase_17 AC 564 ms
14,060 KB
testcase_18 AC 551 ms
11,476 KB
testcase_19 AC 688 ms
11,296 KB
testcase_20 AC 650 ms
11,100 KB
testcase_21 AC 677 ms
11,352 KB
testcase_22 AC 217 ms
6,680 KB
testcase_23 AC 207 ms
6,472 KB
testcase_24 AC 231 ms
6,980 KB
testcase_25 AC 215 ms
6,800 KB
testcase_26 AC 624 ms
10,408 KB
testcase_27 AC 524 ms
9,832 KB
testcase_28 AC 703 ms
10,892 KB
testcase_29 AC 565 ms
14,008 KB
testcase_30 AC 545 ms
11,424 KB
testcase_31 AC 551 ms
14,208 KB
testcase_32 AC 543 ms
14,172 KB
testcase_33 AC 246 ms
6,740 KB
testcase_34 AC 242 ms
6,812 KB
testcase_35 AC 737 ms
11,208 KB
testcase_36 AC 738 ms
11,204 KB
testcase_37 AC 739 ms
11,292 KB
testcase_38 AC 734 ms
11,304 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 12 ms
4,380 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 4 ms
4,384 KB
testcase_43 AC 7 ms
4,380 KB
testcase_44 AC 17 ms
4,380 KB
testcase_45 AC 4 ms
4,380 KB
testcase_46 AC 14 ms
4,380 KB
testcase_47 AC 213 ms
7,016 KB
testcase_48 AC 212 ms
6,864 KB
testcase_49 AC 231 ms
6,860 KB
testcase_50 AC 215 ms
6,744 KB
testcase_51 AC 190 ms
6,404 KB
testcase_52 AC 136 ms
5,940 KB
testcase_53 AC 190 ms
6,620 KB
testcase_54 AC 12 ms
4,384 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,384 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